简体   繁体   中英

What type of Linq to SQL queries should be compiled?

I'm using Linq to SQL on a project and have read articles about how inefficient the framework is. I've read that one way to drastically increase the performance is to create compiled queries. Would all queries perform better compiled? Or are there certain instances where it may not make much of a difference? I assume this is critical on large scale applications that get a high volume of traffic.

Thanks.

I've run into this before. Compiling queries can be a great thing and does improve performance significantly. That is not always the best solution though.

I was working on some reports that took 30-40 minutes to run. What I found was that we were calling certain queries hundreds and thousands of times. Compiling the queries did give improve things, but not nearly enough.

What I ended up doing was running less queries that returned more data. This is less opening and closing connections. The results was reports that ran in less than a minute.

So, if you're having to run a query a few times, compiling it is a great option. If it's running hundreds or thousands of times, you may just need a new approach.

A few posts regarding these two things:

SQL Query Optimization for Reporting: a Different Approach :

http://www.foliotek.com/devblog/sql-query-optimization-for-reporting-a-different-approach/

Unexpected Benefits of Precompiled Queries in Linq:

http://www.foliotek.com/devblog/unexpected-benefits-of-precompilation-of-linq/

One more thing to consider would be the indexes on your database. Your query is slow, but you don't know why it's slow. If it is searching on a column that is not indexed, then that could be your problem, too.

Beware premature optimization!

Until you've profiled your application and determined that you're spending a big chunk of your time creating queries, it's not worth the developer time and the reduction in simplicity, maintainability and flexibility.

Even if you are spending a lot of time on query construction, be aware that pre-compiling queries will reduce their running time by a relatively small constant, and not by orders of magnitude. So begin by considering procedural changes (like Narnian mentions) or caching techniques to reduce the number of times you actually have to run these queries in the first place.

Once you've done this, and still find that there's a bottleneck in composing a query, chances are that 90% of your time is spent in about 5% (or less) of the queries you're running. Optimize just those queries.

Update

Modern implementations of Entity Framework now have query caching built in, so just by using a modern version of .NET you are likely getting 90% of the benefits that precompiled queries would have offered you anyway. All the more reason not to worry about changing your code structure to accommodate a theoretical performance boost.

If you have similar queries running again and again, you can gain a performance increase by compiling them.

There are some caveats though: http://omaralzabir.com/solving_common_problems_with_compiled_queries_in_linq_to_sql_for_high_demand_asp_net_websites/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM