Some facts about my data
- I have a
TablePatternthat defines periodic day pattern (usually long from few days and up to 28) - Day pattern is stored as
varbinary(4)as bit flags. Some days are on (set to 1 == true), others are off (set to 0 == false). There’s alsoLengthcolumn that defines pattern length stored in the binary column - Each pattern record also has information whether weekends and holidays nullify pattern settings (explained later)
- I have table
Holidaysthat defines holidays (some repeatable others fixed on a particular year) - I have
TableCalendarwhere I need to create X number of records. One for each day.
The problem
When generating records
- I know which pattern to use
- I know date range of days whose records I have to create
- Along with pattern I also know whether weekends/holidays take presence over pattern settings – this means that when I will be generating records it will automatically set them to
falseeven though pattern says it should be true - I also have additional
Offsetthat tells me how to apply the pattern to my date range – ifoffset == 0pattern starts at the first day;offset == 3pattern starts on fourth day; offset is always between 0 and pattern length less 1 (in range of pattern length modulo values) - all days before offset also have to be set as if pattern has started earlier (before starting day of the date range)
Questions
I have basically two options:
- I can generate calendar data on the middle tier, which would make it simpler, but would require a lot of DB calls to insert each record into
TableCalendar. -
I can write a stored procedure and provide it:
- date range
- pattern record ID
- offset
and then write all the logic that would generate and insert records into
TableCalendar. I can’t even imagine how to write this complex stored procedure that would use binary pattern and apply it with offsets etc.
Date ranges will usually be 1, 3 and 12 months long the second one being the most frequent one.
And since this generation is part of a web application’s Ajax call it has to be as snappy as possible. That’s the only reason I suppose that generating all records with a singe DB call would be faster than making lots of DB calls and execute some extremely simple insert statement. But I’m pretty sure that SP will become quite complex hance can also become sluggish.
What would you suggest I should do?
You can as well provide some TSQL code that would show how to generate those records using data I provided in a stored procedure.
You do know you can insert multiple row in one insert. If you can sort your data by the clustered index you will get much faster insert. Change your fill on SQL. TSQL does not like more than 999 rows so I load 900 rows at a time. It is pretty fast – I am inserting 1,000 rows a second on a slow server.