order by 列加索引

本文通过一个具体的SQL查询案例,展示了如何通过创建索引来显著提高查询效率。对比了使用全表扫描与索引范围扫描的不同执行计划及性能差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


create table t_order_tb 
as
select * from dba_objects ; 
select * from t_order_tb t where t.object_id > 2 ;
select * from t_order_tb t where t.object_id > 2 order by t.object_id;
create index ind_order_object_id on t_order_tb(object_id) ;
select * from t_order_tb t where t.object_id > 2 order by t.object_id;
select t.object_id from t_order_tb t where t.object_id > 2 order by t.object_id;



SQL> set autotrace traceonly;
SQL> select * from t_order_tb t where t.object_id > 2 ;

已选择50628行。

已用时间:  00: 00: 02.25

执行计划
----------------------------------------------------------
Plan hash value: 2653850918

--------------------------------------------------------------------------------
| Id  | Operation         | Name       | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |            | 41858 |  7235K|   162   (3)| 00:00:02 |
|*  1 |  TABLE ACCESS FULL| T_ORDER_TB | 41858 |  7235K|   162   (3)| 00:00:02 |
--------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - filter("T"."OBJECT_ID">2)

Note
-----
   - dynamic sampling used for this statement


统计信息
----------------------------------------------------------
          4  recursive calls
          0  db block gets
       4080  consistent gets
        399  physical reads
          0  redo size
    2453530  bytes sent via SQL*Net to client
      37510  bytes received via SQL*Net from client
       3377  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      50628  rows processed

SQL>
SQL> select * from t_order_tb t where t.object_id > 2 order by t.object_id;

已选择50628行。

已用时间:  00: 00: 01.50

执行计划
----------------------------------------------------------
Plan hash value: 3469110092

-----------------------------------------------------------------------------------------
| Id  | Operation          | Name       | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
-----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT   |            | 41858 |  7235K|       |  1793   (1)| 00:00:22 |
|   1 |  SORT ORDER BY     |            | 41858 |  7235K|    16M|  1793   (1)| 00:00:22 |
|*  2 |   TABLE ACCESS FULL| T_ORDER_TB | 41858 |  7235K|       |   162   (3)| 00:00:02 |
-----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - filter("T"."OBJECT_ID">2)

Note
-----
   - dynamic sampling used for this statement


统计信息
----------------------------------------------------------
          4  recursive calls
          0  db block gets
        758  consistent gets
          0  physical reads
          0  redo size
    2447912  bytes sent via SQL*Net to client
      37510  bytes received via SQL*Net from client
       3377  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
      50628  rows processed

SQL> select * from t_order_tb t where t.object_id > 2 order by t.object_id;

已选择50628行。

已用时间:  00: 00: 01.68

执行计划
----------------------------------------------------------
Plan hash value: 780952064

---------------------------------------------------------------------------------------------------
| Id  | Operation                   | Name                | Rows  | Bytes | Cost (%CPU)| Time     |
---------------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT            |                     | 41858 |  7235K|   876   (1)| 00:00:11 |
|   1 |  TABLE ACCESS BY INDEX ROWID| T_ORDER_TB          | 41858 |  7235K|   876   (1)| 00:00:11 |
|*  2 |   INDEX RANGE SCAN          | IND_ORDER_OBJECT_ID | 41858 |       |   126   (2)| 00:00:02 |
---------------------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   2 - access("T"."OBJECT_ID">2)
       filter("T"."OBJECT_ID">2)

Note
-----
   - dynamic sampling used for this statement


统计信息
----------------------------------------------------------
          9  recursive calls
          0  db block gets
       7609  consistent gets
        112  physical reads
          0  redo size
    5439440  bytes sent via SQL*Net to client
      37510  bytes received via SQL*Net from client
       3377  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      50628  rows processed

SQL>
SQL> select t.object_id from t_order_tb t where t.object_id > 2 order by t.object_id;

已选择50628行。

已用时间:  00: 00: 00.48

执行计划
----------------------------------------------------------
Plan hash value: 957369183

----------------------------------------------------------------------------------------
| Id  | Operation        | Name                | Rows  | Bytes | Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT |                     | 41858 |   531K|   126   (2)| 00:00:02 |
|*  1 |  INDEX RANGE SCAN| IND_ORDER_OBJECT_ID | 41858 |   531K|   126   (2)| 00:00:02 |
----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   1 - access("T"."OBJECT_ID">2)
       filter("T"."OBJECT_ID">2)

Note
-----
   - dynamic sampling used for this statement


统计信息
----------------------------------------------------------
          7  recursive calls
          0  db block gets
       3546  consistent gets
          0  physical reads
          0  redo size
     732819  bytes sent via SQL*Net to client
      37510  bytes received via SQL*Net from client
       3377  SQL*Net roundtrips to/from client
          0  sorts (memory)
          0  sorts (disk)
      50628  rows processed

### MySQL 中对时间索引的影响 对于 `time` 或其他日期时间类型的字段,在 MySQL 数据库中创建索引可以显著提高涉及这些的查询性能。当表中的记录数量较大时,未索引的时间可能导致全表扫描操作,这会消耗大量资源并降低读取速度。 通过为时间戳或日期类型的数据建立 B-Tree 索引,MySQL 能够更高效地处理基于范围的选择条件以及精确匹配的情况。例如,如果经常执行类似于 `SELECT * FROM table WHERE date_column BETWEEN 'start_date' AND 'end_date';` 的语句,则应该考虑在此类上构建索引快响应时间[^1]。 然而需要注意的是,并不是所有的场景下都适合给时间加上索引: - 如果该为频繁更新的对象(如每次插入新行都会更改当前时间),那么维护额外的索引结构可能会带来写入开销; - 对于非常小规模的数据集来说,增索引可能不会明显改善性能反而增了存储成本; 为了验证具体应用环境中索引的效果,可以通过对比有无索引状态下相同 SQL 查询所花费的时间来进行评估。下面是一个简单的测试脚本用于测量带索引与不带索引两种情况下 SELECT 语句的速度差异: ```sql -- 创建测试用临时表, 并填充一些随机数据 CREATE TEMPORARY TABLE test_time_index ( id INT AUTO_INCREMENT PRIMARY KEY, created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ); INSERT INTO test_time_index (created_at) VALUES ('2023-01'),...,('2024-01-01'); -- 插入多条记录... -- 测量没有索引时查询耗时 SET @noindex_start=NOW(); SELECT COUNT(*) FROM test_time_index WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY); SET @noindex_end=NOW(); -- 给 created_at 字段索引 ALTER TABLE test_time_index ADD INDEX idx_created_at(created_at); -- 再次测量带有索引后的查询耗时 SET @withindex_start=NOW(); SELECT COUNT(*) FROM test_time_index WHERE created_at >= DATE_SUB(NOW(), INTERVAL 7 DAY); SET @withindex_end=NOW(); -- 输出结果比较两者之间的区别 SELECT TIMESTAMPDIFF(MICROSECOND,@noindex_start ,@noindex_end ) AS noIndexTime , TIMESTAMPDIFF(MICROSECOND,@withindex_start ,@withindex_end )AS withIndexTime; ``` 上述代码片段展示了如何在一个小型实验里模拟不同条件下同一查询的表现变化情况。实际生产环境下的影响因素更多样化,因此建议依据具体的业务逻辑来决定是否有必要实施此类优化措施[^2].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值