简体   繁体   English

Entity Framework 6,从具有许多指向其他表的链接的大表中获取过滤数据的最佳策略

[英]Entity Framework 6, best strategy to get filtered data from big table with many links to other tables

Which of these ways is better:以下哪种方式更好:

  1. to perform 10000 small queries with filtration conditions to table linked with many other tables (we need data from linked tables too (doing.include() in LINQ expression)) or;对与许多其他表链接的表执行 10000 个带有过滤条件的小查询(我们也需要链接表中的数据(LINQ 表达式中的 doing.include()))或;
  2. to get the data from the table and linked tables piece by piece without any conditions (in a loop, in 10 iterations => 10 queries to db), load them in a collection and then during every iteration perform all analytical work by LINQ with collection?要在没有任何条件的情况下逐个从表和链接表中获取数据(在循环中,在 10 次迭代中 => 10 次对 db 的查询),将它们加载到集合中,然后在每次迭代期间执行 LINQ 与集合的所有分析工作?

I think that second way will be faster but what about memory and how to solve this problem according to the rules of clean architecture?我认为第二种方式会更快,但是 memory 呢?如何根据干净架构的规则解决这个问题?

Neither of these two ways of solving the problem is look adequate.这两种解决问题的方法看起来都不合适。 If you suddenly got in the same situation like me, let`s exhale, drink tea, take a walk in the park.如果你突然陷入和我一样的境地,让我们呼气,喝茶,去公园散步。

Then try to rewrite the algorithm from the beginning.然后尝试从头重写算法。 Almost certainly you can divide it onto the two parts:几乎可以肯定,您可以将其分为两部分:

At first, one query to db to filter out clients with whom you will work further.首先,对 db 进行一次查询以过滤掉您将进一步合作的客户。 That`s can look like:这可能看起来像:

var outdatedClients = db.Clients.Where(client => client.Orders.Any(order => order.Date < DateTime.Now.AddDays(-client.Term));

Then with foreach (var client in outdatedClients){} you can work with outdated orders by using .include() if it necessary.然后使用foreach (var client in outdatedClients){} ,您可以在必要时使用.include()处理过时的订单。 In most situations you can even avoid this cycle and get necessary data with one more query to db.在大多数情况下,您甚至可以避免此循环并通过对 db 进行多一次查询来获取必要的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM