我需要創建一個銷售&委員會的報告總結報告範圍
基本上它去(請原諒SaleDate表的blatent瘋狂,但我簡化了業務邏輯,並在現實中,它其實是有道理的這樣)
SELECT agentName,
SUM(sales.Amount) AS Gross,
SUM(sales.Amount * sales.Commission) AS Commission
FROM agent
INNER JOIN sales ON agent.agentId = sales.agentId
WHERE sales.saleId IN (SELECT saleId FROM saleDate WHERE saleDate.myDate BETWEEN @minDate AND @maxDate)
GROUP BY agentName
所以這個查詢工作絕對好。當我需要添加第二個日期範圍時,會出現問題。
IE瀏覽器,他們想在2007年的銷售比較&並肩銷售。
我現在有基本相同的查詢,但我已經添加別名銷售表,並添加另一個
SELECT agentName,
SUM(sales1.Amount) AS Gross1,
SUM(sales1.Amount * sales1.Commission) AS Commission1,
SUM(sales2.Amount) AS Gross2,
SUM(sales2.Amount * sales2.Commission) AS Commission2,
SUM(sales3.Amount) AS Gross3,
SUM(sales3.Amount * sales3.Commission) AS Commission3
FROM agent
INNER JOIN sales1 ON agent.agentId = sales1.agentId
INNER JOIN sales2 ON agent.agentId = sales2.agentId
INNER JOIN sales3 ON agent.agentId = sales3.agentId
WHERE sales1.saleId IN (SELECT saleId FROM saleDate WHERE saleDate.myDate BETWEEN @minDate1 AND @maxDate1) OR
sales2.saleId IN (SELECT saleId FROM saleDate WHERE saleDate.myDate BETWEEN @minDate2 AND @maxDate2) OR
sales3.saleId IN (SELECT saleId FROM saleDate WHERE saleDate.myDate BETWEEN @minDate3 AND @maxDate3)
GROUP BY agentName
此查詢但永遠走(前20分鐘,我取消了),在原來不到一秒鐘,如果我只使用兩個組,則需要9秒。
有關如何改善此性能的任何想法?
我願意改變這個查詢的設計。
我結束了使用UNION(需要不到一秒的時間了),我做了一個SELECT AGENTNAME,Gross1,Commission1,NULL,NULL,NULL,NULL FROM。 .. UNION SELECT agentName,NULL,NULL,Gross2,Commission2,NULL,NULL FROM ... 然後做了一個選擇從那些和分組和總結 – 2009-09-25 18:52:04