Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ-to-SQL CompiledQuery.Compile() with Update, Delete, Insert?

All,

So I've got all my select queries in LINQ-to-SQL converted to using CompiledQueries to speed things up. Works great so far for select statements, but I haven't been able to figure out how to pre-compile insert, update, or delete statements.

Granted, when you're inserting, deleting or updating in LINQ-to-SQL, you have to use the object model. But obviously somewhere along the way it generates a query, which would be nice to pre-compile and store in a static member.

Is this possible? What is LINQ performance like for updates, deletes and inserts when its not pre-compiled? I could see it being a lot faster than the selects, because what they do underneath is a lot simpler and less "dynamic"...

like image 566
Sam Schutte Avatar asked Dec 09 '08 21:12

Sam Schutte


2 Answers

There is a big difference. Linq-To-SQL select queries can be large complex expressions trees. Its these that may take sometime 'compiling'. In this case coalescing to some T-SQL that can be run against a SQL Server. So it makes sense to cache the result of an operation so that it can be re-used.

However other Delete, Update, and Insert are simple operations that do not require an expression tree to be converted to T-SQL (LINQ itself is all about querying). Its just unfortunate that we've been trained to think of SQL code that performs these other operations as 'queries', we're not actuall asking for any info.

These operations are only defined by the DataContext not by LINQ hence the code to perform these functions is already compiled.

like image 159
AnthonyWJones Avatar answered Oct 18 '22 07:10

AnthonyWJones


I think of the three only insert would make sense to be able to compile and re-use because delete is trivially simple (DELETE FROM Table WHERE Key...) and UPDATE only updates the fields that have changed and so varies per update operation.

[)amien

like image 36
DamienG Avatar answered Oct 18 '22 07:10

DamienG