一次內鏈子查詢優化 2
----------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
----------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 0 |00:00:13.11 | 3115K|
| 1 | SORT ORDER BY | | 1 | 2 | 0 |00:00:13.11 | 3115K|
| 2 | CONCATENATION | | 1 | | 0 |00:00:13.11 | 3115K|
|* 3 | FILTER | | 1 | | 0 |00:00:06.44 | 1557K|
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers |
----------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 0 |00:00:13.11 | 3115K|
| 1 | SORT ORDER BY | | 1 | 2 | 0 |00:00:13.11 | 3115K|
| 2 | CONCATENATION | | 1 | | 0 |00:00:13.11 | 3115K|
|* 3 | FILTER | | 1 | | 0 |00:00:06.44 | 1557K|
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|* 4 | TABLE ACCESS BY INDEX ROWID | yy | 1 | 1 | 0 |00:00:06.44 | 1557K| 6
|* 5 | INDEX RANGE SCAN | yy| 1 | 169 | 181K|00:00:05.81 | 1452K| 5
|* 6 | TABLE ACCESS BY INDEX ROWID | yy | 181K| 1 | 181K|00:00:02.41 | 725K| 2
|* 7 | INDEX RANGE SCAN | yy| 181K| 1 | 181K|00:00:01.69 | 544K| 1
|* 8 | TABLE ACCESS BY INDEX ROWID| yy | 181K| 1 | 181K|00:00:02.21 | 725K| 4
|* 9 | INDEX RANGE SCAN | yy | 181K| 2 | 181K|00:00:01.49 | 544K| 3
| 10 | NESTED LOOPS | | 0 | 1 | 0 |00:00:00.01 | 0 |
| 11 | TABLE ACCESS BY INDEX ROWID | yy | 0 | 1 | 0 |00:00:00.01 | 0 |
|* 12 | INDEX UNIQUE SCAN | yy| 0 | 1 | 0 |00:00:00.01 | 0 |
|* 13 | TABLE ACCESS FULL | yy| 0 | 1 | 0 |00:00:00.01 | 0 |
|* 14 | FILTER | | 1 | | 0 |00:00:06.67 | 1557K|
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|* 4 | TABLE ACCESS BY INDEX ROWID | yy | 1 | 1 | 0 |00:00:06.44 | 1557K| 6
|* 5 | INDEX RANGE SCAN | yy| 1 | 169 | 181K|00:00:05.81 | 1452K| 5
|* 6 | TABLE ACCESS BY INDEX ROWID | yy | 181K| 1 | 181K|00:00:02.41 | 725K| 2
|* 7 | INDEX RANGE SCAN | yy| 181K| 1 | 181K|00:00:01.69 | 544K| 1
|* 8 | TABLE ACCESS BY INDEX ROWID| yy | 181K| 1 | 181K|00:00:02.21 | 725K| 4
|* 9 | INDEX RANGE SCAN | yy | 181K| 2 | 181K|00:00:01.49 | 544K| 3
| 10 | NESTED LOOPS | | 0 | 1 | 0 |00:00:00.01 | 0 |
| 11 | TABLE ACCESS BY INDEX ROWID | yy | 0 | 1 | 0 |00:00:00.01 | 0 |
|* 12 | INDEX UNIQUE SCAN | yy| 0 | 1 | 0 |00:00:00.01 | 0 |
|* 13 | TABLE ACCESS FULL | yy| 0 | 1 | 0 |00:00:00.01 | 0 |
|* 14 | FILTER | | 1 | | 0 |00:00:06.67 | 1557K|
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|* 15 | TABLE ACCESS BY INDEX ROWID | yy | 1 | 1 | 0 |00:00:06.67 | 1557K|
|* 16 | INDEX RANGE SCAN | yy| 1 | 459 | 181K|00:00:05.99 | 1452K|
|* 17 | TABLE ACCESS BY INDEX ROWID | yy | 181K| 1 | 181K|00:00:02.53 | 725K|
|* 18 | INDEX RANGE SCAN |yy| 181K| 1 | 181K|00:00:01.80 | 544K|
|* 19 | TABLE ACCESS BY INDEX ROWID| yy | 181K| 1 | 181K|00:00:02.29 | 725K|
|* 20 | INDEX RANGE SCAN | yy | 181K| 2 | 181K|00:00:01.55 | 544K|
|* 21 | TABLE ACCESS BY INDEX ROWID | yy | 181K| 1 | 181K|00:00:02.41 | 725K|
|* 22 | INDEX RANGE SCAN | yy| 181K| 1 | 181K|00:00:01.69 | 544K|
|* 23 | TABLE ACCESS BY INDEX ROWID | yy | 181K| 1 | 181K|00:00:02.21 | 725K|
|* 24 | INDEX RANGE SCAN | yy | 181K| 2 | 181K|00:00:01.49 | 544K|
| 25 | NESTED LOOPS | | 0 | 1 | 0 |00:00:00.01 | 0 |
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
|* 15 | TABLE ACCESS BY INDEX ROWID | yy | 1 | 1 | 0 |00:00:06.67 | 1557K|
|* 16 | INDEX RANGE SCAN | yy| 1 | 459 | 181K|00:00:05.99 | 1452K|
|* 17 | TABLE ACCESS BY INDEX ROWID | yy | 181K| 1 | 181K|00:00:02.53 | 725K|
|* 18 | INDEX RANGE SCAN |yy| 181K| 1 | 181K|00:00:01.80 | 544K|
|* 19 | TABLE ACCESS BY INDEX ROWID| yy | 181K| 1 | 181K|00:00:02.29 | 725K|
|* 20 | INDEX RANGE SCAN | yy | 181K| 2 | 181K|00:00:01.55 | 544K|
|* 21 | TABLE ACCESS BY INDEX ROWID | yy | 181K| 1 | 181K|00:00:02.41 | 725K|
|* 22 | INDEX RANGE SCAN | yy| 181K| 1 | 181K|00:00:01.69 | 544K|
|* 23 | TABLE ACCESS BY INDEX ROWID | yy | 181K| 1 | 181K|00:00:02.21 | 725K|
|* 24 | INDEX RANGE SCAN | yy | 181K| 2 | 181K|00:00:01.49 | 544K|
| 25 | NESTED LOOPS | | 0 | 1 | 0 |00:00:00.01 | 0 |
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 26 | TABLE ACCESS BY INDEX ROWID| yy | 0 | 1 | 0 |00:00:00.01 | 0 |
|* 27 | INDEX UNIQUE SCAN | yy | 0 | 1 | 0 |00:00:00.01 | 0 |
|* 28 | TABLE ACCESS FULL | yy| 0 | 1 | 0 |00:00:00.01 | 0 |
----------------------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| 26 | TABLE ACCESS BY INDEX ROWID| yy | 0 | 1 | 0 |00:00:00.01 | 0 |
|* 27 | INDEX UNIQUE SCAN | yy | 0 | 1 | 0 |00:00:00.01 | 0 |
|* 28 | TABLE ACCESS FULL | yy| 0 | 1 | 0 |00:00:00.01 | 0 |
----------------------------------------------------------------------------------------------------------------------
開發使用了內聯子查詢,內聯子查詢會根據條件把外層表的每條資料到內層表進行一次匹配,如果是全表掃描就要進行這樣多行的全表掃描,有點像NEST LOOP,所以這裡
start才會是181K,所以重點落到如何改寫內聯子查詢。
我如下改寫
start才會是181K,所以重點落到如何改寫內聯子查詢。
我如下改寫
select count(*) from (
Select *
From ppp
Where (c1 = '0501' and c2 = 'test' and c3 <= sysdate)
AND (1 = 0 OR SUBSTR(c4, 4, 1) = '0')
AND c5 IN
('1', '2', '3', '4', '5',
'6')
or (c6 = '0501' and c6 = 'test' and c7 <= sysdate)
AND (1 = 0 OR SUBSTR(c4, 4, 1) = '0')
and (123 in
(select 123
from 123
where 123 in (select 123
from 123
where 123 = 'vicky.li')) and
agentcode = 'vicky.li')
and riskcode = '0501') t,
(SELECT DISTINCT t1 FROM sdf where 123 = '0501') p,
(SELECT DISTINCT t2 FROM 123 WHERE 123 = '0501') c
where t.123=p.ProposalNo and t.123=c.123
ORDER BY t.123 desc, t.123 desc;
Select *
From ppp
Where (c1 = '0501' and c2 = 'test' and c3 <= sysdate)
AND (1 = 0 OR SUBSTR(c4, 4, 1) = '0')
AND c5 IN
('1', '2', '3', '4', '5',
'6')
or (c6 = '0501' and c6 = 'test' and c7 <= sysdate)
AND (1 = 0 OR SUBSTR(c4, 4, 1) = '0')
and (123 in
(select 123
from 123
where 123 in (select 123
from 123
where 123 = 'vicky.li')) and
agentcode = 'vicky.li')
and riskcode = '0501') t,
(SELECT DISTINCT t1 FROM sdf where 123 = '0501') p,
(SELECT DISTINCT t2 FROM 123 WHERE 123 = '0501') c
where t.123=p.ProposalNo and t.123=c.123
ORDER BY t.123 desc, t.123 desc;
以前語句13秒,現在語句2秒。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7728585/viewspace-694084/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 一次內鏈子查詢優化 1優化
- 涉及子查詢sql的一次優化SQL優化
- exists與in子查詢優化優化
- MySQL子查詢的優化薦MySql優化
- 一次分頁查詢的優化優化
- Oracle not exist子查詢全掃的優化Oracle優化
- 優化擁有謂詞or的子查詢優化
- PostgreSQL 原始碼解讀(27)- 查詢語句#12(查詢優化-上拉子連結#2)SQL原始碼優化
- Mysql優化系列之——優化器對子查詢的處理MySql優化
- 一文終結SQL 子查詢優化SQL優化
- mysql子查詢的缺陷以及5.6的優化MySql優化
- PostgreSQL 原始碼解讀(29)- 查詢語句#14(查詢優化-上拉子查詢)SQL原始碼優化
- 查詢優化優化
- 一次系統檢視查詢的優化優化
- 優化-mysql子查詢索引失效問題解決優化MySql索引
- 一個NOT EXISTS含有OR條件子查詢的優化優化
- pgsql查詢優化之模糊查詢SQL優化
- 子查詢-表子查詢
- Oracle in 查詢優化Oracle優化
- MySQL查詢優化MySql優化
- join 查詢優化優化
- HBase查詢優化優化
- 查詢優化器優化
- SQL查詢優化SQL優化
- Oracle_Day2 子查詢Oracle
- MySQL 優化五(關聯查詢子查詢以及 in 的效率問題)(高階篇)MySql優化
- 記一次 Golang 資料庫查詢元件的優化。Golang資料庫元件優化
- Mysql第七天查詢優化2MySql優化
- StoneDB 子查詢最佳化
- PostgreSQL 原始碼解讀(17)- 查詢語句#2(查詢優化基礎)SQL原始碼優化
- MySQL 的查詢優化MySql優化
- 分頁查詢優化優化
- MySQL 慢查詢優化MySql優化
- KunlunDB 查詢優化(一)優化
- MySQL優化COUNT()查詢MySql優化
- 優化sql查詢速度優化SQL
- EntityFramework優化:查詢效能Framework優化
- EntityFramework優化:查詢WITH(NOLOCK)Framework優化