简体   繁体   English

使用存在的EF查询会抛出内存不足异常

[英]EF query using exists throws outofmemory exception

Im trying to get a list of Shop_Orders where their order_num value does not exist in a table called WarhouseOrder . 我试图获取Shop_Orders的列表, Shop_Orders列表中的order_num值在名为WarhouseOrder的表中不存在。 This table contains 500K records and there is an index on OrderNo . 该表包含50万条记录,并且在OrderNo上有一个索引。 List<Shop_Order> contains approx 150 items, each of which has an order_num . List<Shop_Order>包含大约150个项目,每个项目都有一个order_num When this code is executed, it seems very inefficient, slow and results in an outofmemory exception. 当执行此代码时,它看起来效率非常低,缓慢,并导致内存不足异常。 Is there a better way to do this? 有一个更好的方法吗?

List<Shop_Order> new_orders = (from a in osource.order 
    where !ctx.WarehouseOrders.ToList()
    .Exists(o => o.OrderNo == a.order_num) select a).ToList();

WarehouseOrders.ToList() downloads all warehouse orders into memory. WarehouseOrders.ToList()将所有仓库订单下载到内存中。 You can at least avoid that if you'll use Queryable.Any to check condition on database side: 如果要使用Queryable.Any在数据库端检查条件,则至少可以避免这种情况:

!ctx.WarehouseOrders.Any(o => o.OrderNo == a.order_num)

But that will do database query for each shop order. 但这将对每个车间订单进行数据库查询。 I assume that you can get required shop orders with single database query. 我假设您可以通过单个数据库查询获得所需的车间订单。 Eg you can do group join shop orders with warehouse orders and select only those which don't have matches. 例如,您可以使用仓库订单对加入车间的订单进行分组,并仅选择不匹配的订单。 Something like: 就像是:

from so in ctx.ShopOrders
join wo in ctx.WarehouseOrders on so.order_num equals wo.OrderNo into g
where !g.Any()
select so

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

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