根据详细信息过滤器返回主行

发布于 2024-08-18 16:24:02 字数 658 浏览 2 评论 0原文

我有一个查询,我想根据详细信息是否满足特定条件返回主行。

例如,如果至少一个详细信息行具有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千年*琉璃梦 2024-08-25 16:24:02
select M.basketID, max(M.field1) as field1, max(M.field2) as field2 
from dbo.basketItems as M
Inner join detail on M.basketID = detail.basketID
where detail.basketItemTypeID = '3'
group by M.basketID

(将主表连接到详细信息。选择详细信息具有所需条件的所有行。将结果行压缩到每个主记录 1 行。)

select M.basketID, max(M.field1) as field1, max(M.field2) as field2 
from dbo.basketItems as M
Inner join detail on M.basketID = detail.basketID
where detail.basketItemTypeID = '3'
group by M.basketID

(Join master to detail. select all rows where detail has the required criterion. Squish the resulting rows down to 1 per master record.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文