Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
rollershade
Starting Member
1 Post |
Posted - 2008-06-11 : 21:38:27
|
Hi guysive got a cursor which takes over an hour to complete and have read on this site that a temp table and the while loop could be quicker so ive created a test before i change live code over. the two code snippets are below, results are from 5 runs on each code and averaged, first result from each test was taken out.Cursor time : 695.6While time : 1000.5So not convinced by temporary table deal, have i written something slightly wrong in the while? as ive not used it before.thankscolCODE :select getdate()dECLARE mm_Cursor CURSOR FOR select TOP 10000 date from playerpointsOPEN mm_CursorDECLARE @date NVARCHAR(50)-- Initial read from the cursorFETCH NEXT FROM mm_Cursor INTO @date-- Loop thru the cursorWHILE @@FETCH_STATUS = 0BEGIN dECLARE @date1 DATETIME set @date1 = @date -- Read from the cursor again FETCH NEXT FROM mm_Cursor INTO @dateENDCLOSE mm_CursorDEALLOCATE mm_Cursorselect getdate()select getdate()CREATE TABLE #TempLoop ( RowID int IDENTITY(1, 1), [date] NVARCHAR(50))DECLARE @NumberRecords int, @RowCount intDECLARE @date AS NVARCHAR(50)-- Insert the resultset we want to loop through-- into the temporary tableINSERT INTO #TempLoop (date) SELECT top 10000 date from playerpoints-- Get the number of records in the temporary tableSET @NumberRecords = @@ROWCOUNTSET @RowCount = 1-- loop through all records in the temporary table-- using the WHILE loop constructWHILE @RowCount <= @NumberRecordsBEGIN dECLARE @date1 DATETIME set @date1 = (SELECT date FROM #TempLoop WHERE RowID = @RowCount) SET @RowCount = @RowCount + 1END-- drop the temporary tableDROP TABLE #TempLoopselect getdate() |
|
tkizer
Almighty SQL Goddess
38200 Posts |
Posted - 2008-06-11 : 23:17:32
|
Why do you need to loop at all? Looping is what is making this so slow, not necessarily the actual cursor or while/#temp.Please describe in words what needs to happen here.Tara KizerMicrosoft MVP for Windows Server System - SQL Serverhttp://weblogs.sqlteam.com/tarad/Subscribe to my blog |
 |
|
jsmith8858
Dr. Cross Join
7423 Posts |
|
|
|
|