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 |
|
lappin
Posting Yak Master
182 Posts |
Posted - 2011-01-21 : 11:17:24
|
| Hi I have a procedure which inserts /updates data for a single day. I have another proc which takes a date range and calls the proc for each day in the range. The method I use uses a number table and a cursor. In testing the daterange takes much longer than the total of the individual single daily runs.I have tested over a range of dates where no data exists. The daily proc simply exits out in this case. E.g. Calling daily proc for 15 days takes 1 second:DBCC DROPCLEANBUFFERSEXECUTE [INS_Daily]'1 jan 2012'GOEXECUTE [INS_Daily]'2 jan 2012'....................EXECUTE [INS_Daily]'14 jan 2012'GOEXECUTE [INS_Daily]'15 jan 2012'GOBut calling for date range takes 2 minutes:DBCC DROPCLEANBUFFERSExec [INS_DateRange] @startdate='1 jan 2012', @enddate='15 jan 2012'Here is the daterange proc:alter PROCEDURE [dbo].[INS_DateRange] (@startdate datetime,@enddate datetime )ASDECLARE @RC intDECLARE @ThisDate datetime--RUN Proc once before date incrementedEXECUTE @RC = [INS_Daily] @startdatedeclare @period datetimedeclare datecursor cursor forward_only for Select dateadd(d,Number,@startdate) from dbo.Numbers where Number <= datediff(d, @startdate, @enddate) order by Numberopen datecursorwhile (1=1)begin fetch next from datecursor into @period if @@fetch_status <> 0 break;EXECUTE @RC = [dbo].[INS_Daily] @Periodendclose datecursordeallocate datecursorGO---------------------------------I checked the numbers table performance. It takes < 1 second to run.declare @startdate datetime,@enddate datetimeset @startdate='1 jan 2012'set @enddate='15 jan 2012'Select dateadd(d,Number,@startdate) from dbo.Numberswhere Number <= datediff(d, @startdate, @enddate)order by NumberDoes anyone have an alternative date loop method? |
|
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2011-01-21 : 12:22:34
|
| >> I have a procedure which inserts /updates data for a single day. I have another proc which takes a date range and calls the proc for each day in the range. The method I use uses a number table and a cursor. In testing the date range takes much longer than the total of the individual single daily runs. <<I find that hard to believe. Can you post the DDL and the daily procedure code? A clustered index ont he date involved should make things very fast.--CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-01-24 : 11:32:45
|
| instead of calling the proc for each date cant you change proc to take date range(start,end) and process everything between. in making so you can avoid first proc altogether------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|