sql优化问题,在线等,我想把重复的查询条件分出来,只是有这思路。。。

[复制链接]
查看11 | 回复2 | 2021-1-27 06:31:05 | 显示全部楼层 |阅读模式
select(selectshortnamefromldcomwherecomcode=v.comcode)||'____',
(selectcodename
fromldcode
wherecodetype='salechnl'
andcode=v.code),
sum(cbcount),
nvl(sum(cbsumprem),0),
sum(wtcount),
nvl(sum(wtsumprem),0),
sum(ctcount),
nvl(sum(ctsumprem),0),
sum(xtcount),
nvl(sum(xtsumprem),0)
from(selectk.comcodecomcode,
k.shortname,
d.codeCODE,
d.codename,
(selectcount(distinctl.contno)
fromv_temp_bqcontl
wherel.optype='CB'
andl.managecomlikek.comcode||'%'
andopdatebetweento_date('2017-01-01','yyyy-mm-dd')andto_date('2017-07-27','yyyy-mm-dd')
andl.salechnl=D.CODE)cbcount,
(selectSUM(l.money)
fromv_temp_bqcontl
wherel.optype='CB'
andl.managecomlikek.comcode||'%'
andopdatebetweento_date('2017-01-01','yyyy-mm-dd')andto_date('2017-07-27','yyyy-mm-dd')
andl.salechnl=D.CODE)cbsumprem,
(selectcount(DISTINCTl.contno)
fromv_temp_bqcontl
wherel.optype='WT'
andl.managecomlikek.comcode||'%'
andopdatebetweento_date('2017-01-01','yyyy-mm-dd')andto_date('2017-07-27','yyyy-mm-dd')
andl.salechnl=D.CODE)wtcount,
-1*(selectsum(l.money)
fromv_temp_bqcontl
wherel.optype='WT'
andl.managecomlikek.comcode||'%'
andopdatebetweento_date('2017-01-01','yyyy-mm-dd')andto_date('2017-07-27','yyyy-mm-dd')
andl.salechnl=D.CODE)wtsumprem,
(selectcount(DISTINCTl.contno)
fromv_temp_bqcontl
wherel.optype='CT'
andl.managecomlikek.comcode||'%'
andopdatebetweento_date('2017-01-01','yyyy-mm-dd')andto_date('2017-07-27','yyyy-mm-dd')
andl.salechnl=D.CODE)ctcount,
-1*(selectsum(l.money)
fromv_temp_bqcontl
wherel.optype='CT'
andl.managecomlikek.comcode||'%'
andopdatebetweento_date('2017-01-01','yyyy-mm-dd')andto_date('2017-07-27','yyyy-mm-dd')
andl.salechnl=D.CODE)ctsumprem,
(selectCOUNT(DISTINCTl.contno)
fromv_temp_bqcontl
wherel.optypeIN('XT','YT')
andl.managecomlikek.comcode||'%'
andopdatebetweento_date('2017-01-01','yyyy-mm-dd')andto_date('2017-07-27','yyyy-mm-dd')
andl.salechnl=D.CODE)xtcount,
-1*(selectsum(l.money)
fromv_temp_bqcontl
wherel.optypeIN('XT','YT')
andl.managecomlikek.comcode||'%'
andopdatebetweento_date('2017-01-01','yyyy-mm-dd')andto_date('2017-07-27','yyyy-mm-dd')
andl.salechnl=D.CODE)xtsumprem
fromldcomk,ldcoded
whered.codetype='salechnl'
andk.comcodenotlike'8600%'
andk.comcodelike'86030201%'
andd.code='03'
andLENGTH(k.comcode)=8
orderbyk.comcode,d.code)v
groupbyROLLUP(v.comcode,v.code)

分 -->
回复

使用道具 举报

千问 | 2021-1-27 06:31:05 | 显示全部楼层
首先:(最内层)
selectl.optype,l.managecom,l.salechnl,COUNT(DISTINCTl.contno)cc,sum(l.money)mm
fromv_temp_bqcontl
whereopdatebetweento_date('2017-01-01','yyyy-mm-dd')andto_date('2017-07-27','yyyy-mm-dd')
groupbyl.optype,l.managecom,l.salechnl
第二层:将上述中间表与ldcomk,ldcode关联按条件取值;
第三层:按v.comcode,v.code分组得最终结果
另:中间层中尽量不要orderby,这会增加额外开销
回复

使用道具 举报

千问 | 2021-1-27 06:31:05 | 显示全部楼层
我滴个乖乖你这查询列中出现了多少个子查询,
把查询列中的子查询使用关联查询按条件取值,
把你子查询上的orderby去掉,节省开销,而且还是没意义的开销
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行