Linq to Entities Performance and Regeneration Views

We take a lot of time on the first runs of our linq queries for EF. I was surprised to see the absence of differences after the anticipating views. I came across the following statement: stackoverflow

The presented generation only helps for "standard" queries (for example, when calling someObject.RelatedEnd.Load () or MyContext.SomeSetName (). This does not help for special queries with LINQ or ESQL for obvious reasons. Use CompiledQuery to optimize them.

When he says "for obvious reasons," I have to say, "Well, it’s not yet obvious to me." If I understand this correctly, he claims that Linq to SQL queries are not affected by the EF preliminary view.

I thought that entity representations are general comparisons between entities and tables and have nothing to do with any particular query. Is this a mistake?

I can see the huge amount of time that is used in the first run of our Linq to Entities queries, with much shorter times after that, so I assumed that views were created for the corresponding objects and tables. If this is not an EF view that can be pre-generated based on the entire first run time, then what is it?

So my question has three parts:

  • Are EF views created for each query, or do they simply link tables to objects regardless of the queries made?

  • Is the above statement that preliminary representations of EF do not affect the correctness of Linq's answers to EF? Do I need to use CompileQueries?

  • What are the “obvious reasons” mentioned above?

Note. I wouldn’t even ask, but on the Internet there are several recommendations (including msdn) for previewing submissions if you use EF. This is the only place where I saw that he suggested that if you use Linq for Entities, then pregenerating is not relevant to your requests.

+1
source share
1 answer

I do not think the answer you found is correct. You can think of EF's views as a way to abstract the database so that EF can do its own thing without knowing anything about the real database. Therefore, EF requires viewing for any query or modification operation that passes through the EF query / update pipeline. In fact, any / all EF queries (whether they are Linq queries or ESQL queries) are created by compiling basic queries, which are actually representations.

To answer your questions:

  • Views are generated only once, usually on the first request
  • compiled queries and views are orthogonal - I explained above, compiled queries relate to caching the generated SQL for the query to avoid compiling the query again. This helps, because usually the set of queries used in the application is limited, so the generated SQL can be cached (note that if you pass parameters to the query, this will not affect the generated SQL, since it will also be parameterized). Starting with EF5, 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, several improvements have been made to view generation. I would say that in the vast majority of cases you will not have to use pre-created views with EF6 at all (and I say this as the author of T4 to generate views for EF5 and EF6, as well as interactive views for EF6 ). However, we found that for Code First applications in EF6, the actual bottleneck was creating the model. There were also several other perforation issues - see this blog post for more details. Many of these problems have been fixed in EF6.0.2, so if you have not upgraded to EF6.0.2, you should. I think that in EF 6.1 there are a few more fixes related to perforation.

Side Note: Notice, he said LINQ or ESQL , not Linq to SQL . ESQL is an SQL-like query language supported by EF. If you are interested, you can read it here . Since EF has good LINQ support, there are actually not many scenarios where you would like to use ESQL rather than Linq for Entities. Linq to SQL is not related to EF / ESQL / Linq with objects.

+1
source

Source: https://habr.com/ru/post/978838/


All Articles