【发布时间】:2016-03-11 20:57:56
【问题描述】:
我有一个查询,在 foreach 循环中搜索此查询以查找每个 item
但是太慢了。
foreach (string item in X)
{
//This is the prototype
querystring = "select distinct g.N,g.NH + ' - ' + g.NV + ' - ' + g.S + ' - ' + g.VS ID
from Table1 as g inner join Table2 as d on d.V=g.V
where d.MD= '1234' and g.AC= 'item' and
g.N <> '' and g.NV <> '' and g.S <> '' and g.VS <> '' ORDER BY g.N";
}
我跑了Estimated Execution Plan,我不能在这里附上结果,但是对于表1,index seek(nonClustered) 的成本是40%,而parallelism (repartion streams) is 24% 其余的都很低。
【问题讨论】:
-
是单个查询慢,还是处理
foreach需要很多时间? -
即使单次查询也很慢,foreach 最多搜索20次。但它太慢了。给我结果需要 4 到 7 秒
-
在 C#(?) 代码中触发单个查询并检索 X 中每个项目的相关数据可能更有效。
-
我实际上只为一个项目运行了 Estimated Execution Plan
-
您认为我是否会更好地编写嵌入查询的 foreach?
标签: sql sql-server sql-server-2008 optimization query-optimization