1,某客户数据库系统发现如下SQL
select
M.BEGIN_DATE,
M.SERV_ID,
M.EXPIRE_DATE,
M.MAIN_REGION_CODE
M.SID,
M.SO_NBR ,
M.ROWID as MROWID___
from
I_WK_GROUP_MEMBER_852_ZXY M
where
M.SERV_ID = :1
AND M.MAIN_REGION_CODE = :2
AND M.SO_NBR = :3
AND M.SID = :4
order by
M.EXPIRE_DATE ASC
2,从执行计划看,使用全表扫描访问所需数据
3,表的统计信息
4,WHERE条件列的选择度不错

5,开始测试
5.1,数据库版本
SQL> select * from v$version;
BANNER
--------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
5.2,构建和上述SQL引用的类似表结构
SQL> create table t_order(so_nbr int,begin_date date,sid int);
Table created.
SQL> insert into t_order select level,sysdate-level,level+2 from dual connect by level<=100000;
100000 rows created.
SQL> commit;
Commit complete.
SQL> exec dbms_stats.gather_table_stats(user,'t_order',cascade=>true);
PL/SQL procedure successfully completed.
5.3,未创建索引,使用全表扫描,逻辑读为319
SQL> set autot traceonly
SQL> select so_nbr,begin_date,sid from t_order where so_nbr=1 and begin_date=to_date('2015-04-11 00:00:00','yyyy-mm-dd hh24:mi:ss') and sid=3 order by begin_date desc;
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 2317089884
-----------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 18 | 88 (2)| 00:00:02 |
|* 1 | TABLE ACCESS FULL| T_ORDER | 1 | 18 | 88 (2)| 00:00:02 |
-----------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("SO_NBR"=1 AND "SID"=3 AND "BEGIN_DATE"=TO_DATE('
2015-04-11 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
319 consistent gets
0 physical reads
0 redo size
471 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
5.4,创建SELECT的复合索引
SQL> set autot off
SQL> create index idx_t_order on t_order(so_nbr,begin_date,sid) online;
Index created.
SQL> exec dbms_stats.gather_table_stats(user,'t_order',cascade=>true);
PL/SQL procedure successfully completed.
5.5,构建复合索引后,执行计划为索引范围扫描,逻辑读为2(ORDER BY排序列在索引列中)
SQL> set autot traceonly
SQL> select so_nbr,begin_date,sid from t_order where so_nbr=1 and begin_date=to_date('2015-04-11 00:00:00','yyyy-mm-dd hh24:mi:ss') and sid=3 order by begin_date desc;
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 3942612543
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 18 | 1 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| IDX_T_ORDER | 1 | 18 | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("SO_NBR"=1 AND "BEGIN_DATE"=TO_DATE(' 2015-04-11
00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "SID"=3)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
471 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
5.6,构建不包含ORDER BY排序列的复合索引
SQL> set autot off
SQL> drop index idx_t_order;
Index dropped.
SQL> create index idx_t_order on t_order(so_nbr,sid) online;
Index created.
SQL> exec dbms_stats.gather_table_stats(user,'t_order',cascade=>true);
PL/SQL procedure successfully completed.
5.7,基于不包含ORDER BY 排序列的复合索引,仍会索引范围扫描,然后回表访问所需数据,逻辑读为3
SQL> select so_nbr,begin_date,sid from t_order where so_nbr=1 and begin_date=to_date('2015-04-11 00:00:00','yyyy-mm-dd hh24:mi:ss') and sid=3 order by begin_date desc;
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 2436638254
-------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 18 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS BY INDEX ROWID| T_ORDER | 1 | 18 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_T_ORDER | 1 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("BEGIN_DATE"=TO_DATE(' 2015-04-11 00:00:00', 'syyyy-mm-dd
hh24:mi:ss'))
2 - access("SO_NBR"=1 AND "SID"=3)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
471 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
5.8,仅基于ORDER BY 排序列构建索引
SQL> set autot off
SQL> drop index idx_t_order;
Index dropped.
SQL> create index idx_t_order on t_order(begin_date) online;
Index created.
SQL> exec dbms_stats.gather_table_stats(user,'t_order',cascade=>true);
PL/SQL procedure successfully completed.
5.9,仅基于ORDER BY 排序列构建索引,先是索引范围扫描,然后回表,逻辑读为2
SQL> set autot traceonly
SQL> select so_nbr,begin_date,sid from t_order where so_nbr=1 and begin_date=to_date('2015-04-11 00:00:00','yyyy-mm-dd hh24:mi:ss') and sid=3 order by begin_date desc;
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 2436638254
-------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 18 | 2 (0)| 00:00:01 |
|* 1 | TABLE ACCESS BY INDEX ROWID| T_ORDER | 1 | 18 | 2 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_T_ORDER | 1 | | 1 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("SO_NBR"=1 AND "SID"=3)
2 - access("BEGIN_DATE"=TO_DATE(' 2015-04-11 00:00:00', 'syyyy-mm-dd
hh24:mi:ss'))
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
471 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
6.0,清空表记录
SQL> set autot off
SQL> truncate table t_order;
Table truncated.
6.1,增加表记录,对比仅ORDER BY 排序列的索引及包含ORDER BY 排序列的索引的性能区别
SQL> set autot off
SQL> truncate table t_order;
Table truncated.
SQL> insert into t_order select level,sysdate-level,level+2 from dual connect by level<=1000000;
1000000 rows created.
SQL> commit;
Commit complete.
SQL> drop index idx_t_order;
Index dropped.
6.2,包含order by排序列的复合索引
SQL> create index idx_t_order on t_order(so_nbr,begin_date,sid) online;
Index created.
SQL> exec dbms_stats.gather_table_stats(user,'t_order',cascade=>true);
PL/SQL procedure successfully completed.
6.3,包含order by排序列的复合索引,逻辑读为3
SQL> set autot traceonly
SQL> select so_nbr,begin_date,sid from t_order where so_nbr=1 and begin_date=to_date('2015-04-11 00:00:00','yyyy-mm-dd hh24:mi:ss') and sid=3 order by begin_date desc;
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 3942612543
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 18 | 3 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| IDX_T_ORDER | 1 | 18 | 3 (0)| 00:00:01 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("SO_NBR"=1 AND "BEGIN_DATE"=TO_DATE(' 2015-04-11
00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "SID"=3)
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
471 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
6.4,构建仅ORDER BY 排序列的索引
SQL> set autot off
SQL> drop index idx_t_order;
Index dropped.
SQL> create index idx_t_order on t_order(begin_date) online;
Index created.
SQL> exec dbms_stats.gather_table_stats(user,'t_order',cascade=>true);
PL/SQL procedure successfully completed.
6.5,仅ORDER BY 排序列的索引,逻辑读为3
SQL> select so_nbr,begin_date,sid from t_order where so_nbr=1 and begin_date=to_date('2015-04-11 00:00:00','yyyy-mm-dd hh24:mi:ss') and sid=3 order by begin_date desc;
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 2436638254
-------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 18 | 4 (0)| 00:00:01 |
|* 1 | TABLE ACCESS BY INDEX ROWID| T_ORDER | 1 | 18 | 4 (0)| 00:00:01 |
|* 2 | INDEX RANGE SCAN | IDX_T_ORDER | 1 | | 3 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("SO_NBR"=1 AND "SID"=3)
2 - access("BEGIN_DATE"=TO_DATE(' 2015-04-11 00:00:00', 'syyyy-mm-dd
hh24:mi:ss'))
Statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
471 bytes sent via SQL*Net to client
508 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
小结:
1,构建包含ORDER BY 排序列的复合索引,不会回表
2,仅ORDER BY 排序列的索引,会回表,但逻辑读和包含ORDER BY 排序列的复合索引消耗的逻辑读相同,但成本不同,后者比前者低一些
3,不包含ORDER BY 排序列的索引,会回表,但这种逻辑读和成本比第1,2方案都高
个人简介
项目经验:
贵州移动crm及客服数据库性能优化项目
贵州移动crm及客服务数据库sql审核项目