• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

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.
0
ram_0218
Asked:
ram_0218
  • 4
  • 2
  • 2
  • +1
3 Solutions
 
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.
0
 
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)
0
 
ram_0218Author Commented:
Angel,

What about the rollback segments? this plain statemet would be able to update all the 2 Million records?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
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
0
 
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
0
 
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.
0
 
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
0
 
TimCotteeHead of Software ServicesCommented:
acperkins,

Missed the group by when typing.

Tim
0
 
Anthony PerkinsCommented:
Tim,

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

Anthony
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

  • 4
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now