標量子查詢優化(用group by 代替distinct)
標量子查詢優化
當使用另外一個SELECT 語句來產生結果中的一列的值的時候,這個查詢必須只能返回一行一列的值。這種型別的子查詢被稱為標量子查詢
在某些情況下可以進行優化以減少標量子查詢的重複執行,但更糟糕的場景是每一行都需要標量子查詢的執行。
explain plan for SELECT B.EMP_NO,
B.CUST_NO,
B.CUST_NAME,
A.CARD_NO,
A.TRANS_AMT,
A.TRANS_ATTR,
/*(0 ?? 1 魯盲 2 魯盲?祿?4 ??)*/
A.TRANS_TIME,
A.SEQNO,
A.OLD_TRANSDATE
FROM (SELECT * FROM DWF.F_EVT_REAL_JOURLIST WHERE TRANS_TYPE = '00') A
LEFT JOIN (SELECT AGMT_ID,
CUST_MAGR EMP_NO,
CUST_NO,
(SELECT DISTINCT PTY_NAME
FROM DWF.F_PTY_TABLE
WHERE PTY_ID = A.CUST_NO
AND START_DT <= TO_DATE('2012-09-30', 'YYYY-MM-DD')
AND END_DT > TO_DATE('2012-09-30', 'YYYY-MM-DD')) CUST_NAME
FROM DWF.F_AGT_CADB_BOOK_H A
WHERE START_DT <= TO_DATE('2012-09-30', 'YYYY-MM-DD')
AND END_DT > TO_DATE('2012-09-30', 'YYYY-MM-DD')) B ON A.CARD_NO =
B.AGMT_ID;
--220109
SELECT AGMT_ID,
CUST_MAGR EMP_NO,
CUST_NO,
(SELECT DISTINCT PTY_NAME
FROM DWF.F_PTY_TABLE
WHERE PTY_ID = A.CUST_NO
AND START_DT <= TO_DATE('2012-09-30', 'YYYY-MM-DD')
AND END_DT > TO_DATE('2012-09-30', 'YYYY-MM-DD')) CUST_NAME
FROM DWF.F_AGT_CADB_BOOK_H A
WHERE START_DT <= TO_DATE('2012-09-30', 'YYYY-MM-DD')
AND END_DT > TO_DATE('2012-09-30', 'YYYY-MM-DD')
查詢返回了204947條記錄
###########################################################################################################################
Plan hash value: 579615344
---------------------------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads | OMem | 1Mem | Used-Mem |
---------------------------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 220K|00:00:12.12 | 781K| 280K| | | |
| 1 | SORT UNIQUE | |155K | 1 | 155K|00:00:06.52 | 501K| 2882 | 2048 | 2048 | 2048 (0)|
|* 2 | TABLE ACCESS BY INDEX ROWID | F_PTY_TABLE |155K | 1 | 155K|00:00:05.41 | 501K| 2882 | | | |
|* 3 | INDEX RANGE SCAN | SYS_C0052731 |155K | 1 | 188K|00:00:04.17 | 313K| 448 | | | |
|* 4 | HASH JOIN OUTER | | 1 | 1399K| 220K|00:00:12.12 | 781K| 280K| 20M| 2674K| 25M (0)|
|* 5 | TABLE ACCESS FULL | F_EVT_REAL_JOURLIST | 1 | 145K| 220K|00:00:00.08 | 7904 | 7900 | | | |
| 6 | VIEW | | 1 | 3929K| 204K|00:00:11.54 | 773K| 272K| | | |
|* 7 | TABLE ACCESS FULL | F_AGT_CADB_BOOK_H | 1 | 3929K| 204K|00:00:04.68 | 272K| 270K| | | |
--------------------------------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
2 - filter("END_DT">TO_DATE(' 2012-09-30 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
3 - access("PTY_ID"=:B1 AND "START_DT"<=TO_DATE(' 2012-09-30 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
filter("START_DT"<=TO_DATE(' 2012-09-30 00:00:00', 'syyyy-mm-dd hh24:mi:ss'))
4 - access("F_EVT_REAL_JOURLIST"."CARD_NO"="B"."AGMT_ID")
5 - filter("TRANS_TYPE"='00')
7 - filter(("START_DT"<=TO_DATE(' 2012-09-30 00:00:00', 'syyyy-mm-dd hh24:mi:ss') AND "END_DT">TO_DATE(' 2012-09-30 00:00:00',
'syyyy-mm-dd hh24:mi:ss')))
43 rows selected.
此時對F_PTY_TABLE索引掃描了155K次,回表了155K次 這種效率能高嗎?
改寫為關聯
###########################################################################################
SELECT b.emp_no,
b.cust_no,
b.cust_name,
a.card_no,
a.trans_amt,
a.trans_attr,
a.trans_time,
a.seqno,
a.old_transdate
FROM (SELECT * FROM dwf.f_evt_real_jourlist WHERE trans_type = '00') a
LEFT JOIN (SELECT agmt_id,
cust_magr emp_no,
cust_no,
c_n.pty_name AS cust_name
FROM dwf.f_agt_cadb_book_h a
LEFT JOIN (SELECT pty_name, pty_id
FROM dwf.f_pty_table
WHERE start_dt <=
to_date('2012-09-30', 'YYYY-MM-DD')
AND end_dt > to_date('2012-09-30', 'YYYY-MM-DD')
GROUP BY pty_name, pty_id) c_n
ON c_n.pty_id = a.cust_no
WHERE start_dt <= to_date('2012-09-30', 'YYYY-MM-DD')
AND end_dt > to_date('2012-09-30', 'YYYY-MM-DD')) b
ON a.card_no = b.agmt_id;
這裡巧妙的運用了group by 來去除重複資料,像偉大的教主致敬.
相關文章
- 用LEFT JOIN優化標量子查詢優化
- 查詢中的distinct與group by
- oracle之優化一用group by或exists優化distinctOracle優化
- 用WITH…AS改寫標量子查詢
- 標量子查詢
- 效能為王:SQL標量子查詢的優化案例分析SQL優化
- 標量子查詢(二)
- 標量子查詢(一)
- 都是標量子查詢惹的禍
- MYSQL學習筆記25: 多表查詢(子查詢)[標量子查詢,列子查詢]MySql筆記
- MYSQL count標量子查詢改left joinMySql
- GreatSQL 最佳化技巧:將 MINUS 改寫為標量子查詢SQL
- Oracle常用抓取SQL-標量子查詢等OracleSQL
- 帶彙總的標量子查詢改寫
- 查詢優化優化
- mysql 標量子查詢和現金盤程式製作非法子查詢MySql
- group by 查詢原理
- 增加Distinct後查詢效率反而提高——SQL優化之Everything is possibleSQL優化
- sql優化用group by 函式代替分析函式SQL優化函式
- pgsql查詢優化之模糊查詢SQL優化
- 影響Oracle標量子查詢效能的三個因素Oracle
- Oracle in 查詢優化Oracle優化
- MySQL查詢優化MySql優化
- join 查詢優化優化
- HBase查詢優化優化
- 查詢優化器優化
- SQL查詢優化SQL優化
- group by,having查詢 ”每**“的查詢
- group by分組查詢
- Group by 優化優化
- DISTINCT和GROUP BY的區別
- 【TUNE_ORACLE】列出有標量子查詢的SQL參考OracleSQL
- [20150709]慎用標量子查詢.txt
- [Mysql 查詢語句]——分組查詢group byMySql
- MySQL 的查詢優化MySql優化
- 分頁查詢優化優化
- MySQL 慢查詢優化MySql優化
- KunlunDB 查詢優化(一)優化