我有一個龐大的查詢,這些查詢已經逐漸發展多年,所有的iv完成都是添加子程序來滿足增長。現在問題是這個查詢需要大約3分鐘的時間來運行。可有一個人幫我優化這個查詢幫助優化SQL查詢
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)
,你可以看到這兩個子程序都是一樣的,唯一不同的是在select語句的總和。有沒有辦法讓我只能運行其中一個子程序來查找總和?
Iv顯示完整的查詢,請問有人能幫我嗎?
哪個數據庫...... SQL-Server,MySQL,Oracle等...... – DRapp 2011-06-09 13:06:45
此外,僅僅從主要集羣加入/子加入來確認,Entry_ID是Entry中的一個自動遞增鍵表,這是直接加入你的「細節」表的「關鍵」(不是你的其他「在哪裏」條件來限定具體細節)......但僅僅是JOIN基礎本身? – DRapp 2011-06-09 13:27:54
Im使用Interbase – c11ada 2011-06-10 10:10:57