[英]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.