sql效率优化:用union all代替or

高效:
explain plan for
SELECT COUNT(a.product_no) AS cnt, a.area_code, b.current_name
  FROM t_info_customer     a,
       t_info_account_card c,
       T_DICT_AREA_CITY    b,
       v_txn_log_his      txn
 where a.area_code = b.current_code
   and a.customer_no = c.customer_no
   and c.binding_flag = '1'
   and c.binding_time <= TO_DATE('20121130', 'yyyyMMdd')
   AND txn.TXN_CHANNEL IN ('02', '03', '04', '05', '06', '08') --线下
   AND a.status <> '9'
   AND a.customer_no LIKE '86%'
   and   c.fund_account_no = txn.account_no
  GROUP BY a.area_code, b.current_name;
 
SQL> select * from table(dbms_xplan.display);
 
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1913910982
--------------------------------------------------------------------------------
| Id  | Operation               | Name                      | Rows  | Bytes |Tem
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT        |                           |  5290 |   542K|
|   1 |  HASH GROUP BY          |                           |  5290 |   542K|
|*  2 |   HASH JOIN             |                           |  7718 |   791K|
|   3 |    TABLE ACCESS FULL    | T_DICT_AREA_CITY          |   374 |  7480 |
|*  4 |    HASH JOIN            |                           |  7731 |   641K|
|*  5 |     HASH JOIN           |                           |  7949 |   457K|  1
|*  6 |      TABLE ACCESS FULL  | T_INFO_ACCOUNT_CARD       | 30701 |  1409K|
|   7 |      VIEW               | V_TXN_LOG_HIS             |   113K|  1332K|
|   8 |       UNION-ALL         |                           |       |       |
|*  9 |        TABLE ACCESS FULL| T_LOG_ONLINE_PAYMENT_HIS  |   113K|  2221K|
|* 10 |        TABLE ACCESS FULL| T_LOG_OFFLINE_PAYMENT_HIS |     3 |    60 |
|* 11 |     TABLE ACCESS FULL   | T_INFO_CUSTOMER           | 29860 |   758K|
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
 
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
---------------------------------------------------
   2 - access("A"."AREA_CODE"="B"."CURRENT_CODE")
   4 - access("A"."CUSTOMER_NO"="C"."CUSTOMER_NO")
   5 - access("C"."FUND_ACCOUNT_NO"="TXN"."ACCOUNT_NO")
   6 - filter("C"."CUSTOMER_NO" LIKE '86%' AND "C"."BINDING_FLAG"='1' AND
              "C"."BINDING_TIME"<=TIMESTAMP' 2012-11-30 00:00:00')
   9 - filter("TXN_CHANNEL"='02' OR "TXN_CHANNEL"='03' OR "TXN_CHANNEL"='04' OR
              OR "TXN_CHANNEL"='06' OR "TXN_CHANNEL"='08')
  10 - filter("TXN_CHANNEL"='02' OR "TXN_CHANNEL"='03' OR "TXN_CHANNEL"='04' OR
              OR "TXN_CHANNEL"='06' OR "TXN_CHANNEL"='08')
  11 - filter("A"."CUSTOMER_NO" LIKE '86%' AND "A"."STATUS"<>'9')
 
32 rows selected
 

create or replace view v_txn_log_his as
select "TXN_SEQ_NO","SETTLE_DATE","TXN_TIME","TXN_MONTH","BUSINESS_TYPE","TXN_TYPE","TXN_DSCPT","TXN_CHANNEL","ACCEPT_ORG_CODE","ACCEPT_ORG_TYPE","ACCEPT_TRANS_SEQ_NO","ACCEPT_TRANS_DATE","ACCEPT_TRANS_TIME","TRANSFER_ORG_CODE","TRANSFER_ORG_TYPE","TRANSFER_TRANS_SEQ_NO","TRANSFER_TRANS_DATE","TRANSFER_TRANS_TIME","PAY_ORG_CODE","PAY_ORG_TYPE","PAY_TRANS_SEQ_NO","PAY_TRANS_DATE","PAY_TRANS_TIME","SUPPLY_ORG_CODE","SUPPLY_ORG_TYPE","SUPPLY_TRANS_SEQ_NO","SUPPLY_TRANS_DATE","SUPPLY_TRANS_TIME","TXN_AMT","BEFORE_AMT","AFTER_AMT","FEE_IN_FLAG","PROFIT_IN_FLAG","FEE_AMT","PROFIT_AMT","P_FEE_CODE","P_FEE_AMT","ACCEPT_FEE_CODE","ACCEPT_FEE_AMT","ACCEPT_FEE_FLAG","TRANSFER_FEE_CODE","TRANSFER_FEE_AMT","TRANSFER_FEE_FLAG","PAY_FEE_CODE","PAY_FEE_AMT","PAY_FEE_FLAG","SUPPLY_FEE_CODE","SUPPLY_FEE_AMT","SUPPLY_FEE_FLAG","CARD_SN","CARD_CNT","TXN_TAC","TAC_CHECK_LABEL","CARD_DPT_AMT","CARD_EXP_DATE","BATCH_FILE_ID","UPLOAD_FLAG","UPLOAD_PKG_NO","INNER_CARD_NO","OUTER_CARD_NO","CARD_MEDIA_TYPE","CARD_BRAND_TYPE","ACCOUNT_TYPE","ACCOUNT_NO","AREA_CODE","CITY_CODE","TERMINAL_NO","TERMINAL_SEQ_NO","TERMINAL_COMM_SEQ_NO","TERMINAL_OPER_NO","BATCH_NO","TRANS_SEQ_TYPE","ACCEPT_MATCH_FLAG","TRANSFER_MATCH_FLAG","PAY_MATCH_FLAG","SUPPLY_MATCH_FLAG","PAYMENT_OBJ_NO","PAYMENT_OBJ_TYPE","L_ACCEPT_TRANS_SEQ_NO","L_ACCEPT_TRANS_DATE","L_ACCEPT_TRANS_TIME","L_TRANSFER_TRANS_SEQ_NO","L_TRANSFER_TRANS_DATE","L_TRANSFER_TRANS_TIME","L_PAY_TRANS_SEQ_NO","L_PAY_TRANS_DATE","L_PAY_TRANS_TIME","L_SUPPLY_TRANS_SEQ_NO","L_SUPPLY_TRANS_DATE","L_SUPPLY_TRANS_TIME","PSAM_CARD_NO","PRE_AUTH_SEQ_NO","ACCEPT_PROC_FLAG","TRANSFER_PROC_FLAG","PAY_PROC_FLAG","SUPPLY_PROC_FLAG","ACCEPT_RESP_CODE","TRANSFER_RESP_CODE","PAY_RESP_CODE","SUPPLY_CODE","TXN_FIN_TS","TXN_TO_TS","IS_CLEARING","CLEARING_ID","RESEND_NUM","SETTLE_MONTH","SETTLE_DAY","REVSAL_FLAG","REVSAL_TXN_SEQ_NO","CANCEL_FLAG","CANCEL_TXN_SEQ_NO","L_TXN_SEQ_NO","RETURN_FLAG","RETURN_COUNT","RETURN_AMT","DUBIOUS_FLAG","BATCH_DATE","BATCH_FLAG","ACCEPT_SETTLE_AMT","TRANSFER_SETTLE_AMT","PAY_SETTLE_AMT","SUPPLY_SETTLE_AMT","TRANSFER_IN_ORG_CODE","IN_ACCOUNT_NO","TRANSFER_OUT_ORG_CODE","OUT_ACCOUNT_NO","RESV_FLD1","RESV_FLD2","RESV_FLD3","RESV_FLD4","RESV_FLD5" from t_log_online_payment_his union all select "TXN_SEQ_NO","SETTLE_DATE","TXN_TIME","TXN_MONTH","BUSINESS_TYPE","TXN_TYPE","TXN_DSCPT","TXN_CHANNEL","ACCEPT_ORG_CODE","ACCEPT_ORG_TYPE","ACCEPT_TRANS_SEQ_NO","ACCEPT_TRANS_DATE","ACCEPT_TRANS_TIME","TRANSFER_ORG_CODE","TRANSFER_ORG_TYPE","TRANSFER_TRANS_SEQ_NO","TRANSFER_TRANS_DATE","TRANSFER_TRANS_TIME","PAY_ORG_CODE","PAY_ORG_TYPE","PAY_TRANS_SEQ_NO","PAY_TRANS_DATE","PAY_TRANS_TIME","SUPPLY_ORG_CODE","SUPPLY_ORG_TYPE","SUPPLY_TRANS_SEQ_NO","SUPPLY_TRANS_DATE","SUPPLY_TRANS_TIME","TXN_AMT","BEFORE_AMT","AFTER_AMT","FEE_IN_FLAG","PROFIT_IN_FLAG","FEE_AMT","PROFIT_AMT","P_FEE_CODE","P_FEE_AMT","ACCEPT_FEE_CODE","ACCEPT_FEE_AMT","ACCEPT_FEE_FLAG","TRANSFER_FEE_CODE","TRANSFER_FEE_AMT","TRANSFER_FEE_FLAG","PAY_FEE_CODE","PAY_FEE_AMT","PAY_FEE_FLAG","SUPPLY_FEE_CODE","SUPPLY_FEE_AMT","SUPPLY_FEE_FLAG","CARD_SN","CARD_CNT","TXN_TAC","TAC_CHECK_LABEL","CARD_DPT_AMT","CARD_EXP_DATE","BATCH_FILE_ID","UPLOAD_FLAG","UPLOAD_PKG_NO","INNER_CARD_NO","OUTER_CARD_NO","CARD_MEDIA_TYPE","CARD_BRAND_TYPE","ACCOUNT_TYPE","ACCOUNT_NO","AREA_CODE","CITY_CODE","TERMINAL_NO","TERMINAL_SEQ_NO","TERMINAL_COMM_SEQ_NO","TERMINAL_OPER_NO","BATCH_NO","TRANS_SEQ_TYPE","ACCEPT_MATCH_FLAG","TRANSFER_MATCH_FLAG","PAY_MATCH_FLAG","SUPPLY_MATCH_FLAG","PAYMENT_OBJ_NO","PAYMENT_OBJ_TYPE","L_ACCEPT_TRANS_SEQ_NO","L_ACCEPT_TRANS_DATE","L_ACCEPT_TRANS_TIME","L_TRANSFER_TRANS_SEQ_NO","L_TRANSFER_TRANS_DATE","L_TRANSFER_TRANS_TIME","L_PAY_TRANS_SEQ_NO","L_PAY_TRANS_DATE","L_PAY_TRANS_TIME","L_SUPPLY_TRANS_SEQ_NO","L_SUPPLY_TRANS_DATE","L_SUPPLY_TRANS_TIME","PSAM_CARD_NO","PRE_AUTH_SEQ_NO","ACCEPT_PROC_FLAG","TRANSFER_PROC_FLAG","PAY_PROC_FLAG","SUPPLY_PROC_FLAG","ACCEPT_RESP_CODE","TRANSFER_RESP_CODE","PAY_RESP_CODE","SUPPLY_CODE","TXN_FIN_TS","TXN_TO_TS","IS_CLEARING","CLEARING_ID","RESEND_NUM","SETTLE_MONTH","SETTLE_DAY","REVSAL_FLAG","REVSAL_TXN_SEQ_NO","CANCEL_FLAG","CANCEL_TXN_SEQ_NO","L_TXN_SEQ_NO","RETURN_FLAG","RETURN_COUNT","RETURN_AMT","DUBIOUS_FLAG","BATCH_DATE","BATCH_FLAG","ACCEPT_SETTLE_AMT","TRANSFER_SETTLE_AMT","PAY_SETTLE_AMT","SUPPLY_SETTLE_AMT","TRANSFER_IN_ORG_CODE","IN_ACCOUNT_NO","TRANSFER_OUT_ORG_CODE","OUT_ACCOUNT_NO","RESV_FLD1","RESV_FLD2","RESV_FLD3","RESV_FLD4","RESV_FLD5" from t_log_offline_payment_his;
 
 
 
 
 
 
 
用or低效:
 
explain plan for
SELECT COUNT(a.product_no) AS cnt, a.area_code, b.current_name
  FROM t_info_customer     a,
       t_info_account_card c,
       T_DICT_AREA_CITY    b,
       t_log_offline_payment_his      txn,
       t_log_online_payment_his txn_on
 where a.area_code = b.current_code
   and a.customer_no = c.customer_no
   and c.binding_flag = '1'
   and c.binding_time <= TO_DATE('20121130', 'yyyyMMdd')
   AND (txn.TXN_CHANNEL IN ('02', '03', '04', '05', '06', '08') --线下
   or txn_on.TXN_CHANNEL IN ('02', '03', '04', '05', '06', '08')   )
   AND a.status <> '9'
   AND a.customer_no LIKE '86%'
   and    ( c.fund_account_no = txn.account_no or c.fund_account_no = txn_on.account_no)
 GROUP BY a.area_code, b.current_name;
SQL> select * from table(dbms_xplan.display);
 
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1203085281
--------------------------------------------------------------------------------
| Id  | Operation                        | Name                      | Rows  | B
--------------------------------------------------------------------------------
|   0 | SELECT STATEMENT                 |                           |  5290 |
|   1 |  HASH GROUP BY                   |                           |  5290 |
|   2 |   CONCATENATION                  |                           |       |
|*  3 |    HASH JOIN                     |                           |    87M|
|*  4 |     TABLE ACCESS FULL            | T_LOG_ONLINE_PAYMENT_HIS  |   113K|
|*  5 |     HASH JOIN                    |                           |    69M|
|*  6 |      TABLE ACCESS FULL           | T_INFO_ACCOUNT_CARD       | 30701 |
|*  7 |      HASH JOIN                   |                           |    69M|
|*  8 |       TABLE ACCESS FULL          | T_INFO_CUSTOMER           | 29860 |
|   9 |       MERGE JOIN CARTESIAN       |                           |   871K|
|  10 |        TABLE ACCESS FULL         | T_LOG_OFFLINE_PAYMENT_HIS |  2329 | 4
|  11 |        BUFFER SORT               |                           |   374 |
|  12 |         TABLE ACCESS FULL        | T_DICT_AREA_CITY          |   374 |
|  13 |    TABLE ACCESS BY INDEX ROWID   | T_DICT_AREA_CITY          |     1 |
|  14 |     NESTED LOOPS                 |                           |     1 |
 
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
|  15 |      NESTED LOOPS                |                           |     1 |
|  16 |       NESTED LOOPS               |                           |     1 |
|* 17 |        HASH JOIN                 |                           |     1 |
|  18 |         TABLE ACCESS FULL        | T_LOG_OFFLINE_PAYMENT_HIS |  2329 | 4
|* 19 |         TABLE ACCESS FULL        | T_INFO_ACCOUNT_CARD       | 30701 |
|* 20 |        TABLE ACCESS FULL         | T_LOG_ONLINE_PAYMENT_HIS  |  5686 |
|* 21 |       TABLE ACCESS BY INDEX ROWID| T_INFO_CUSTOMER           |     1 |
|* 22 |        INDEX UNIQUE SCAN         | PK_T_INFO_CUSTOMER        |     1 |
|* 23 |      INDEX RANGE SCAN            | PK_T_DICT_AREA_CITY       |     1 |
|* 24 |    HASH JOIN                     |                           |   159K|
|* 25 |     TABLE ACCESS FULL            | T_LOG_ONLINE_PAYMENT_HIS  |   253K|
|* 26 |     HASH JOIN                    |                           | 89427 |
|* 27 |      TABLE ACCESS FULL           | T_INFO_ACCOUNT_CARD       | 30701 |
|* 28 |      HASH JOIN                   |                           | 89429 |
|  29 |       MERGE JOIN CARTESIAN       |                           |  1122 | 4
|* 30 |        TABLE ACCESS FULL         | T_LOG_OFFLINE_PAYMENT_HIS |     3 |
|  31 |        BUFFER SORT               |                           |   374 |
|  32 |         TABLE ACCESS FULL        | T_DICT_AREA_CITY          |   374 |
|* 33 |       TABLE ACCESS FULL          | T_INFO_CUSTOMER           | 29860 |
|  34 |    TABLE ACCESS BY INDEX ROWID   | T_DICT_AREA_CITY          |     1 |
|  35 |     NESTED LOOPS                 |                           |     1 |
 
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
|  36 |      NESTED LOOPS                |                           |     1 |
|  37 |       NESTED LOOPS               |                           |     1 |
|* 38 |        HASH JOIN                 |                           |     1 |
|* 39 |         TABLE ACCESS FULL        | T_LOG_OFFLINE_PAYMENT_HIS |     3 |
|* 40 |         TABLE ACCESS FULL        | T_INFO_ACCOUNT_CARD       | 30701 |
|* 41 |        TABLE ACCESS FULL         | T_LOG_ONLINE_PAYMENT_HIS  | 12655 |
|* 42 |       TABLE ACCESS BY INDEX ROWID| T_INFO_CUSTOMER           |     1 |
|* 43 |        INDEX UNIQUE SCAN         | PK_T_INFO_CUSTOMER        |     1 |
|* 44 |      INDEX RANGE SCAN            | PK_T_DICT_AREA_CITY       |     1 |
--------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
   3 - access("C"."FUND_ACCOUNT_NO"="TXN_ON"."ACCOUNT_NO")
   4 - filter("TXN_ON"."TXN_CHANNEL"='02' OR "TXN_ON"."TXN_CHANNEL"='03' OR "TXN
              "TXN_ON"."TXN_CHANNEL"='05' OR "TXN_ON"."TXN_CHANNEL"='06' OR "TXN
   5 - access("A"."CUSTOMER_NO"="C"."CUSTOMER_NO")
   6 - filter("C"."CUSTOMER_NO" LIKE '86%' AND "C"."BINDING_FLAG"='1' AND "C"."B
              2012-11-30 00:00:00')
   7 - access("A"."AREA_CODE"="B"."CURRENT_CODE")
 
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
   8 - filter("A"."CUSTOMER_NO" LIKE '86%' AND "A"."STATUS"<>'9')
  17 - access("C"."FUND_ACCOUNT_NO"="TXN"."ACCOUNT_NO")
  19 - filter("C"."CUSTOMER_NO" LIKE '86%' AND "C"."BINDING_FLAG"='1' AND "C"."B
              2012-11-30 00:00:00')
  20 - filter(("TXN_ON"."TXN_CHANNEL"='02' OR "TXN_ON"."TXN_CHANNEL"='03' OR "TX
              "TXN_ON"."TXN_CHANNEL"='05' OR "TXN_ON"."TXN_CHANNEL"='06' OR "TXN
              LNNVL("C"."FUND_ACCOUNT_NO"="TXN_ON"."ACCOUNT_NO"))
  21 - filter("A"."STATUS"<>'9')
  22 - access("A"."CUSTOMER_NO"="C"."CUSTOMER_NO")
       filter("A"."CUSTOMER_NO" LIKE '86%')
  23 - access("A"."AREA_CODE"="B"."CURRENT_CODE")
  24 - access("C"."FUND_ACCOUNT_NO"="TXN_ON"."ACCOUNT_NO")
  25 - filter(LNNVL("TXN_ON"."TXN_CHANNEL"='02') AND LNNVL("TXN_ON"."TXN_CHANNEL
              LNNVL("TXN_ON"."TXN_CHANNEL"='04') AND LNNVL("TXN_ON"."TXN_CHANNEL
              LNNVL("TXN_ON"."TXN_CHANNEL"='06') AND LNNVL("TXN_ON"."TXN_CHANNEL
  26 - access("A"."CUSTOMER_NO"="C"."CUSTOMER_NO")
  27 - filter("C"."CUSTOMER_NO" LIKE '86%' AND "C"."BINDING_FLAG"='1' AND "C"."B
              2012-11-30 00:00:00')
  28 - access("A"."AREA_CODE"="B"."CURRENT_CODE")
  30 - filter("TXN"."TXN_CHANNEL"='02' OR "TXN"."TXN_CHANNEL"='03' OR "TXN"."TXN
              "TXN"."TXN_CHANNEL"='05' OR "TXN"."TXN_CHANNEL"='06' OR "TXN"."TXN
 
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
  33 - filter("A"."CUSTOMER_NO" LIKE '86%' AND "A"."STATUS"<>'9')
  38 - access("C"."FUND_ACCOUNT_NO"="TXN"."ACCOUNT_NO")
  39 - filter("TXN"."TXN_CHANNEL"='02' OR "TXN"."TXN_CHANNEL"='03' OR "TXN"."TXN
              "TXN"."TXN_CHANNEL"='05' OR "TXN"."TXN_CHANNEL"='06' OR "TXN"."TXN
  40 - filter("C"."CUSTOMER_NO" LIKE '86%' AND "C"."BINDING_FLAG"='1' AND "C"."B
              2012-11-30 00:00:00')
  41 - filter(LNNVL("C"."FUND_ACCOUNT_NO"="TXN_ON"."ACCOUNT_NO") AND LNNVL("TXN_
              LNNVL("TXN_ON"."TXN_CHANNEL"='03') AND LNNVL("TXN_ON"."TXN_CHANNEL
              LNNVL("TXN_ON"."TXN_CHANNEL"='05') AND LNNVL("TXN_ON"."TXN_CHANNEL
              LNNVL("TXN_ON"."TXN_CHANNEL"='08'))
  42 - filter("A"."STATUS"<>'9')
  43 - access("A"."CUSTOMER_NO"="C"."CUSTOMER_NO")
       filter("A"."CUSTOMER_NO" LIKE '86%')
  44 - access("A"."AREA_CODE"="B"."CURRENT_CODE")
 
97 rows selected

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/27573546/viewspace-750974/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/27573546/viewspace-750974/

你可能感兴趣的:(sql效率优化:用union all代替or)