一條sql的優化過程
點選(此處)摺疊或開啟
-
selectzsxm_dm,
-
zspm_dm,
-
wspzzl_dm,
-
wspzzg,
-
wspzhm,
-
zsuuid,
-
sum(nvl(fpdksk,0))asfpdksk,
-
sum(nvl(jsyj,0))asjsyj,
-
sl,
-
hy_dm,
-
skssqq,
-
skssqz,
-
sksx_dm,
-
tfrq,
-
djxh,
-
yzpzzl_dm
-
from(selectjks.djxh,
-
yz.yzpzxhaszsuuid,
-
yz.yzpzzl_dm,
-
jks.pzzl_dmaswspzzl_dm,
-
jks.pzzg_dmaswspzzg,
-
nvl(jks.pzhm,jks.dzsphm)aswspzhm,
-
jks.zsxm_dm,
-
jks.zspm_dm,
-
jks.sl_1assl,
-
jks.jsyj,
-
jks.sjjeasfpdksk,
-
jks.skssqq,
-
jks.skssqz,
-
jks.hy_dm,
-
jks.kjrqastfrq,
-
jks.sksx_dm
-
fromhx_zs.zs_jks jks,hx_zs.zs_yjsf yz
-
wherejks.tzlx_dmin(\'1\',\'4\')
-
andjks.sksx_dmnotlike\'02%\'
-
and(jks.kjdjxh=\'10106001062640079\'orjks.djxh=\'10106001062640079\')
-
and(1=0orjks.skssqq>=to_date(\'2014-11-01\',\'yyyy-mm-dd\'))
-
and(1=0orjks.skssqz<
-
to_date(to_char(\'2014-12-31\',\'yyyy-mm-dd\'),\'yyyy-mm-dd\')+1)
-
and(1=0orjks.kjrq>=to_date(\'2014-02-01\',\'yyyy-mm-dd\'))
-
and(1=0orjks.kjrq<
-
to_date(to_char(\'2015-03-31\',\'yyyy-mm-dd\'),\'yyyy-mm-dd\')+1)
-
andjks.sjrq_1isnotnull
-
andjks.zsuuid=yz.zsuuid
-
andyz.tzlx_dmin(\'1\',\'4\')
-
andyz.skcllx_dm=\'1\'
-
andyz.skzl_dm<>\'20\'
-
unionall
-
selectwsz.djxh,
-
yz.yzpzxhaszsuuid,
-
yz.yzpzzl_dm,
-
wsz.pzzl_dmaswspzzl_dm,
-
wsz.pzzg_dmaswspzzg,
-
wsz.pzhmaswspzhm,
-
wsz.zsxm_dm,
-
wsz.zspm_dm,
-
wsz.sl_1assl,
-
wsz.jsyj,
-
wsz.sjjeasfpdksk,
-
wsz.skssqq,
-
wsz.skssqz,
-
wsz.hy_dm,
-
wsz.kjrqastfrq,
-
wsz.sksx_dm
-
fromhx_zs.zs_wsz wsz,hx_zs.zs_yjsf yz
-
wherewsz.tzlx_dmin(\'1\',\'4\')
-
andwsz.sksx_dmnotlike\'02%\'
-
and(wsz.djxh=\'10106001062640079\'orwsz.kjdjxh=\'10106001062640079\')
-
and(1=0orwsz.skssqq>=to_date(\'2014-11-01\',\'yyyy-mm-dd\'))
-
and(1=0orwsz.skssqz<
-
to_date(to_char(\'2014-12-31\',\'yyyy-mm-dd\'),\'yyyy-mm-dd\')+1)
-
and(1=0orwsz.kjrq>=to_date(\'2014-02-01\',\'yyyy-mm-dd\'))
-
and(1=0orwsz.kjrq<
-
to_date(to_char(\'2015-03-31\',\'yyyy-mm-dd\'),\'yyyy-mm-dd\')+1)
-
andwsz.zsuuid=yz.zsuuid
-
andyz.tzlx_dmin(\'1\',\'4\')
-
andyz.skcllx_dm=\'1\'
-
andyz.skzl_dm<>\'20\')b
-
groupbyzsxm_dm,
-
zspm_dm,
-
wspzzl_dm,
-
wspzzg,
-
wspzhm,
-
zsuuid,
-
sl,
-
hy_dm,
-
skssqq,
-
skssqz,
-
sksx_dm,
-
tfrq,
-
djxh,
- yzpzzl_dm;
初步分析:
1.前天資料做過收集統計,而收集統計之前未發現該sql超時問題。
2.此sql本身耗費確實較高,需要進一步優化。
3.由執行計劃初步可看耗費較高的地方是在hx_zs.zs_wsz上走索引IDX_ZS_WSZS_SKFJ_KJDJXH_SSQQ時採取了INDEX SKIP SCAN ,COST達到了2064。
4.HX_ZS.ZS_JKS及HX_ZS.ZS_WSZ這兩張表是分割槽表,而SQL中卻未新增分割槽關鍵條件,造成的對所有分割槽的掃描。
5.另外,以上sql的結構模式是select (jks,yjsf union all wsz,yjsf) where gruop by ,兩次對同一個表進行掃描。
優化步驟:
1.收集統計。針對sql中涉及的表做了收集統計!
2. jks,wsz加上skssswjg條件,因為這兩個表是以skssswjg進行分割槽。
3. sql結構調整為select (jks union all wsz),yjsf where gruop by 結構!
4. 試著加上kjdjxh和skssqq的兩列索引看看避免skip scan後的效果
執行以上步驟之後sql的執行計劃 cost降到了7,cpu耗費增到1千7百萬左右,時間為1
附件是修改後sql等!
修改後sql:
點選(此處)摺疊或開啟
-
select b.zsxm_dm,
-
b.zspm_dm,
-
wspzzl_dm,
-
wspzzg,
-
wspzhm,
-
b.zsuuid,
-
sum(nvl(b.fpdksk, 0)) as fpdksk,
-
sum(nvl(b.jsyj, 0)) as jsyj,
-
sl,
-
b.hy_dm,
-
b.skssqq,
-
b.skssqz,
-
b.sksx_dm,
-
tfrq,
-
b.djxh,
-
yz.yzpzzl_dm
-
from ((select jks.djxh,
-
jks.pzzl_dm as wspzzl_dm,
-
jks.pzzg_dm as wspzzg,
-
nvl(jks.pzhm, jks.dzsphm) as wspzhm,
-
jks.zsxm_dm,
-
jks.zspm_dm,
-
jks.sl_1 as sl,
-
jks.jsyj,
-
jks.sjje as fpdksk,
-
jks.skssqq,
-
jks.skssqz,
-
jks.hy_dm,
-
jks.kjrq as tfrq,
-
jks.sksx_dm,
-
jks.zsuuid
-
from hx_zs.zs_jks jks
-
where jks.tzlx_dm in (\'1\', \'4\')
-
and jks.sksx_dm not like \'02%\'
-
and jks.skssswjg_dm=\'24401030000\'
-
and (jks.kjdjxh = \'1016001062640079\' or jks.djxh = \'1016001062640079\')
-
and (1 = 0 or jks.skssqq >= to_date(\'2014-11-01\', \'yyyy-mm-dd\'))
-
and (1 = 0 or jks.skssqz <
-
to_date(to_char(\'2014-12-31\', \'yyyy-mm-dd\'), \'yyyy-mm-dd\') + 1)
-
and (1 = 0 or jks.kjrq >= to_date(\'2014-02-01\',\'yyyy-mm-dd\'))
-
and (1 = 0 or jks.kjrq <
-
to_date(to_char(\'2015-03-31\', \'yyyy-mm-dd\'), \'yyyy-mm-dd\') + 1)
-
and jks.sjrq_1 is not null
-
union all
-
select wsz.djxh,
-
wsz.pzzl_dm as wspzzl_dm,
-
wsz.pzzg_dm as wspzzg,
-
wsz.pzhm as wspzhm,
-
wsz.zsxm_dm,
-
wsz.zspm_dm,
-
wsz.sl_1 as sl,
-
wsz.jsyj,
-
wsz.sjje as fpdksk,
-
wsz.skssqq,
-
wsz.skssqz,
-
wsz.hy_dm,
-
wsz.kjrq as tfrq,
-
wsz.sksx_dm,
-
wsz.zsuuid
-
from hx_zs.zs_wsz wsz
-
where wsz.tzlx_dm in (\'1\', \'4\')
-
and wsz.sksx_dm not like \'02%\'
-
and wsz.skssswjg_dm=\'24401030000\'
-
and (wsz.djxh = \'1016001062640079\' or wsz.kjdjxh = \'1016001062640079\')
-
and (1 = 0 or wsz.skssqq >= to_date(\'2014-11-01\',\'yyyy-mm-dd\'))
-
and (1 = 0 or wsz.skssqz <
-
to_date(to_char(\'2014-12-31\', \'yyyy-mm-dd\'), \'yyyy-mm-dd\') + 1)
-
and (1 = 0 or wsz.kjrq >= to_date(\'2014-02-01\',\'yyyy-mm-dd\'))
-
and (1 = 0 or wsz.kjrq <
-
to_date(to_char(\'2015-03-31\', \'yyyy-mm-dd\'), \'yyyy-mm-dd\') + 1)) ) b , hx_zs.zs_yjsf yz
-
where b.zsuuid = yz.zsuuid
-
and yz.tzlx_dm in (\'1\', \'4\')
-
and yz.skcllx_dm = \'1\'
-
and yz.skzl_dm <> \'20\'
-
group by b.zsxm_dm,
-
b.zspm_dm,
-
wspzzl_dm,
-
wspzzg,
-
wspzhm,
-
b.zsuuid,
-
sl,
-
b.hy_dm,
-
b.skssqq,
-
b.skssqz,
-
b.sksx_dm,
-
tfrq,
-
b.djxh,
- yzpzzl_dm;
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29863023/viewspace-1472438/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- 一條SQL語句的優化過程SQL優化
- 一條Sql的執行過程SQL
- 通過新增條件優化SQL優化SQL
- 一條sql語句的優化SQL優化
- SQL優化--用各種hints優化一條SQLSQL優化
- pl/sql儲存過程優化一例SQL儲存過程優化
- 一條sql語句的執行過程SQL
- 一條 sql 的執行過程詳解SQL
- MySQL 中一條 sql 的執行過程MySql
- ORACLE SQL過濾條件是IS NULL or !=的優化OracleSQLNull優化
- SQL優化34條SQL優化
- 從一條問題SQL優化看SQL TransformationSQL優化ORM
- 增加複合索引優化SQL的簡單過程索引優化SQL
- 編寫和優化SQL Server的儲存過程優化SQLServer儲存過程
- 記一次SQL調優過程SQL
- 通過_optimizer_rownum_pred_based_fkr優化一條sql語句優化SQL
- SQL優化經驗總結34條(一)SQL優化
- 一條大sql的調優SQL
- 優化同事發過來的一個sql優化SQL
- [20170104]一條sql優化.txtSQL優化
- 對一條基於分割槽的簡單SQL的優化SQL優化
- 一條sql語句優化不出來, 哭了一鼻子SQL優化
- MySQL系列之一條SQL查詢語句的執行過程MySql
- 詳述一條SQL引發的高CPU故障處理過程SQL
- 一次效能優化調整過程.優化
- 效能優化的過程學習優化
- 一個缺乏索引和統計資訊的優化過程索引優化
- Oracle SQL效能優化的40條軍規OracleSQL優化
- 34條簡單的SQL優化準則SQL優化
- SQL Server儲存過程的優缺點SQLServer儲存過程
- 一個sql的優化SQL優化
- SQL優化(一)SQL優化
- 從一條更新SQL的執行過程窺探InnoDB之REDOLOGSQL
- 記錄一個sql最佳化的全過程.txtSQL
- 分享工作中一次優化程式的過程優化
- 一條select的查詢的過程
- 一條簡單的SQL語句優化-新年新氣象SQL優化
- SQL 優化經驗總結34條SQL優化