Hi,
I have a problem with a dContext that have many records (100k) and field (70).
I see that 41000 record was processed in 4 hour and 45 minutes!
This is my code:
Code
var sourceRecords = _dbContext.VEstrazioneVendites.ToList();My experiences with ado are very different, more fast.
Thanks

Amit MohantyRank #112 · 20K reputationPosted Jun 28, 2023, 2:54 AM EDT
Option 1:
If streaming is supported by your database provider, you can use IEnumerable instead of ToList() to stream the records directly without loading the entire result set into memory. Here's an example:
var sourceRecords = _dbContext.VEstrazioneVendites.AsEnumerable(); foreach (var record in sourceRecords) { // Process each record here }Using AsEnumerable() allows you to iterate over the records one by one, without loading the entire dataset into memory. This can be more memory-efficient and might improve performance for large datasets.
Option 2:
Instead of loading all records at once, retrieve the data in smaller chunks using pagination. This way, you can process and load a subset of records at a time, reducing memory consumption and improving performance. You can achieve pagination using methods like Skip() and Take().
int pageSize = 1000; int pageNumber = 1; var totalCount = _dbContext.VEstrazioneVendites.Count(); var totalPages = (int)Math.Ceiling((double)totalCount / pageSize); while (pageNumber e.Id) .Skip((pageNumber - 1) * pageSize) .Take(pageSize) .ToList(); // Process the pageRecords here pageNumber++; }Deepak RawatRank #646 · 3.7K reputationPosted Jun 28, 2023, 3:08 AM EDT
Use IQueryable instead of ToList(): Calling
ToList()loads all the records into memory at once, which can consume a significant amount of resources. Instead, useIQueryableto build the query and retrieve data in smaller batches or as needed. This allows for better memory management and can improve performance.Apply filters and pagination: If you don't need to process all the records at once, consider applying filters or using pagination techniques to retrieve and process data in smaller chunks. This can reduce the amount of data loaded into memory and improve overall performance.
Optimize database queries: Ensure that your database is properly indexed to improve query performance. Analyze the queries being executed and identify any potential bottlenecks or inefficient queries. Consider optimizing queries by adding appropriate indexes or rewriting queries to be more efficient.
Use asynchronous processing: If your application allows, consider using asynchronous programming techniques to process the records. This can help improve overall throughput by allowing multiple tasks to run concurrently.
Use caching: If the data you are processing doesn't change frequently, you can consider implementing caching mechanisms to reduce the number of database calls. This can significantly improve performance, especially if the same data is accessed multiple times during the processing.
Monitor and optimize resource usage: Check for any resource-intensive operations or bottlenecks that might be slowing down the processing. Monitor CPU and memory usage to identify potential issues and optimize your code accordingly.
Consider using a batch processing approach: If possible, break down the processing into smaller batches and process them in parallel. This can help distribute the workload and improve overall performance.
cjard43 reputationPosted Jun 28, 2023, 10:55 AM EDT
Why do you want to load 41,000 entities into your client side code? Leave them in the DB and ask the DB for the few you want when you want them
Mic Got91 reputationPosted Jun 28, 2023, 11:47 AM EDT
Hi,
The reason for load all records is that i want to copy them from one dbContext to another.
As regards the performance i have make some test and i have solved the problem with:
But this is not the only reason of the slowness, i have make a mistake with:
I have move it out of the foreach iteration so i execute this code only once instead of each record.
cjard43 reputationPosted Jul 1, 2023, 2:03 AM EDT
If you want to copy one database to another, or one table to another, using EF and downloading all as entities into the client is about the slowest way to do it.. look at database replication or direct query/bulk copy over linked databases
Prasad RaveendranRank #320 · 7.8K reputationPosted Jul 1, 2023, 12:07 PM EDT
Select only necessary fields: If you don't need all 70 fields from each record, consider selecting only the required fields using the Select method. This reduces the amount of data transferred from the database to your application, which can improve performance.
var pageSize = 1000; // Adjust the page size as per your requirements var pageNumber = 0; var records = new List(); do { var pageRecords = await _dbContext.VEstrazioneVendites .OrderBy(v => v.Id) // Order by an appropriate column for consistent results .Skip(pageNumber * pageSize) .Take(pageSize) .Select(v => new VEstrazioneVendite { // Select only the necessary fields here Field1 = v.Field1, Field2 = v.Field2, // ... }) .ToListAsync(); records.AddRange(pageRecords); pageNumber++; } while (pageRecords.Count == pageSize);By implementing these optimizations, you should see improved performance when fetching and processing a large number of records. Remember to adjust the pageSize value according to your specific scenario and database performance characteristics.