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.
LVL 17
ram_0218Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony PerkinsCommented:
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.
Guy Hengel [angelIII / a3]Billing EngineerCommented:
This single update will do it in 1 go:

UPDATE  doctaba
SET     primaryrow = 'Y'
FROM  doctaba d
WHERE  folderid <> 'N/A'
and  effrecdate  =  (select min (i.effrecdate ) from doctaba i where i.folderid = e.folderid )

to work fast, it will need 1 index on the fields folderid + effrecdate (both fields in the same index, in that order)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ram_0218Author Commented:
Angel,

What about the rollback segments? this plain statemet would be able to update all the 2 Million records?
IT Pros Agree: AI and Machine Learning Key

We’d all like to think our company’s data is well protected, but when you ask IT professionals they admit the data probably is not as safe as it could be.

TimCotteeHead of Software ServicesCommented:
Hi ram_0218,

Surely you could achieve the same thing with:

Update doctaba Set PrimaryRow = 'Y' From doctaba inner join (select folderID, Min(effrecdate) as Oldest From doctaba where folderid <> 'N/A') doctaba1 On doctaba.folderid = doctaba1.folderid and doctaba.effrecdate = doctaba1.oldest

It would certainly be quicker than having to select individual records from the whole table for each row returned in the cursor.

Tim Cottee
Anthony PerkinsCommented:
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
Guy Hengel [angelIII / a3]Billing EngineerCommented:
>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.
Anthony PerkinsCommented:
Let me try that again:

UPDATE  d
SET     primaryrow = 'Y'
From     doctaba d
     Inner Join (
          Select     folderid,
               MIN(effrecdate) FirstDate
          From     doctaba
          WHERE folderid <> 'N/A'
          Group By
               folderid) a On d.folderid = a.folderid and d.effrecdate = a.FirstDate
TimCotteeHead of Software ServicesCommented:
acperkins,

Missed the group by when typing.

Tim
Anthony PerkinsCommented:
Tim,

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

Anthony
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.