4. What should I pay attention to improve the entity framework performance.

Source: Internet
Author: User

I found that many bloggers are opposed to using the EF framework, saying it has low performance. In fact, as long as you use it well, the performance is not a problem. After testing, it will be close to the access of ado.net.

Of course, if you do not know EF and use it casually, it will lead to performance problems. because EF query statements are generated by themselves. if you do not pay attention to it, it will query the database multiple times or use inefficient statements to query.

Next I will summarize the problems we encountered in the project. for your reference. of course, there are still some that are not listed. I hope all the netizens will provide them together to avoid detours.

  1. When paging, try to paging in the database. in my actual project, I found that my colleague does not understand the EF attribute, and its paging is implemented in the memory. see his code below.

QueryToList (). Skip (pageInfo. PageIndex-1) * pageInfo. PageSize). Take (pageInfo. PageSize );

Like the above statement, he will first find the data from the database before paging.

The correct statement should be as follows:

Query. Skip (pageInfo. PageIndex-1) * pageInfo. PageSize). Take (pageInfo. PageSize). ToList.

2. Disable delayed loading as much as possible, and use preload and explicit loading as much as possible.

The default Generation Code of Vs enables delayed loading. This is often because some developers perform multiple queries to the database without understanding it. The following code.

Using (SchoolContainer db = new SchoolContainer ())

{

Var list = db. People. Where (ent => ent. PersonID <30). ToList ();

Foreach (var people in list)

{

Foreach (var ent in people. StudentGrades)

{

Console. WriteLine (ent. Grade );

}

}

}

If delayed loading is enabled, multiple round-trip queries to and from the database will inevitably result in poor performance.

Therefore, we should use preload here. The Code is as follows:

Var list = db. People. Include ("StudentGrades"). Where (ent => ent. PersonID <30). ToList ();

Of course, the preload is used, and the delayed loading will not be queried multiple times. but when programmers write code, they forget to pre-load the database. If delayed loading is enabled, the database will be queried multiple times. if it is not enabled, the programmer will not be able to obtain the data, so that he will immediately understand that the data is to be preloaded.

Of course, nothing is absolute. If the object to be queried is too complex, use preload with caution.

3. Pay attention to the short nature of the transaction.

When using transactions, we should try our best to move query statements or other statements outside the transaction to execute outside the transaction. otherwise, it takes too long for a transaction to cause resource deadlocks. I optimized the code of one of my colleagues last time, and put all irrelevant items in the Code for execution, which leads to a long transaction time, when stress testing is used, the resource is immediately locked.

4. If you do not consider deleting or modifying the queried object, use NoTracking.

For how to use Notracking, see. http://www.cnblogs.com/LingzhiSun/archive/2011/04/27/EF_Trick4.html

5. Delete and modify objects in batches. Do not query objects first, and then delete and modify objects one by one. This will produce a large number of statements, and the efficiency will certainly be low.

The solution is to execute SQL statements directly, and write an extension method on your own. although many people disagree with this method. but I personally prefer to write an extension method myself. in this way, there are two benefits. first, it is easy for developers to use. second, convenient management. some netizens have suggested using ObjectStateManager. changeObjectState is used for batch deletion, but I have not found any related batch deletion methods. in addition, exceptions often occur.

6. Compiled queries are used. Although the results of EF5.0 are automatically cached, compiled queries are more efficient than automatic cache.

Use the compile query, refer to the http://msdn.microsoft.com/zh-cn/library/bb896297.aspx.

7. Pre-generated view,

The procedure is as follows:

Http://blogs.msdn.com/ B /appfabriccat/archive/2010/08/06/isolating-performance-with-precompiled-pre-generated-views-in-the-entity-framework-4.aspx

8. For complex queries, we need to monitor the generated query statements at any time.

After all, the statements generated by EF are often more complex than the statements we generate. In this case, we need to consider whether to Improve the Performance through other methods.

9. For more EF performance optimization, see http://msdn.microsoft.com/zh-cn/library/cc853327.aspx

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.