October 5, 2014 at 3:47 pm
Hi,
I need to prove that there is a speed issue with one of our stored procs/functions but to do this I need to create a large static work pattern calendar table.
Currently I have
1) A table that contains the start date of numerous work pattern.
2) A table that contains the details of the work patterns i.e on days, on night and off days to the work pattern.
Different work patterns are of different lengths, some may be 8 days, some 34 days, 14 days etc. and they repeat over bank holidays and weekends.
What I need to create is a big work pattern calendar table stating at 01/01/1980 to 31/12/2099 that contains a record for each work pattern for each day.
To create the table using TSQL, I'm currently looking at using multiple loops. But can any one suggest any better ways of doing it or even point me in the direction of similar examples of code?
Thanks
October 5, 2014 at 5:37 pm
I don't know what's involved in your detailed work patterns, so I don't know if you can replace it. My guess would be that you could, but I can't say for sure.
To replace the dates, that's pretty simple once you're familiar with a Tally table. Jeff's article at http://www.sqlservercentral.com/articles/T-SQL/62867/ describes them in some detail and they're well worth the time to read it. The article will really change the way you look at loops. Here's an example of returning a table of dates:
SELECT DATEADD(day, t.N, '01/01/1980')
FROM dbo.Tally t
WHERE t.N < DATEDIFF(day, '01/01/1980', '12/31/2099');
From this, you can perform a set-based insert against your table without a loop. From there, if you have a table with a "master set" of work patterns, you may be able to insert into your work patterns table by using a cross apply of it with your dates table.
I know this isn't complete, but without the DDL and details of what you're after, I don't know where to go from here. I hope it helps.
October 5, 2014 at 11:59 pm
itwhiz (10/5/2014)
Hi,I need to prove that there is a speed issue with one of our stored procs/functions but to do this I need to create a large static work pattern calendar table.
Currently I have
1) A table that contains the start date of numerous work pattern.
2) A table that contains the details of the work patterns i.e on days, on night and off days to the work pattern.
Different work patterns are of different lengths, some may be 8 days, some 34 days, 14 days etc. and they repeat over bank holidays and weekends.
What I need to create is a big work pattern calendar table stating at 01/01/1980 to 31/12/2099 that contains a record for each work pattern for each day.
To create the table using TSQL, I'm currently looking at using multiple loops. But can any one suggest any better ways of doing it or even point me in the direction of similar examples of code?
Thanks
As Ed suggested, generating shedloads of data sequential and constrained randomized data is pretty easy. Can you post what you'd like to see for CREATE TABLE statements for the tables and a bit of data so we can se what you mean by "different work patterns of different lengths" so we have a better understanding of the data you want to generate? Thanks.
--Jeff Moden
Change is inevitable... Change for the better is not.
October 6, 2014 at 5:32 am
itwhiz (10/5/2014)
Hi,I need to prove that there is a speed issue with one of our stored procs/functions ...
Proving that there is a performance issue need not take as much time and effort as you think. Why not post up the actual execution plan so folks have an opportunity to investigate and offer suggestions?
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
October 7, 2014 at 5:45 pm
Are you all set or do you still need help?
--Jeff Moden
Change is inevitable... Change for the better is not.
October 9, 2014 at 3:26 pm
Hi everyone,
Thanks for the replies.
I was working on creating the table using a row for each day/work day pattern, but the slight error has been proven by the testers within the system, so it's been handed back to the software house to rectify, plus I've been moved on to something else.
But thanks again for the offer of help it was much appreciated.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply