EF performance optimization, ef Performance
Overview
In the project, it took me about 1 hour and 20 minutes to use EF to store 2 W pieces of data. Then I had to worry about Optimization ......
Test sketch
The test sketch is for reference only. I will summarize it in detail below.
Summary of optimization methods 1. Smooth connection
This means that you do not need to disconnect the database when necessary (except in special cases). The purpose is to reduce database operations.
2. Disable some EF configurations.
When EF is used, it will be configured in Config. For the CRUD function, some cannot be used. You can disable it. After the function is disabled, the test effect will be improved by several seconds.
3. Save to List
EF supports AddRange. If you need to store five pieces of data, it is faster to store these five pieces of data in a list.
4. Use Linq or Lamba to find data
In this case, you need to extract a specific piece of data from the database, process this piece of data, and save it to another table. Use foreach, right? It's too slow. It's changed to Linq, there is a note in the above sketch, and try again with lamba.
5. Put data into memory
If you want to use data from a table multiple times, it is better to first put all the data in the table or the required feature data into the memory to read the data from the memory, this is much faster than database operations, which reduces the number of database operations and consumes performance.
6. Use BulkInsert
Use the plug-in Extended and use BulkSaveChanges to replace the original SaveChanges of EF to save the data. The following is a reference:
Then I will test the results
1000 data records, 1 s for BulkSaveChanges, 27 s for SaveChanges ......
Plug-in name:
For some usage of this plug-in, I also attached the URL:
Http://www.zzzprojects.com/products/dotnet-development/bulk-operations/
7. Use SQL statements
If you are not satisfied with the speed, CRUD can directly use SQL statements.
Refer:
Http://my.oschina.net/Yamazaki/blog/185621
8. Only one table can be saved.
In this case, the relationship between the P and T tables is 1 -- (0, 1). You may first store the data in the P table and then foreach the data in the P table, find the corresponding navigation attribute assigned to the T table and store the data to the T table. This problem is serious and ridiculous, but I am missing it, you only need to save the navigation data (T table data), and the associated table data is stored in (P table)
End
In my summary above, I didn't use SQL statements. I had to spend 70 s for an hour and 20 minutes.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.