Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 speed

Author  Topic 

maya_zakry
Constraint Violating Yak Guru

379 Posts

Posted - 2007-03-09 : 23:13:33
hello,
i need some opinion on how to sum up or group by more than 2k records faster.. eg, how do i optimize this?

SELECT DISTINCT r.ClientID,c.ClientName, r.ItemID, r.StockID,r.StockName, r.ExpectedQty,r.QCQty,r.AVAQty,r.PNDQty as pnd, r.VMIQCQty,r.VMIAVAQty,r.VMIPNDQty as vmipnd,

(Select isnull( SUM(d.HoldQty) ,0) FROM tblItemdetail d WHERE d.itemid=r.itemid AND d.ConsignorID=@ClientID AND d.Ownerstatus='VMI') AS VMIPNDQty,

(Select isnull( SUM(d.HoldQty) ,0) FROM tblItemdetail d WHERE d.itemid=r.itemid AND d.ConsignorID=@ClientID AND d.Ownership= i.Supplier AND d.Ownerstatus='VMI') AS PNDQty,

(Select isnull(SUM(d.OriginQty - d.PickQty -d.HoldQty -d.qcqty),0) FROM tblItemDetail d WHERE d.ConsignorID=@ClientID AND d.Ownership= i.Supplier AND d.Ownerstatus='OWN') AS StockAtCustAVAQty,

(Select isnull(SUM(d.HoldQty),0) FROM tblItemDetail d WHERE d.ConsignorID=@ClientID AND d.Ownership= i.Supplier AND d.Ownerstatus='OWN') AS StockAtCustPNDQty,

(Select isnull(SUM(d.qcqty),0) FROM tblItemDetail d WHERE d.ConsignorID=@ClientID AND d.Ownership= i.Supplier AND d.Ownerstatus='OWN') AS StockAtCustQCQty

FROM tblItemCrossRef r
INNER JOIN tblClient c ON c.ClientID=r.ClientID
INNER JOIN tblItemClients i on i.Supplier=r.ClientID
WHERE r.ClientID=@ClientID AND r.StockID LIKE @StockID+'%'

~~~Focus on problem, not solution~~~

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-09 : 23:38:10
Don't use sub-query. Use INNER JOIN or LEFT JOIN if you don't expect any record from the JOINs.

Try this.

SELECT DISTINCT
r.ClientID, c.ClientName,
r.ItemID, r.StockID, r.StockName,
r.ExpectedQty, r.QCQty, r.AVAQty, r.PNDQty as pnd,
r.VMIQCQty, r.VMIAVAQty, r.VMIPNDQty as vmipnd,
a.VMIPNDQty,
b.PNDQty,
e.StockAtCustAVAQty,
e.StockAtCustPNDQty,
e.StockAtCustQCQty
FROM tblItemCrossRef r
INNER JOIN tblClient c ON c.ClientID = r.ClientID
INNER JOIN tblItemClients i on i.Supplier = r.ClientID
INNER JOIN (
Select d.itemid, isnull( SUM(d.HoldQty) ,0) AS VMIPNDQty
FROM tblItemdetail d
WHERE d.ConsignorID = @ClientID
AND d.Ownerstatus ='VMI'
GROUP BY d.itemid
) a on a.itemid = r.itemid
INNER JOIN (
Select d.itemid, d.Ownership, isnull( SUM(d.HoldQty) ,0) AS PNDQty
FROM tblItemdetail d
WHERE d.ConsignorID = @ClientID
AND d.Ownerstatus = 'VMI'
GROUP BY d.itemid
) b on b.itemid = r.itemid and b.Ownership = i.Supplier
INNER JOIN (
Select d.Ownership,
isnull(SUM(d.OriginQty - d.PickQty -d.HoldQty -d.qcqty), 0) AS StockAtCustAVAQty,
isnull(SUM(d.HoldQty),0) as StockAtCustPNDQty,
isnull(SUM(d.qcqty),0) as StockAtCustQCQty
FROM tblItemDetail d
WHERE d.ConsignorID = @ClientID
AND d.Ownerstatus = 'OWN'
) e on e.Ownership = i.Supplier
WHERE r.ClientID = @ClientID
AND r.StockID LIKE @StockID + '%'



KH

Go to Top of Page

maya_zakry
Constraint Violating Yak Guru

379 Posts

Posted - 2007-03-13 : 02:24:15
hello khtan.. this help with making it .. i think 30% faster for >3-4k records..

~~~Focus on problem, not solution~~~
Go to Top of Page

maya_zakry
Constraint Violating Yak Guru

379 Posts

Posted - 2007-03-19 : 04:41:00
hi again.(just a feedback)
ive upgrade my codebehind recently, khtan, your solution works great.. it actually improve the speed 100%!! it's my codebehind who slow it eventhough ive already modify the SP according to your solution.. thanks..

~~~Focus on problem, not solution~~~
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-19 : 04:51:46
Still struggling with this query?
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=80489


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

maya_zakry
Constraint Violating Yak Guru

379 Posts

Posted - 2007-03-19 : 11:37:49
hi peso.. no.. that one is done, completed after khtan proposed the use of cursor, and u enhanced the speed of SP :)..
Now it's a different thing... that one is for my inventory tables tables...
this one is for my autopick function.. 2 different thing

~~~Focus on problem, not solution~~~
Go to Top of Page
   

- Advertisement -