c11*_*ada 0 sql optimization select
我有一个巨大的查询,这些年来逐渐增长,所有的iv都是为了满足增长而增加子程序.我现在的问题是这个查询大约需要3分钟才能运行.有人可以帮我优化这个查询
SELECT ENTRY_1.REP_CODE,CONTACT_1.NAME,
(select sum((d2.total_goods-d2.total_cost)*et2.sign)
from detail d2
join entry e2 on d2.entry_id=e2.entry_id
join entry_type et2 on et2.entry_type=e2.entry_type
join rep r2 on e2.rep_code=r2.rep_code
join contact c2 on r2.contact_id=c2.contact_id
where ((d2.detail_type = 'O' and d2.charged_qty<d2.qty
and d2.nocharge_qty=0)
or (d2.detail_type = 'N' and e2.entry_type in('SINV','SCRN','DREC','DSRF'))
or (( e2.entry_type = 'SJIN' ) and ( d2.total_goods = 0 )))
and c2.contact_id=contact_1.contact_id
and (e2.year_num=entry_1.year_num and e2.period_num=entry_1.period_num)) MTD_GP,
-- get the month to date sales
(select sum((d2.total_goods)*et2.sign)
from detail d2
join entry e2 on d2.entry_id=e2.entry_id
join entry_type et2 on et2.entry_type=e2.entry_type
join rep r2 on e2.rep_code=r2.rep_code
join contact c2 on r2.contact_id=c2.contact_id
where ((d2.detail_type = 'O' and d2.charged_qty<d2.qty
and d2.nocharge_qty=0)
or (d2.detail_type = 'N' and e2.entry_type in('SINV','SCRN','DREC','DSRF'))
or (( e2.entry_type = 'SJIN' ) and ( d2.total_goods = 0 )))
and c2.contact_id=contact_1.contact_id
and (e2.year_num=entry_1.year_num and e2.period_num=entry_1.period_num)) MTD_SALES,
--
(select sum((d2.total_goods-d2.total_cost)*et2.sign)
from detail d2
join entry e2 on d2.entry_id=e2.entry_id
join entry_type et2 on et2.entry_type=e2.entry_type
join rep r2 on e2.rep_code=r2.rep_code
join contact c2 on r2.contact_id=c2.contact_id
where (d2.detail_type = 'O' and d2.charged_qty<d2.qty and d2.nocharge_qty=0)
and c2.contact_id=contact_1.contact_id
and (e2.year_num=entry_1.year_num and e2.period_num=entry_1.period_num)) ORD_GP,
(select sum((d2.total_goods)*et2.sign)
from detail d2
join entry e2 on d2.entry_id=e2.entry_id
join entry_type et2 on et2.entry_type=e2.entry_type
join rep r2 on e2.rep_code=r2.rep_code
join contact c2 on r2.contact_id=c2.contact_id
where (d2.detail_type = 'O' and d2.charged_qty<d2.qty and d2.nocharge_qty=0)
and c2.contact_id=contact_1.contact_id
and (e2.year_num=entry_1.year_num and e2.period_num=entry_1.period_num)) ORD_SALE,
(select sum((d2.total_goods-d2.total_cost)*et2.sign)
from detail d2
join entry e2 on d2.entry_id=e2.entry_id
join entry_type et2 on et2.entry_type=e2.entry_type
join rep r2 on e2.rep_code=r2.rep_code
join contact c2 on r2.contact_id=c2.contact_id
where d2.detail_type = 'N' and e2.entry_type in('SINV','SCRN','DREC','DSRF')
and c2.contact_id=contact_1.contact_id
and (e2.year_num=entry_1.year_num and e2.period_num=entry_1.period_num)) INV_GP,
(select sum((d2.total_goods)*et2.sign)
from detail d2
join entry e2 on d2.entry_id=e2.entry_id
join entry_type et2 on et2.entry_type=e2.entry_type
join rep r2 on e2.rep_code=r2.rep_code
join contact c2 on r2.contact_id=c2.contact_id
where d2.detail_type = 'N' and e2.entry_type in('SINV','SCRN','DREC','DSRF')
and c2.contact_id=contact_1.contact_id
and (e2.year_num=entry_1.year_num and e2.period_num=entry_1.period_num)) INV_SALE,
(select sum((d2.total_goods-d2.total_cost)*et2.sign)
from detail d2
join entry e2 on d2.entry_id=e2.entry_id
join entry_type et2 on et2.entry_type=e2.entry_type
join rep r2 on e2.rep_code=r2.rep_code
join contact c2 on r2.contact_id=c2.contact_id
where ( e2.entry_type = 'SJIN' ) and ( d2.total_goods = 0 )
and c2.contact_id=contact_1.contact_id
and (e2.year_num=entry_1.year_num and e2.period_num=entry_1.period_num)) EXPEND,
(select count(distinct e2.trader_id)
from entry e2
join rep r2 on e2.rep_code=r2.rep_code
join contact c2 on r2.contact_id=c2.contact_id
where e2.entry_type in('SORD','SINV','DREC')
and c2.contact_id=contact_1.contact_id
and (e2.year_num=entry_1.year_num and e2.period_num=entry_1.period_num)) MTD_PUR,
(select count(distinct e2.our_reference)
from entry e2
join rep r2 on e2.rep_code=r2.rep_code
join contact c2 on r2.contact_id=c2.contact_id
where e2.entry_type in('SORD') and c2.contact_id=contact_1.contact_id
and (e2.year_num=entry_1.year_num and e2.period_num=entry_1.period_num)) MTD_ORD,
(select sum((d1.total_goods-d1.total_cost)*et.sign)
from detail d1
join entry e1 on d1.entry_id=e1.entry_id
join entry_type et on et.entry_type=e1.entry_type
left outer join detail d2 on d2.detail_id=d1.link_detail_id
join rep r2 on e1.rep_code=r2.rep_code
join contact c2 on r2.contact_id=c2.contact_id
where ((d1.detail_type = 'O' and d1.charged_qty<d1.qty and d1.nocharge_qty=0 and e1.entry_type in ('SORD','SRTN')
and e1.our_reference not like 'AUTO%')
or (d1.detail_type = 'N' and e1.entry_type in('SCRN','DREC','DSRF'))
or (( e1.entry_type = 'SJIN' ) and ( d1.total_goods = 0 ))
or(( d1.DETAIL_TYPE = 'N' ) and ( e1.ENTRY_TYPE = 'SINV' ) and ( e1.TAXPOINT_DATE = CURRENT_DATE ) and ( d2.DETAIL_TYPE = 'A' )))
and e1.taxpoint_date=current_date
and c2.contact_id=contact_1.contact_id) DAILY_GP,
(select count(*)
from entry e2
join rep r2 on e2.rep_code=r2.rep_code
join contact c2 on r2.contact_id=c2.contact_id
where e2.entry_type='SORD'
and e2.taxpoint_date=current_date
and c2.contact_id=contact_1.contact_id) NUM_ORDS,
(select count(tn.note)
from trader_notes tn join trader t on tn.trader_id=t.trader_id
join rep r2 on t.rep_code=r2.rep_code
join contact c2 on r2.contact_id=c2.contact_id
where tn.created between current_date
and (current_date+1)
and tn.note_type in ('N','S','G')
and tn.note is not null
and c2.contact_id=contact_1.contact_id) NOTES
FROM ENTRY ENTRY_1 INNER JOIN REP REP_1 ON
(REP_1.REP_CODE = ENTRY_1.REP_CODE)
INNER JOIN CONTACT CONTACT_1 ON
(CONTACT_1.CONTACT_ID = REP_1.CONTACT_ID)
WHERE ( ENTRY_1.ENTRY_TYPE = 'SJOB' )
AND ( ENTRY_1.AGE = 0 )
Run Code Online (Sandbox Code Playgroud)
正如你可以看到这两个子程序是相同的,唯一的区别是select语句的总和.有没有办法我只能运行其中一个子程序来查找总和?
我显示了完整的查询,请有人帮助我吗?
确定 - 这样的事情:
SELECT ENTRY_1.AGE, ENTRY_1.REP_CODE,CONTACT_1.NAME, ENTRY_1.GOODS,
sum((d2.total_goods-d2.total_cost)*et2.sign)
, sum((d2.total_goods)*et2.sign)
.
.
. CODE TAKEN OUT
.
.
FROM
detail d2
join entry e2 on d2.entry_id=e2.entry_id
join entry_type et2 on et2.entry_type=e2.entry_type
join rep r2 on e2.rep_code=r2.rep_code
join contact c2 on r2.contact_id=c2.contact_id
where ((d2.detail_type = 'O' and d2.charged_qty<d2.qty
and d2.nocharge_qty=0)
or (d2.detail_type = 'N' and e2.entry_type in('SINV','SCRN','DREC','DSRF'))
or (( e2.entry_type = 'SJIN' ) and ( d2.total_goods = 0 )))
and c2.contact_id=contact_1.contact_id
and (e2.year_num=entry_1.year_num and e2.period_num=entry_1.period_num)) MTD_GP
, ENTRY ENTRY_1 INNER JOIN REP REP_1 ON
(REP_1.REP_CODE = ENTRY_1.REP_CODE)
INNER JOIN CONTACT CONTACT_1 ON
(CONTACT_1.CONTACT_ID = REP_1.CONTACT_ID)
WHERE ( ENTRY_1.ENTRY_TYPE = 'SJOB' )
AND ( ENTRY_1.AGE = 0 )
Run Code Online (Sandbox Code Playgroud)
当然,这个想法是你将连接移动到下面 - 将它们从SELECT子句中取出并将它们(一次)放入FROM和WHERE子句中. - 我做的剪切和粘贴不是很正确,但你明白了(你也没有提供完整的查询以实际用作基础)