Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Better logic than cursor Expand / Collapse
Author
Message
Posted Saturday, March 2, 2013 2:06 AM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Sunday, June 9, 2013 11:51 AM
Points: 13, Visits: 31
Hi All,

I have two tables Tab1 and Tab2.For each row in Tab1, the table Tab2 contains multiple rows.Important columns in both tables are C0,C1 and C2. So i have to update the C1 and C2 columns in Tab1 with count(C1) and max(C2) for each C0.
assume C0="Temp" and Tab2 has 10 rows for Temp.So Tab1 should be updated with C1 as 10 and C2 asmax(C2) in Tab2.

so i wrote a cursor as below.It takes 1 min for two rows in Tab1.Any better solution ?

declare T1_cursor CURSOR for
select distinct C0 from Tab1

OPEN T1_cursor

FETCH NEXT FROM T1_cursor
INTO @C0

WHILE @@FETCH_STATUS = 0
BEGIN
select @recordCount=count(*)+1,@maxTime=max(maxtime) from Tab2
where C0=@C0

update T1 set RecordCount=@recordCount , Maxtime=@maxTime where C0=@C0

FETCH NEXT FROM T1_cursor
INTO @C0

END -- End of cursor

CLOSE T1_cursor
DEALLOCATE T1_cursor
Post #1425834
Posted Saturday, March 2, 2013 11:59 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 7:46 AM
Points: 20,801, Visits: 32,724
Actually, yes. Don't use a cursor. Oh, wait, you would like some actual code here wouldn't you?

Okay, we can that once you post the DDL (CREATE TABLE statements) for the tables, some sample data (as INSERT INTO statements) for each of the tables, the expected results based on the sample data provided (usually best provided as another table and series of insert statements to make testing/comparisons easier).

Need help with this, read the first article I have referenced below in my signature block. It will help you with everything you need to do to get the best possible answers quickly.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1425881
Posted Saturday, March 2, 2013 1:43 PM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Today @ 7:29 AM
Points: 35,554, Visits: 32,150
Compassionate (3/2/2013)
Hi All,

I have two tables Tab1 and Tab2.For each row in Tab1, the table Tab2 contains multiple rows.Important columns in both tables are C0,C1 and C2. So i have to update the C1 and C2 columns in Tab1 with count(C1) and max(C2) for each C0.
assume C0="Temp" and Tab2 has 10 rows for Temp.So Tab1 should be updated with C1 as 10 and C2 asmax(C2) in Tab2.

so i wrote a cursor as below.It takes 1 min for two rows in Tab1.Any better solution ?

declare T1_cursor CURSOR for
select distinct C0 from Tab1

OPEN T1_cursor

FETCH NEXT FROM T1_cursor
INTO @C0

WHILE @@FETCH_STATUS = 0
BEGIN
select @recordCount=count(*)+1,@maxTime=max(maxtime) from Tab2
where C0=@C0

update T1 set RecordCount=@recordCount , Maxtime=@maxTime where C0=@C0

FETCH NEXT FROM T1_cursor
INTO @C0

END -- End of cursor

CLOSE T1_cursor
DEALLOCATE T1_cursor


I'm really concerned for your system because even a cursor isn't normally as slow as that. There's something really funky going on if it's only doing 2 rows a minute. Perhaps it's just some really big tables that have no indexes. If that's true, even the following set based code may be slower than desired.

And, no... haven't tested it because you didn't post any readily consumable data to test with. Follow the link Lynn pointed to. You'll get much quicker responses and even tested coded answers if you follow the suggestions at that article.

 UPDATE t1
SET RecordCount = preagg.RecordCount,
MaxTime = preagg.MaxTime
FROM dbo.Tab1 t1
JOIN (
SELECT CO, RecordCount = COUNT(*)+1, MaxTime = MAX(MaxTime)
FROM dbo.Tab2
GROUP BY CO
) preagg
ON preagg.CO = t1.CO
;



--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1425898
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse