简体   繁体   English

查询处理器用尽了内部资源,无法在EF中生成查询计划

[英]The query processor ran out of internal resources and could not produce a query plan in EF

I have a query in EF where there is a List of string value that it checks for existence in another table. 我在EF中有一个查询,该查询中有一个字符串值列表,它会检查另一个表中是否存在。

Please consider the below query for more details. 请考虑以下查询以获取更多详细信息。

Code

List<string> ItmsStock = item.Select(ds => ds.ItemNum).ToList(); // Currently, This List items count is 80,000 records.
this.Db.Database.CommandTimeout = 180;
var existsStckList = Db.Stocktakes.Where(ds => ItmsStock.Contains(ds.ItemNo)).Select(ds => ds.ItemNo).ToList();
item.RemoveAll(ds => existsStckList.Contains(ds.ItemNum));
var ItmsExists = Db.Items.Where(ds => ItmsStock.Contains(ds.ItemNo)).Select(ds => ds.ItemNo).ToList();
ItmsExists = Db.Stocktakes.Where(ds => !ItmsExists.Contains(ds.ItemNo)).Select(ds => ds.ItemNo).ToList();

I searched on the internet and found the converted sql uses IN to check for existence. 我在互联网上搜索,发现转换后的sql使用IN来检查是否存在。 so, the limit for the IN makes the problem. 因此, IN的限制导致了问题。 My question here is, How can I efficiently perform the above actions without using for loop. 我的问题是,如何在不使用for循环的情况下有效地执行上述操作。

I ll be appreciating you, If anybody can help me out. 如果有人可以帮助我,我将不胜感激。

Edit 编辑

Previously, I had the below code. 以前,我有以下代码。 After facing the performance issue with the below code, I wrote the above one. 在遇到以下代码的性能问题后,我编写了上面的代码。

foreach (var stockitems in item)
{
   if (Db.Stocktakes.Any(a => a.ItemNo == stockitems.ItemNum))
   {
      StockResult ss = new StockResult();
      ss.ItemNumber = stockitems.ItemNum;
      ss.FileName = stockitems.FileName;
      Stockres.Add(ss);

   }
   else if (!Db.Stocktakes.Any(a => a.ItemNo == stockitems.ItemNum) && Db.Items.Any(a => a.ItemNo == stockitems.ItemNum))
   {
      var ItemNo = stockitems.ItemNum;
      var AdminId = Convert.ToInt32(Session["AccId"]);
      var CreatedOn = System.DateTime.Now;
      int dbres = Db.Database.ExecuteSqlCommand("insert into Stocktake values({0},{1},{2})", ItemNo, AdminId, CreatedOn);
      Db.SaveChanges();
      totalcount = totalcount + 1;
   }
   else
   {
      StockResult sss = new StockResult();
      sss.ItemNumber = stockitems.ItemNum;
      sss.FileName = stockitems.FileName;
      Stockitemsdup.Add(sss);
   }
 }

Thanks. 谢谢。

Issue batches of 1000 item IDs to the database, or use native SQL and submit a table-valued parameter, or a temp table filled with SqlBulkCopy . 向数据库发出1000个项目ID的批次,或使用本机SQL并提交表值参数或填充有SqlBulkCopy的临时表。

I'm surprised you got htis particular message. 我很惊讶您收到特定消息。 The parameter limit is about 2000 parameters. 参数限制约为2000个参数。 Your query should have been rejected. 您的查询应该已被拒绝。

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

相关问题 实体框架核心-仅使用EF,执行SQL Server并获取结果的“查询处理器耗尽了内部资源”错误 - Entity Framework Core - “The query processor ran out of internal resources” error only using EF, executing SQL server and get the results 查询处理器无法为并行查询执行启动必要的线程资源 - The query processor could not start the necessary thread resources for parallel query execution Parallel.ForEach错误:查询处理器无法启动并行查询执行所需的线程资源 - Parallel.ForEach error: The query processor could not start the necessary thread resources for parallel query execution EF查询可以为空吗? - Could EF query be empty? 无法翻译 EF 核心查询 - EF core query could not be translated EF6使用命令树拦截器禁用查询计划缓存 - EF6 Disable Query Plan Caching with Command Tree Interceptor LINQ不会使用EF Core在组上产生COUNT()查询 - LINQ does not produce COUNT() query on group using EF Core EF Core (LINQ) - 无法翻译查询表达式 - EF Core (LINQ) - The Query expression could not be Translated 使用已运行的Sql查询填充TextBox - Populate TextBox with the Sql query that was ran EF.Functions.Like 在已编译查询中无法转换为 SQL 等效项 - EF.Functions.Like in a compiled query could not be translated to SQL equivalent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM