巧用rowid簡化sql查詢(r2筆記47天)
生產系統中有一條sql語句,目前執行的時間有點長了,而且看起來有些臃腫,客戶問能不能改進一下。得到的sql語句如下:
SELECT COUNT(1)
FROM (
SELECT /*+ leading (payment_temp_table payment PAYMENT_DETAILS account memo) use_nl (payment_temp_table ar1_payment PAYMENT_DETAILS account memo) index (payment payment_pk) index (PAYMENT_DETAILS PAYMENT_DETAILS_pk) */
PAYMENT_DETAILS.PAYMENT_ID,
PAYMENT.CREDIT_ID,
PAYMENT.ACCOUNT_ID,
PAYMENT.AMOUNT,
PAYMENT.ORIGINAL_AMOUNT,
PAYMENT.ORIGINAL_CONVERTED_AMOUNT,
PAYMENT_DETAILS.PAYMENT_METHOD,
PAYMENT_DETAILS.DEPOSIT_DATE,
PAYMENT_DETAILS.CURRENCY,
PAYMENT_DETAILS.CHECK_NO,
PAYMENT_DETAILS.CREDIT_CARD_NUMBER,
PAYMENT_DETAILS.BANK_CODE,
PAYMENT_DETAILS.BANK_ACCOUNT_NUMBER,
ACCOUNT.ACCOUNT_TIMESTAMP,
ACCOUNT.CURRENCY,
PAYMENT_DETAILS.PAYMENT_SUB_METHOD,
ACCOUNT.BE,
MEMO.MEMO_TEXT,
PAYMENT_DETAILS.PERIOD_KEY,
PAYMENT_DETAILS.PARTITION_ID,
PAYMENT_DETAILS.AMOUNT,
PAYMENT.ACTIVITY_DATE,
PAYMENT_DETAILS.ACCOUNT_ID,
PAYMENT.L9_RT_ID,
PAYMENT_DETAILS.L9_CONV_INV_NUMBER,
PAYMENT_DETAILS.CC_EXPIRY_DATE,
PAYMENT_DETAILS.DIRECT_DEBIT_VOUCHER,
PAYMENT_DETAILS.PAYMENT_SOURCE_TYPE,
PAYMENT.L9_WHT_AMT,
PAYMENT.L9_WHT_CERT_NO,
PAYMENT.L9_WHT_RATE,
PAYMENT.L9_VAT_AMOUNT,
PAYMENT.L9_PRINT_RT_IND,
PAYMENT.L9_USER_ID,
PAYMENT.L9_RT_EXTRACT_IND,
PAYMENT.L9_ECA_REASON_CODE,
PAYMENT.L9_RT_GENERATE_MODE,
PAYMENT_DETAILS.L9_VAT_AMOUNT,
PAYMENT.BILL_SEQ_NO,
PAYMENT.ACTIVITY_INDICATOR,
ACCOUNT.L9_COMPANY_CODE,
PAYMENT_DETAILS.PAYMENT_SOURCE_ID,
PAYMENT.OPERATOR_ID
FROM PAYMENT_DETAILS,
PAYMENT,
ACCOUNT,
MEMO,
(SELECT row_number,
PAYMENT_ID,
CREDIT_ID,
ACCOUNT_ID,
PD_PARTITION_ID,
PD_PERIOD_KEY,
PT_PARTITION_ID,
PT_PERIOD_KEY
FROM (SELECT rownum as row_number,
PAYMENT_ID,
CREDIT_ID,
ACCOUNT_ID,
PD_PARTITION_ID,
PD_PERIOD_KEY,
PT_PARTITION_ID,
PT_PERIOD_KEY
FROM (SELECT /*+ leading (pt a pd) use_nl (pt a pd) index (pd PAYMENT_DETAILS_pk) */
DISTINCT PD.PAYMENT_ID,
PT.CREDIT_ID,
A.ACCOUNT_ID,
pd.partition_id pd_partition_id,
pd.period_key pd_period_key,
pt.partition_id pt_partition_id,
pt.period_key pt_period_key
FROM PAYMENT_DETAILS PD,
PAYMENT PT,
ACCOUNT A
WHERE PT.PARTITION_ID = A.PARTITION_ID
AND PT.ACCOUNT_ID = A.ACCOUNT_ID
AND PD.PARTITION_ID = PT.PYMDT_PARTITION_ID
AND PD.PERIOD_KEY = PT.PYMDT_PERIOD_KEY
AND PD.PAYMENT_ID = PT.PAYMENT_ID
AND (a.be IN
(SELECT child_be_id
FROM gn1_boh_relation
START WITH parent_be_id = NVL(0, 0)
CONNECT BY PRIOR
child_be_id = parent_be_id) OR
a.be = NVL(0, 0))
AND ROWNUM <= 2000
ORDER BY pt.ACTIVITY_DATE DESC,
pt.CREDIT_ID DESC))
WHERE row_number BETWEEN 1 AND 100) PAYMENT_TEMP_TABLE
WHERE PAYMENT_DETAILS.PAYMENT_ID = PAYMENT_TEMP_TABLE.PAYMENT_ID
AND PAYMENT_DETAILS.partition_id =
payment_temp_table.pd_partition_id
AND PAYMENT_DETAILS.period_key =
payment_temp_table.pd_period_key
AND ACCOUNT.account_id = payment_temp_table.account_id
AND PAYMENT.credit_id = payment_temp_table.credit_id
AND PAYMENT.partition_id = payment_temp_table.pt_partition_id
AND PAYMENT.period_key = payment_temp_table.pt_period_key
AND MEMO.memo_id(+) = PAYMENT_DETAILS.memo_id);
INDEX_NAME TABLESPACE INDEX_TYPE UNIQUENES PAR COLUMN_LIST TABLE_TYPE STATUS NUM_ROWS LAST_ANAL G ------------------------------ ---------- ---------- --------- --- ------------------------------ ---------- ------ ---------- --------- - PAYMENT_DETAILS_PK NORMAL UNIQUE YES PAYMENT_ID,PARTITION_ID,PERIOD_KEY TABLE N/A 6718838 16-JUL-14 N ------------------------------ ---------- ---------- --------- --- ------------------------------ ---------- ------ ---------- --------- - PAYMENT_PK NORMAL UNIQUE YES CREDIT_ID,PARTITION_ID,PERIOD_KEY TABLE N/A 6914026 16-JUL-14 N
可以看到基本索引在查詢條件中能用都用到了,而且使用了hint,根據sql內容來看,這個查詢只是需要得到對應的記錄條數而已,是在原有的查詢語句的基礎上直接加了select count(1) from xxx改進的來的。 所以對於這類查詢,就需要擺脫思想的束縛,可以最大程度上簡化sql,達到同樣的效果。 改進後的sql語句如下:
SELECT COUNT(1) FROM ( SELECT PAYMENT_DETAILS.rowid, PAYMENT.rowid, ACCOUNT.rowid , --AR1_MEMO.rowid FROM PAYMENT_DETAILS, PAYMENT, ACCOUNT, -- AR1_MEMO, (SELECT pdrowid, ptrowid, arowid FROM (SELECT rownum as row_number, pdrowid, ptrowid, arowid FROM (SELECT /*+ leading (pt a pd) */ DISTINCT PD.rowid pdrowid, PT.rowid ptrowid, A.rowid arowid FROM PAYMENT_DETAILS PD, PAYMENT PT, ACCOUNT A WHERE PT.PARTITION_ID = A.PARTITION_ID AND PT.ACCOUNT_ID = A.ACCOUNT_ID AND PD.PARTITION_ID = PT.PYMDT_PARTITION_ID AND PD.PERIOD_KEY = PT.PYMDT_PERIOD_KEY AND PD.PAYMENT_ID = PT.PAYMENT_ID AND (a.be IN (SELECT child_be_id FROM gn1_boh_relation START WITH parent_be_id = NVL(0, 0) CONNECT BY PRIOR child_be_id = parent_be_id) OR a.be = NVL(0, 0)) AND ROWNUM <= 2147483627 ORDER BY pt.ACTIVITY_DATE DESC, pt.CREDIT_ID DESC)) WHERE row_number BETWEEN 1 AND 9223372036854775807) PAYMENT_TEMP_TABLE WHERE PAYMENT_DETAILS.rowid = PAYMENT_TEMP_TABLE.pdrowid AND ACCOUNT.rowid = payment_temp_table.arowid AND PAYMENT.rowid = payment_temp_table.ptrowid AND MEMO.memo_id(+) = PAYMENT_DETAILS.memo_id
直接通過rowid來做關聯,因為不需要輸出所有的資料,只要輸出列出含有主鍵列,就可以考慮使用rowid來代替。 sql語句極大的簡化了,不過還沒有完,還可以考慮做點什麼。因為memo這個表比較大,沒有走主鍵,查詢會走全表掃描,耗費不少時間,但是結果集中貌似也不是很需要,因為結果集只考慮最終的返回資料條數,可以考慮是否能夠從查詢中去掉這個表。 簡單驗證一下,模擬這種類似的外連線,看看結果集是否會有影響。
SQL>
insert into test_full values(1);
insert into test_full values(2);
insert into test_full values(3);
insert into test_partial values(1,'a');
insert into test_partial values(2,'b');
1 row created.
SQL>
1 row created.
SQL>
1 row created.
SQL>
1 row created.
SQL>
1 row created.
SQL>
SQL> select test_full.id, test_partial.name from test_full,test_partial
2 where test_partial.id(+) = test_full.id
3 /
ID NAME
---------- ------------------------------
1 a
2 b
3
通過上面的簡單測試,可以說明,如果表test_full中含有的記錄,通過和test_partial做外連線,也是全量輸出test_full的值。 這樣,可以考慮把memo表去除,整理後的sql語句簡化為:
SELECT COUNT(1)
FROM (
SELECT
PAYMENT_DETAILS.rowid,
PAYMENT.rowid,
ACCOUNT.rowid
FROM PAYMENT_DETAILS,
PAYMENT,
ACCOUNT,
(SELECT pdrowid, ptrowid, arowid
FROM (SELECT rownum as row_number, pdrowid, ptrowid, arowid
FROM (SELECT /*+ leading (pt a pd) */
DISTINCT PD.rowid pdrowid,
PT.rowid ptrowid,
A.rowid arowid
FROM PAYMENT_DETAILS PD,
PAYMENT PT,
ACCOUNT A
WHERE PT.PARTITION_ID = A.PARTITION_ID
AND PT.ACCOUNT_ID = A.ACCOUNT_ID
AND PD.PARTITION_ID = PT.PYMDT_PARTITION_ID
AND PD.PERIOD_KEY = PT.PYMDT_PERIOD_KEY
AND PD.PAYMENT_ID = PT.PAYMENT_ID
AND (a.be IN
(SELECT child_be_id
FROM gn1_boh_relation
START WITH parent_be_id = NVL(0, 0)
CONNECT BY PRIOR
child_be_id = parent_be_id) OR
a.be = NVL(0, 0))
AND ROWNUM <= 2147483627
ORDER BY pt.ACTIVITY_DATE DESC,
pt.CREDIT_ID DESC))
WHERE row_number BETWEEN 1 AND 9223372036854775807) PAYMENT_TEMP_TABLE
WHERE PAYMENT_DETAILS.rowid = PAYMENT_TEMP_TABLE.pdrowid
AND ACCOUNT.rowid = payment_temp_table.arowid
AND PAYMENT.rowid = payment_temp_table.ptrowid
對於一些Hint也做了刪減,保證不必要的資源消耗。 總之,效能sql,對於sql的簡化也是一種考驗,如果能夠最大程度的簡化,也是sql調優的進步。