update修改為merge(max+decode)
記錄日期: 2014-07-30 14:25:27
------------- 最佳化方法: 減少大表掃描次數採用max+decode方式
原sql語句:
UPDATE RKO_ACCT_STATUS A SET RMB_PAYMENT = (SELECT NVL(SUM(POSTING_AMT), 0) FROM RKOT_ACCT_PMT_PRIOR B WHERE B.ACCT = A.ACCT AND ORG = '242' AND POSTING_DATE BETWEEN A.PRIOR_BILLING_DATE + 1 AND ADD_MONTHS(A.PRIOR_BILLING_DATE, 1) AND TXN_DATE <= (SELECT CASE WHEN USER_DATE_10=0 THEN NULL ELSE TO_DATE(USER_DATE_10, 'yyyyddd') END FROM RKOH_HAPS_AMBS_KD WHERE ACCT = A.ACCT AND ORG = 242 AND BATCH_DATE = TO_CHAR(LAST_DAY(A.PRIOR_BILLING_DATE),'yyyymmdd')) + 0.99999), USD_PAYMENT = (SELECT NVL(SUM(POSTING_AMT), 0) FROM RKOT_ACCT_PMT_PRIOR B WHERE B.ACCT = A.ACCT AND ORG = '241' AND POSTING_DATE BETWEEN A.PRIOR_BILLING_DATE + 1 AND ADD_MONTHS(A.PRIOR_BILLING_DATE, 1) AND TXN_DATE <= (SELECT CASE WHEN USER_DATE_10=0 THEN NULL ELSE TO_DATE(USER_DATE_10, 'yyyyddd') END FROM RKOH_HAPS_AMBS_KD WHERE ACCT = A.ACCT AND ORG = 241 AND BATCH_DATE = TO_CHAR(LAST_DAY(A.PRIOR_BILLING_DATE),'yyyymmdd')) + 0.99999) WHERE TO_CHAR(A.PRIOR_BILLING_DATE, 'yyyymm') = :B1;
格式化一下:
UPDATE RKO_ACCT_STATUS A
SET RMB_PAYMENT =
(SELECT NVL(SUM(POSTING_AMT),
0)
FROM RKOT_ACCT_PMT_PRIOR B
WHERE B.ACCT = A.ACCT
AND ORG = '242'
AND POSTING_DATE BETWEEN A.PRIOR_BILLING_DATE + 1 AND
ADD_MONTHS(A.PRIOR_BILLING_DATE,
1)
AND TXN_DATE <=
(SELECT CASE
WHEN USER_DATE_10 = 0 THEN
NULL
ELSE
TO_DATE(USER_DATE_10,
'yyyyddd')
END
FROM RKOH_HAPS_AMBS_KD
WHERE ACCT = A.ACCT
AND ORG = 242
AND BATCH_DATE = TO_CHAR(LAST_DAY(A.PRIOR_BILLING_DATE),
'yyyymmdd')) + 0.99999),
USD_PAYMENT =
(SELECT NVL(SUM(POSTING_AMT),
0)
FROM RKOT_ACCT_PMT_PRIOR B
WHERE B.ACCT = A.ACCT
AND ORG = '241'
AND POSTING_DATE BETWEEN A.PRIOR_BILLING_DATE + 1 AND
ADD_MONTHS(A.PRIOR_BILLING_DATE,
1)
AND TXN_DATE <=
(SELECT CASE
WHEN USER_DATE_10 = 0 THEN
NULL
ELSE
TO_DATE(USER_DATE_10,
'yyyyddd')
END
FROM RKOH_HAPS_AMBS_KD
WHERE ACCT = A.ACCT
AND ORG = 241
AND BATCH_DATE = TO_CHAR(LAST_DAY(A.PRIOR_BILLING_DATE),
'yyyymmdd')) + 0.99999)
WHERE TO_CHAR(A.PRIOR_BILLING_DATE,
'yyyymm') = :B1
;
原sql執行計劃:
------------------------------------- 最佳化
create index ind_RKO_ACCT_date on RKO_ACCT_STATUS(PRIOR_BILLING_DATE) NOLOGGING parallel 20;
alter index ind_RKO_ACCT_date NOPARALLEL;
-------------------------------------------------------------------------------------------最佳化後sql
MERGE INTO RKO_ACCT_STATUS t
USING (SELECT /*+USE_HASH(a,b,c)*/ a.rowid rowids,
sum(DECODE(b.org,
242,
NVL(b.POSTING_AMT,
0))) counts,
sum(DECODE(b.org,
241,
NVL(b.POSTING_AMT,
0))) counts1
FROM RKOT_ACCT_PMT_PRIOR B,
RKOH_HAPS_AMBS_KD c,
RKO_ACCT_STATUS a
WHERE B.ACCT = A.ACCT
AND c.ACCT = A.ACCT
AND b.ORG = c.ORG
AND b.ORG IN (242,
241)
AND (b.POSTING_DATE BETWEEN A.PRIOR_BILLING_DATE + 1 AND
ADD_MONTHS(A.PRIOR_BILLING_DATE,
1))
AND (c.BATCH_DATE =
(TO_CHAR(LAST_DAY(A.PRIOR_BILLING_DATE),
'yyyymmdd')) + 0.99999)
AND b.TXN_DATE <= TO_DATE(USER_DATE_10,
'yyyyddd')
AND A.PRIOR_BILLING_DATE BETWEEN
to_date('2014-04-01',
'YYYY-MM-dd') AND
to_date('2014-05-31',
'YYYY-MM-dd')
GROUP BY a.rowid) t1
ON (t.rowid = t1.rowids)
WHEN MATCHED THEN
UPDATE
SET t.RMB_PAYMENT = t1.counts,
t.USD_PAYMENT = t1.counts1
;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26736162/viewspace-1244055/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- sql server merge 做insert和updateSQLServer
- MERGE新特性(UPDATE WHERE,DELETE WHERE,INSERT WHERE)delete
- Hive學習筆記 ---- 支援Update和Delete以及MergeHive筆記delete
- mysql實現merge功能之DUPLICATE key UPDATE語法MySql
- ORACLE sql merge into update where條件位置與效能消耗OracleSQL
- SQL Server的Merge —— 一步實現 insert,update,deleteSQLServerdelete
- Sqlserver、oracle中Merge的使用方法,一個merge語句搞定多個Insert,Update,Delete操作SQLServerOracledelete
- Merge into: Oracle中用一條SQL語句直接進行Insert/Update的操作(R1)OracleSQL
- sudo rosdep init and rosdep update Error 手動新增+修改域名解決方法ROSError
- Windows Update與Microsoft UpdateWindowsROS
- for update和for update nowaitAI
- FOR UPDATE NOWAIT和 FOR UPDATEAI
- GreatSQL 中為什麼 Update 不會被鎖等待SQL
- MySQL中為什麼要使用索引合併(Index Merge)?MySql索引Index
- MySQL 中 update 修改資料與原資料相同會再次執行嗎?MySql
- sql merge intoSQL
- mysql mergeMySql
- Merge Or Rebase
- git mergeGit
- 使用 HTTP PUT, PATCH 以及 MERGE 請求消費 SAP ABAP OData 服務修改操作HTTP
- MYSQL merge union merge sort_union 的不同MySql
- 為oracle 10.1.0.3 打 Critical Patch Update for April 2005Oracle
- for update 和 for update of 有什麼區別
- SQL Server 2008中SQL增強之三:Merge(在一條語句中使用Insert,Update,Delete)SQLServerdelete
- update restartREST
- SQLServer MERGE 用法SQLServer
- jQuery.merge()jQuery
- oracle merge into用法Oracle
- oracle_mergeOracle
- Merge into 學習
- git 中止mergeGit
- mysql update join優化update in查詢效率MySql優化
- Oracle中的for update 和 for update nowaitOracleAI
- [Linux]修改ubuntu dash為bashLinuxUbuntu
- mantis 修改語言為繁體
- 修改 Ubuntu 資料夾為英文Ubuntu
- Git取消合併(merge)、暫存修改(stash)、回退到某個版本(reset)的使用方法Git
- 關於 select ... for update 和 for update nowaitAI