SQL“或”在 where 子句中导致不使用索引

发布于 2024-12-11 22:51:36 字数 532 浏览 0 评论 0原文

我有一个查询如下:

SELECT COUNT(Table1.Identifier) AS NonCancelCnt 
FROM Table1, Table2 
LEFT JOIN eventattendees ON eventattendees.AttendeeID = 47322 
LEFT JOIN eventsignup ON eventattendees.AttendeeID = eventsignup.AttendeeID 
LEFT JOIN transactions on transactions.registrationID=eventsignup.regid 
WHERE ((eventsignup.EventID = Table1.Identifier) Or (eventsignup.EventID = Table1.AttendanceLinkID)) 

“OR”子句导致不使用索引。如果删除任一部分,我的执行路径就会从 95,000 变为 200,并且速度会大幅提高。

我在返工这样的事情上不是很有经验,这样做的最佳选择是什么?

I've got a query as follows:

SELECT COUNT(Table1.Identifier) AS NonCancelCnt 
FROM Table1, Table2 
LEFT JOIN eventattendees ON eventattendees.AttendeeID = 47322 
LEFT JOIN eventsignup ON eventattendees.AttendeeID = eventsignup.AttendeeID 
LEFT JOIN transactions on transactions.registrationID=eventsignup.regid 
WHERE ((eventsignup.EventID = Table1.Identifier) Or (eventsignup.EventID = Table1.AttendanceLinkID)) 

The "OR" clause is causing no index to be used. If I remove either portion, my execution path goes from 95,000 to 200, and speed is drastically increased.

I'm not very experienced in reworking such a thing, what is my best option for doing so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

浅忆流年 2024-12-18 22:51:36

首先,您应该重写查询以指定 Table1、Table2 和 eventattendees 的联接方式。还可以选择是否要在 WHERE 子句中或在 JOIN 关键字之后指定用于联接的列。稍微清理一下之后,优化器可能会更好地选择要使用的正确索引。

如果仍然不起作用,您可以使用 SQL 提示来指定您希望优化器使用的索引:

WITH INDEX(IX_nameofindex)

First, you should rewrite your query to specify how Table1, Table2 and eventattendees are joined. Also choose whether you want to specify the columns to use to join in the WHERE clause or after the JOIN keyword. After you clean it up a bit, the optimizer may do a better job of picking the proper index to use.

If that still doesn't work, you can use a SQL hint to specify the index you want the optimizer to use:

WITH INDEX(IX_nameofindex)

鸵鸟症 2024-12-18 22:51:36
SELECT COUNT(Table1.Identifier) AS NonCancelCnt 
FROM Table1, Table2 
LEFT JOIN eventattendees ON eventattendees.AttendeeID = 47322 
LEFT JOIN eventsignup ON eventattendees.AttendeeID = eventsignup.AttendeeID 
LEFT JOIN transactions on transactions.registrationID=eventsignup.regid 
WHERE eventsignup.EventID = Table1.AttendanceLinkID

union all

SELECT COUNT(Table1.Identifier) AS NonCancelCnt 
FROM Table1, Table2 
LEFT JOIN eventattendees ON eventattendees.AttendeeID = 47322 
LEFT JOIN eventsignup ON eventattendees.AttendeeID = eventsignup.AttendeeID 
LEFT JOIN transactions on transactions.registrationID=eventsignup.regid 
WHERE eventsignup.EventID = Table1.Identifier
SELECT COUNT(Table1.Identifier) AS NonCancelCnt 
FROM Table1, Table2 
LEFT JOIN eventattendees ON eventattendees.AttendeeID = 47322 
LEFT JOIN eventsignup ON eventattendees.AttendeeID = eventsignup.AttendeeID 
LEFT JOIN transactions on transactions.registrationID=eventsignup.regid 
WHERE eventsignup.EventID = Table1.AttendanceLinkID

union all

SELECT COUNT(Table1.Identifier) AS NonCancelCnt 
FROM Table1, Table2 
LEFT JOIN eventattendees ON eventattendees.AttendeeID = 47322 
LEFT JOIN eventsignup ON eventattendees.AttendeeID = eventsignup.AttendeeID 
LEFT JOIN transactions on transactions.registrationID=eventsignup.regid 
WHERE eventsignup.EventID = Table1.Identifier
旧伤慢歌 2024-12-18 22:51:36

不理解 Table1 和 Table2 是什么,也没有以任何形状连接,您将得到一个笛卡尔结果(对于 Table1 中的每条记录,将与 Table2 中的每条记录连接)

此外,您的 where 子句可以用 IN 简化条款

where
   eventsignup.EventID IN ( Table1.Identifier, Table1.AttendanceLinkID )

Not understanding what Table1 and Table2 are, nor are they joined in any shape, you will get a Cartesian result (for each record in Table1, will be joined with each record in Table2)

Additionally, your where clause could just be simplified with an IN clause

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