Dapper icon indicating copy to clipboard operation
Dapper copied to clipboard

TVP SqlDataRecord vs DataTable

Open sqmgh opened this issue 12 years ago • 3 comments

One of the projects I have been maintaining occasionally ends up using a large lists of IDs which then get turned into large amount of parameters. As such, I've been doing some initial poking around to see if it was worth switching over to TVPs instead of just using the 'IN' support.

What I found is that after a ~75 parameters or so TVPs become faster. As the numbers grow, it becomes a massive difference; To the point where once you get to 1000 you being talking about orders of magnitude.

What I also found is that in terms of execution speed there isn't very much difference. DataTables might be very slightly slower, but it's within the margin of error. However, the IEnumerable<int> into a DataTable takes much longer than it does to an List<SqlDataRecord>. With a test of 1000 ints, it takes twice as long to make the DataTable as it does the List. (Ignoring some 100% reproducible weirdness, where generating the DataTable takes 50x as long in one step of the benchmark. However, if I copy paste that step, then the copy pasted section runs without speed again. This 50x delay makes it almost as long as the 'IN parameter' dapper code. I thought it might be GC, but I am forcing it do collection between steps, and it doesn't seem to help.)

This generation speed seems to hold true for the current Dapper TVP support. If I create class inheriting IDynamicParameters, and use it to generate and pass a List<SqlDataRecord> to Dapper, it runs faster than just passing the DataTable as a parameter. The gap in speed aligns pretty well with the difference in generation methods I measured in the hand coded tests. However, the method I am using to do this restricts me to only sending the one parameter, so it's obviously not ideal.

So the question is, before I start trying to schedule time in the future to dig into the core of Dapper. Is there technical reason that makes supporting List<SqlDataRecord> difficult?

sqmgh avatar Oct 17 '14 18:10 sqmgh

There is come complexity in terms of not breaking existing calling patterns which might use lists for IN. I'm intrigued but open-minded to what you have in mind here... On 17 Oct 2014 19:08, "sqmgh" [email protected] wrote:

One of the projects I have been maintaining occasionally ends up using a large lists of IDs which then get turned into large amount of parameters. As such, I've been doing some initial poking around to see if it was worth switching over to TVPs instead of just using the 'IN' support.

What I found is that after a ~75 parameters or so TVPs become faster. As the numbers grow, it becomes a massive difference; To the point where once you get to 1000 you being talking about orders of magnitude.

What I also found is that in terms of execution speed there isn't very much difference. DataTables might be very slightly slower, but it's within the margin of error. However, the IEnumerable into a DataTable takes much longer than it does to an List. With a test of 1000 ints, it takes twice as long to make the DataTable as it does the List. (Ignoring some 100% reproducible weirdness, where generating the DataTable takes 50x as long in one step of the benchmark. However, if I copy paste that step, then the copy pasted section runs without speed again. This 50x delay makes it almost as long as the 'IN parameter' dapper code. I thought it might be GC, but I am forcing it do collection between steps, and it doesn't seem to help.)

This generation speed seems to hold true for the current Dapper TVP support. If I create class inheriting IDynamicParameters, and use it to generate and pass a List to Dapper, it runs faster than just passing the DataTable as a parameter. The gap in speed aligns pretty well with the difference in generation methods I measured in the hand coded tests. However, the method I am using to do this restricts me to only sending the one parameter, so it's obviously not ideal.

So the question is, before I start trying to schedule time in the future to dig into the core of Dapper. Is there technical reason that makes supporting List difficult?

— Reply to this email directly or view it on GitHub https://github.com/StackExchange/dapper-dot-net/issues/193.

mgravell avatar Oct 17 '14 20:10 mgravell

I managed to set aside a few minutes to dig into things, and have submitted pull request #194, which should add the support for this. Luckily, unless I missing something, all the functionality used for DataTables could be reused with only a very slightly change to keep lists being used for IN from breaking.

sqmgh avatar Oct 22 '14 18:10 sqmgh

Merged in, just cleaning up here - thanks again for the contribution!

NickCraver avatar Oct 27 '15 10:10 NickCraver