根据详细信息过滤器返回主行
我有一个查询,我想根据详细信息是否满足特定条件返回主行。
例如,如果至少一个详细信息行具有 SomeProperty = X,我只想返回特定的主行。
基于以下谓词:
predicate = predicate.And(p =>
p.BasketItems.Where(obi => obi.BasketItemTypeID ==
(int) BasketType.Refund).Count() > 0);
生成以下 SQL:
SELECT COUNT(*)
FROM [dbo].[BasketItems] AS [t3]
WHERE ([t3].[BasketId] = [t0].[OrderBasketID]) AND ([t3].[BasketItemTypeID] = 3)
)) > 0)
问题是它正在执行表扫描,因此查询需要跑一会儿。
只是检查我没有做任何疯狂的事情,并想知道是否有什么可以加快这个查询的速度?
谢谢 邓肯
I have a query where I want to return Master rows based on whether the detail fulfil a certain criteria.
For example, I only want to return a particular Master row if AT LEAST one of the Detail rows have SomeProperty = X.
Based on the following predicate:
predicate = predicate.And(p =>
p.BasketItems.Where(obi => obi.BasketItemTypeID ==
(int) BasketType.Refund).Count() > 0);
generates the following SQL:
SELECT COUNT(*)
FROM [dbo].[BasketItems] AS [t3]
WHERE ([t3].[BasketId] = [t0].[OrderBasketID]) AND ([t3].[BasketItemTypeID] = 3)
)) > 0)
Problem with this is it's doing a table scan, so the query takes a while to run.
Just checking that I'm not doing anything crazy and wonder if there's anything that can speed up this query?
Thanks
Duncan
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(将主表连接到详细信息。选择详细信息具有所需条件的所有行。将结果行压缩到每个主记录 1 行。)
(Join master to detail. select all rows where detail has the required criterion. Squish the resulting rows down to 1 per master record.)