標量子查詢優化(用group by 代替distinct)

zhaoyangjian724發表於2013-12-06
標量子查詢優化 

當使用另外一個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 來去除重複資料,像偉大的教主致敬.


 

相關文章