I Have a cursor in stored procedure under SQL Server 2000 (not possible to update right now) that updates all of table but it usually takes few minutes to complete. I need to make it faster.
Whereas GDEPO:Entry depot, CDEPO:Exit depot,Adet: quantity,E_CIKAN quantity that's used.
Record explanations:
Here's the stored procedure translated into English;
CREATE PROC [dbo].[UpdateProductDetails]
as
UPDATE PRODUCTDETAILS SET E_CIKAN=0;
DECLARE @ID int
DECLARE @SK varchar(50),@DP varchar(50) --SK = STOKKODU = PRODUCTID, DP = DEPOT
DECLARE @DEMAND float --Demand=Quantity, We'll decrease it record by record
DECLARE @SUBID int
DECLARE @SUBQTY float,@SUBCK float,@REMAINS float
DECLARE SH CURSOR FAST_FORWARD FOR
SELECT [ID],PRODUCTID,QTY,EXITDEPOT FROM PRODUCTDETAILS WHERE (EXITDEPOT IS NOT NULL) ORDER BY [DATE] ASC
OPEN SH
FETCH NEXT FROM SH INTO @ID, @SK,@DEMAND,@DP
WHILE (@@FETCH_STATUS = 0)
BEGIN
DECLARE SA CURSOR FAST_FORWARD FOR
SELECT [ID],QTY,E_CIKAN FROM PRODUCTDETAILS WHERE (QTY>E_CIKAN) AND (PRODUCTID=@SK) AND (ENTRYDEPOT=@DP) ORDER BY [DATE] ASC
OPEN SA
FETCH NEXT FROM SA INTO @SUBID, @SUBQTY,@SUBCK
WHILE (@@FETCH_STATUS = 0) AND (@DEMAND>0)
BEGIN
SET @REMAINS=@SUBQTY-@SUBCK
IF @DEMAND>@REMAINS --current record isnt sufficient, use it and move on
BEGIN
UPDATE PRODUCTDETAILS SET E_CIKAN=QTY WHERE ID=@SUBID;
SET @DEMAND=@DEMAND-@REMAINS
END
ELSE
BEGIN
UPDATE PRODUCTDETAILS SET E_CIKAN=E_CIKAN+@DEMAND WHERE ID=@SUBID;
SET @DEMAND=0
END
FETCH NEXT FROM SA INTO @SUBID, @SUBAD,@SUBCK
END
CLOSE SA
DEALLOCATE SA
FETCH NEXT FROM SH INTO @ID, @SK,@DEMAND,@DP
END
CLOSE SH
DEALLOCATE SH
This is because the set-based logic for which RDBMS systems like SQL Server are optimized is completely broken and the entire query process has to be repeated for each row.
Cursors could be used in some applications for serialized operations as shown in example above, but generally they should be avoided because they bring a negative impact on performance, especially when operating on a large sets of data.
Based on our conversation in my other answer to this question, I think I have found a way to speed up your routine.
You have two nested cursors:
So the inner cursor loop runs at least once for every exitdepot row you have. However, your system doesn't really care which items went out with which transaction - you are only trying to calculate the final E_CIKAN values.
So ...
Your outer loop only needs to get the total amount of items shipped out for each product/depot combo. Hence you could change the outer cursor definition to:
DECLARE SH CURSOR FAST_FORWARD FOR
SELECT PRODUCTID,EXITDEPOT, Sum(Qty) as TOTALQTY
FROM PRODUCTDETAILS
WHERE (EXITDEPOT IS NOT NULL)
GROUP BY PRODUCTID, EXITDEPOT
OPEN SH
FETCH NEXT FROM SH INTO @SK,@DP,@DEMAND
(and then also change the matching FETCH from SH at the end of the code to match, obviously)
This means your outer cursor will have many fewer rows to loop through, and your inner cursor will have roughtly the same amount of rows to loop through.
So this should be faster.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With