EF performance optimization

The following summarizes some points that should be paid special attention to in the process of using EF, so as to avoid detours.


1. Distinguish between true paging and false paging

Everyone knows that paging is divided into true paging and false paging, and false paging is particularly performance-intensive. We also focus on true paging in the process of use, but when using EF to write paging statements, if we are not careful, true paging will become false paging:

query.ToList().Skip((PageIndex - 1) * PageSize).Take(PageSize); query.Skip((PageIndex - 1) * PageSize).Take(PageSize).ToList();
  • 1
  • 2
  • 3

The above two sentences are similar at first glance, and both will achieve our paging needs. However, the execution process of these two sentences, the generated sql statement is very different. The execution of the first statement is:

a. First query all the data and put it in the memory 
b. Convert it to List 
c. Perform paging operation from the List, and query the results. This is actually the effect of false paging.

The second statement is true paging, passing parameters to the database, generating paging sql statements, and querying the results in the database.


2. Reasonably use the loading method of EF

The choice of data loading method varies from time to time. Improper selection is likely to affect system performance. Each data loading method has its meaning, but there is only one purpose, that is to obtain the required data at the least cost. data.


3. Pay attention to the brevity of transactions

When using transactions, we try to put things that have nothing to do with the transaction to be executed outside the transaction, such as (query statements or other statements outside the transaction). If the execution time of a transaction is too long, it is easy to cause the problem of resource deadlock. , When using the pressure test, the error of the resource being locked will appear immediately.


4. Use of NoTracking

If you do not need to delete and modify the queried entity, please use NoTracking to query.

    using (var context = new MyDbContext())
    { 
         var people = context.People.Where(p => p.PersonID > 100).AsNoTracking().ToList(); } 
  • 1
  • 2
  • 3
  • 4
  • 5

Sometimes our entities just need to be displayed and not updated, so to improve performance we don't need entities to be tracked by the EF context. At this point, you can use the NoTracking query to get the entity, so that the state of the entity will be the Detached state.


5. For queries with relatively complex logic, it is necessary to monitor the generated Sql statements at any time.

After all, the statements generated by EF are often more complex than those generated by us. At this time, we must consider whether to improve performance in other ways. For example, writing native SQL statements yourself, sometimes native SQL statements are a better choice. In addition, we must be good at using the SQL Server Profiler tool to monitor the generated SQL statements in real time.


6. Batch deletion and modification

I don’t know if you have studied EF’s insert, delete and modify operations. When you operate data in batches, you can clearly see through SQL Server Profiler that a large number of Insert and Update statements are generated, and the efficiency is very low; because he inserts a piece of data, it will Correspondingly, an Insert statement is generated. When there are 100,000 pieces of data in your list, 100,000 insert statements will be generated! Fortunately, we have countermeasures: Entity Framework Extendeds, EF extension classes perfectly solve the problem of batch operations.



Because the amount of system data is temporarily small, after making these changes, the performance improvement is not particularly obvious. But little things add up, and we have to strive for a little bit of optimization or improvement. When the amount of system data reaches a certain level, you will find that the effect of this little change will not be just a little bit.

 

 

Summary of Optimization Methods

 

1. Keep the connection open

 

It means not to disconnect when you need to connect when you don't need it, and to connect again when you need it (except in special cases), the purpose is to reduce the operation of the database.

 

2. Turn off some configurations of EF

 

When EF is used, it will be configured in Config. For the use of CRUD functions, some are not used and can be turned off. After turning off, the test effect is accelerated by a few seconds and slightly improved. 
这里写图片描述

 

3. Store in List

 

EF supports AddRange. If you need to store 5 pieces of data, put these 5 pieces of data into the list and store them at one time, which is much faster than storing them one by one.

 

4. Find data with Linq or Lamba

 

In such a situation, you need to fetch a specific piece of data from the database, and then process this data and store it in another table. Use foreach, right, it's too slow, replace it with Linq, there is a note in the above sketch, Change to lamba and try again.

 

5. Put data into memory

 

If you want to find data from a table for use multiple times, it is better to put all the data in the table or the required part of the feature data into the memory first, and the speed of reading from the memory is much faster. It is used to operate the database, and this purpose is to reduce the number of operations on the database and consume performance.

 

6. Use BulkInsert

 

Use the plug-in Extended, and use the BulkSaveChanges instead of EF's original SaveChanges to save data, attach a screenshot to quote a paragraph:

 

这里写图片描述

 

然后我亲测结果如图: 
这里写图片描述 
1000条数据,BulkSaveChanges花费1s,SaveChanges花费27s……

 

插件名: 
这里写图片描述

 

对于该插件的一些使用方法,我也附上网址: 
http://www.zzzprojects.com/products/dotnet-development/bulk-operations/

 

7.使用SQL语句

 

如果你对速度还不满意,可以CRUD,直接使用SQL语句来操作。 
可以参考: 
http://my.oschina.net/Yamazaki/blog/185621

 

8.多表只需存一张

 

遇到这样的情况,P、T两张表,关系是1—–(0,1)的关系,你可能会先存入P表的数据,然后foreach P表的数据,找到对应的给T表的导航属性赋值,然后存入这条数据到T表,这个问题很严重也可笑,但是我身上缺犯了,只需要存 必须存导航数据(T表数据),有关联的表数据自然存入了(P表)

 

结尾

 

上述我总结的方法中,没有使用SQL语句,原先花费1小时20分钟的事,现在花费70s,竟然真的做到了。

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325123890&siteId=291194637