According to official Microsoft Data, the performance of ef5.0 is much higher than that of ef4.x. But why is ef5.0 significantly more than ef4.x? Which part of the performance is optimized? Today, let's take a look.
To solve this problem, a common database query code is introduced:
Using (var db = new productcontext ())
{
VaR allfoods = from P in db. Products
Where p. categoryid = "food"
Orderby P. Name
Select P;
Product productss = allfoods. First ();
}
The above code is very easy to understand, that is, to find the first food that meets the conditions and to query the delay. The real query started when the first () was executed. Everyone knows this, but do you know what entityframework does for us when executing this code? Let's analyze this code step by step:
First:
Using (var db = new productcontext () is nothing to explain, that is, creating a context object.
Second:
VaR allfoods = from P in db. Products
Where p. categoryid = "food"
Orderby P. Name
Select P;
This code is used to create a query expression. Because the query is delayed, it does not do any work. It is just a piece of code written in the LINQ syntax.
Finally:
Product productss = allfoods. First ();
This code is the theme part of today. Let's perform some internal tracking on it:
1. Metadata Loading
2. view generation
3. parse parameters in the LINQ Query
4. query conversion is to convert a LINQ statement into an SQL statement through the LINQ engine.
5. Generate a specific query string to make substantive preparations for database queries.
6. database query is divided into the following three steps, that is, the ADO. Net Data Query Process we used previously
* Connection. open ()
* Command. excutereader ()
* Datareader. Read ()
7. Generate object through the database to be queried, that is, the result object set obtained by the query.
8. Connection. Close ()
The above process is the process of performing a data query by ef. I hope you can understand what EF has done for us after reading this article? After learning about these processes, we will conduct some performance analysis on these processes. Why is EF5 faster than EF4 in terms of performance? What process does Microsoft do.
I previously mentioned this in http://www.cnblogs.com/a_ming/archive/2012/08/17/2643266.htmlbroke.
"The performance of the first query through LINQ to entity is almost the same as that of the previous version, but the second query and later query are much different"
The first query mentioned here is called"Cold query"
The second query is called"Hot query"
But whether it is "Cold query" or "Hot query", we have to go through the steps we have just discussed, but the time spent in each step is different, this is why hot queries are faster than cold queries. The reason is the cache. See the following table:
Cold query:
EF4 Performance performance EF5 Performance
1. Metadata loading has a great impact, but cache operations have a great impact.
2. view generationPossibleThe impact is huge, but the cachePossibleThe impact is huge, but the cache
3. parsing parameters in a LINQ query usually has little impact (Performance Improvement)
4. query conversion has a general impact, but Cache
5. generating specific query strings has a moderate impact, but the cache effect is normal, but the cache
6. The database query may be very high or very high.
* Connection. open () (in some cases, some processing is performed to Improve the Performance)
* Command. excutereader ()
* Datareader. Read ()
7. Generating entity objects through the database to be queried has a general impact
8. The impact of connection. Close () is generally affected
Hot query:
EF4 Performance performance EF5 Performance
1. Metadata loading is very low, because the cache for cold queries is very low.
2. view generationVery low for the same reasonVery low, same as above
3. parsing parameters in a LINQ query usually has little impact (Performance Improvement)
4. the query conversion effect is generally very low, with Cache
5. The specific query string generated is very low, the cache is very low, and the cache is
6. The database query may be very high or very high.
* Connection. open () (in some cases, some processing is performed to Improve the Performance)
* Command. excutereader ()
* Datareader. Read ()
7. Generating entity objects through the database to be queried has a general impact
8. The impact of connection. Close () is generally affected
Conclusion:
From the two tables above, we can see that both cold queries and hot queries have higher EF5 performance than EF4.
EF5 seems to have a slight advantage over EF4 in the performance of cold queries, but in hot queries, EF5 performs some caching in some steps, therefore, we do not know how many times the rate of hot queries is increased. This is why EF5 actually improves performance. I hope readers can carefully compare the table. Find the cause.
Therefore, this EF5 performance improvement is mainly for hot queries, because some caching work is done in cold queries, so that hot queries can be executed quickly. As we all know, a server often performs repeated queries. These duplicate queries result in better performance in ef5.
Note: The knowledge you want to share can help you. The author level is limited. If you have any incorrect information, please comment and point out. Thank you!