Linq to Entities performance and pregenerating views

后端 未结 1 580
余生分开走
余生分开走 2021-01-25 10:43

We are taking a long time on first runs of our linq to EF queries. I was surprised to see no difference after pre-generating views. I ran across the following claim on stackover

相关标签:
1条回答
  • 2021-01-25 11:46

    I don't think the answer you found is correct. You can think of EF views as of a way of abstracting the database so that EF can do its stuff without knowing anything about the actual database. Therefore EF requires views for any query or modification operation that goes through the EF query/update pipeline. In fact, any/all EF queries (be it Linq queries or ESQL queries) are built by composing on base queries which are actually the views.

    To answer your questions:

    • Views are generated only once typically on the first query
    • compiled queries and views are orthogonal - I explained views above, compiled queries are about caching the generated SQL for a query to avoid compiling the query again. This helps because typically a set of queries used in an app is limited so the generated SQL can be cached (note that if you pass parameters to the query this does not affect the generated SQL since it will be parameterized as well). Starting from EF5 the caching happens automatically (http://blogs.msdn.com/b/efdesign/archive/2011/06/30/auto-compiled-linq-queries-entity-framework-june-2011-ctp.aspx).

    In EF6 there have been a number of improvements to view generation. I would say that in the vast majority of cases you shouldn't have to use pre-generated views with EF6 at all (and I say this as the author of the T4 templates for generating views for EF5 and EF6 and also interactive views for EF6). However we found that for Code First apps in EF6 the actual bottleneck was building the model. There were a few other perf issues as well - see this blog post for more details. A lot of these issues were fixed in EF6.0.2 so if you have not upgraded to EF6.0.2 you should. I think there is a few more perf related fixes coming in EF 6.1.

    Side note: Note he said LINQ or ESQL and not Linq to SQL. ESQL is a SQL-like query language supported by EF - if you are interested you can read more here. Since EF has a good support of LINQ there is not really a lot of scenarios where you would want to use ESQL rather than Linq to Entities. Linq to SQL is unrelated to EF/ESQL/Linq to Entities.

    0 讨论(0)
提交回复
热议问题