Link to home
Start Free TrialLog in
Avatar of ram_0218
ram_0218Flag for United States of America

asked on

SQL Tune!!

Guys. Couple of days back I had posted this and got it as a solution. But this peace of code takes hell lot of time and they predict that this would require 21 days to complete the full updation of 10 Million records which is very bad. How can I tune this peace?

DECLARE @l_oldest_effdate   DATETIME
DECLARE @FolderID int

DECLARE  cFOLDERIDS  CURSOR  FOR
SELECT  DISTINCT folderid
FROM    doctaba
WHERE folderid <> 'N/A'
OPEN cFOLDERIDS

FETCH NEXT FROM cFOLDERIDS INTO @FolderID
WHILE @@FETCH_STATUS = 0

BEGIN
            -- get the oldest eff date for this folderid
            SELECT @l_oldest_effdate =  MIN(effrecdate)
            FROM    doctaba
            WHERE   folderid = @FolderID

            UPDATE  doctaba
            SET     primaryrow = 'Y'
            WHERE   folderid = @FolderID
            AND   effrecdate  = l_oldest_effdate;
        FETCH NEXT FROM cFOLDERIDS INTO @FolderID
END

CLOSE cFOLDERIDS
DEALLOCATE cFOLDERIDS


my questions are:-
can someone please modify it to use bulk collect and update and update using rowid. I've no clue about sql server if this were in oracle, i would've known what to do. Please help.
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

This has to be wrong:
UPDATE  doctaba
SET     primaryrow = 'Y'
WHERE   folderid = @FolderID
AND   effrecdate  = l_oldest_effdate;

Surely it should be:

UPDATE  doctaba
SET     primaryrow = 'Y'
WHERE   folderid = @FolderID
      AND   effrecdate  = @l_oldest_effdate;

Not that that is going to make it run much faster.  You need to lose the CURSOR there is simply no reason for it.
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ram_0218

ASKER

Angel,

What about the rollback segments? this plain statemet would be able to update all the 2 Million records?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Try something like this:

UPDATE  d
SET     primaryrow = 'Y'
From      doctaba d
      Inner Join (
            Select      folderid,
                  MIN(effrecdate) FirstDate
            From      doctaba
            Group By
                  folderid) a On d.folderid = a.folderid
>What about the rollback segments? this plain statemet would be able to update all the 2 Million records?
this statement should, with the index in place, run below 1-2 minutes.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
acperkins,

Missed the group by when typing.

Tim
Tim,

And I originally missed the WHERE, so I guess we are even :)

Anthony