开发者

linq to sql query precompilation

开发者 https://www.devze.com 2023-03-25 05:07 出处:网络
Depending on the value of search parameters linq can build different sql queries if there is conditional logic in the where clause.

Depending on the value of search parameters linq can build different sql queries if there is conditional logic in the where clause.

How about taking advantage of query precompilation in this case and take the condition into the account? It looks like more then one precompiled query have to be produced. And then the correct precompiled query needs to be fired depending on the input parameters.

Most blog entries/articles are quite limited and are dealing with linq query precompilation only when there is no conditional logic in the query. And then they just mention in the small print that query precompilation is not always beneficial and you might be better off not using it.

For example if there is one conditional parameter in the linq query then 2 different sql queries can be built. So why not to precompile those two linq queries and reuse then accordingly? I'm sure that if you reuse different precompiled queries multiple times then it will save some processing power in the very end.

Reason why I'm raising this is that I'm currently working on a webapp with quite complex search forms. Typical search form can have about 20 fields ( strings, integers, dropdowns etc. ). 95% of searches use default parameters. But other searches are using multiple search parameters. The underlying linq query is taking all parameters into the account, syntax is quite nice, especially with nullable integers, for example : .Where(row=>!model.id.HasValue || row.id==model.id). So two different sql queries can be produced depending on whether int? id has value or not. And my where clause is much more complex - it takes about 20 conditional parameters.

So the question is : is it going to be beneficial performance wise if I precompile linq query depending on conditional parameters? This would result in a precompiled query per combination of confitional parameters. So parameters would have to be analyzed in order to work out whether precompiled query already exists and it can be used or if new precompiled query needs to be created.

This solution can be taken one step further later on. So when the web application starts all linq queries are going to be proactively precompiled for all possible combinations of conditional parameters. Or, in order to reduce the number of precompiled queries, I could use precompilation for few most commonly used confitional parameters combination开发者_Python百科 and force using not precompiled queries for more complex scenarios.

0

精彩评论

暂无评论...
验证码 换一张
取 消