SQL優化經典案例----讓in/exists子查詢作為驅動表
---http://blog.sina.com.cn/s/blog_61cd89f60102efam.html
在NL連接配接方式中,用小表或小的結果集作業驅動表,是提高SQL執行效率的重要因素之一,下面我們看一下讓in/exists子查詢作為驅動表優化的案例
select rowid rid
from its_car_pass7 v
where 1 = 1
and pass_datetime >=
to_date('2013-07-06 :17:46:04', 'yyyy-mm-dd hh24:mi:ss')
and pass_datetime <=
to_date('2013-07-06 :18:46:06', 'yyyy-mm-dd hh24:mi:ss')
and v.pass_device_unid in
(select unid
from its_base_device
where dev_bay_unid in ('01685EFE4658C19D59C4DDAAEDD37393')
and dev_type = '1'
and dev_chk_flag = '1'
and dev_delete_flag = 'N')
order by v.pass_datetime asc
Execution Plan
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiATO2YSN0kzVlZVT0QlR2kHdpd0Uy4UMwAzLcBTO2cXbvwlbj5yZtlWYul2cuYzcvw1LcpDc0RHaiojIsJye.jpg)
Predicate Information (identified by operation id):
---------------------------------------------------
5 - access("PASS_DATETIME">=TO_DATE(' 2013-07-06 17:46:04', 'syyyy-mm-dd hh24:mi:ss') AND
"PASS_DATETIME"<=TO_DATE(' 2013-07-06 18:46:06', 'syyyy-mm-dd hh24:mi:ss'))
filter("PASS_DATETIME">=TO_DATE(' 2013-07-06 17:46:04', 'syyyy-mm-dd hh24:mi:ss') AND
"PASS_DATETIME"<=TO_DATE(' 2013-07-06 18:46:06', 'syyyy-mm-dd hh24:mi:ss'))
6 - access("V"."PASS_DEVICE_UNID"="UNID")
7 - filter("DEV_BAY_UNID"='01685EFE4658C19D59C4DDAAEDD37393' AND "DEV_TYPE"='1' AND
"DEV_DELETE_FLAG"='N' AND "DEV_CHK_FLAG"='1')
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
110973 consistent gets
0 physical reads
0 redo size
47861 bytes sent via SQL*Net to client
1656 bytes received via SQL*Net from client
105 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
1560 rows processed
檢視outline部分資訊
SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY(NULL, NULL, 'ADVANCED -PROJECTION'));
Outline Data
-------------
SQL優化後:
select
rowid rid
from its_car_pass7 v
where 1 = 1
and pass_datetime >=
to_date('2013-07-06 :17:46:04', 'yyyy-mm-dd hh24:mi:ss')
and pass_datetime <=
to_date('2013-07-06 :18:46:06', 'yyyy-mm-dd hh24:mi:ss')
and v.pass_device_unid in
(select unid
from its_base_device
where dev_bay_unid in ('01685EFE4658C19D59C4DDAAEDD37393')
and dev_type = '1'
and dev_chk_flag = '1'
and dev_delete_flag = 'N')
order by v.pass_datetime asc
優化後執行計劃資訊
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
18645 consistent gets
130 physical reads
0 redo size
47861 bytes sent via SQL*Net to client
1657 bytes received via SQL*Net from client
105 SQL*Net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
1560 rows processed
優化效果還是不錯的,邏輯讀有原有的 110973 變為:18645