Which approach should I use for generating + inserting 100+ records into a SQL table
Some facts about my data
- I have a
TablePattern
that 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 alsoLength
column 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
Holidays
that defines holidays (some repeatable others fixed on a particular year) - I have
TableCalendar
where 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
false
even though pattern says it should be true - I also have additional
Offset
that tells me how to apply the pattern to my date range - ifoffset == 0
pattern starts at the first day;offset == 3
patter开发者_运维知识库n 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.
Insert Into [WFbchDocs] with (holdlock)
([wfID], [bchID], [sID], [folderID]) Values
('1','11','2','1')
,('1','11','3','1')
,('1','11','4','1')
Because I'm not familiar with some of the specifics, I'm not sure how much help I can give, but here's a couple of suggestions I can make.
I'm not certain it would be more efficient, but it is possible to make CLR functions for SQL server. This would allow you to make the complex code you are familiar with in the .Net language of your choice and make that procedure available to SQL Server.
There may be some specifics you are dealing with that I am not understanding, but based on what I have read so far, you might have some success writing a stored procedure using "Common Table Expressions" that may help to make the procedures less complicated.
If you can give more specifics as to your inputs and expected outputs, I might be able to give you a more concrete example.
精彩评论