在 T-SQL 中将临时表连接到多个列的最快执行方法
我需要编写一个 SQL 查询来从另一个包含过滤器值的表中过滤 4 个潜在列的表。
我想做的示例:
-- This table could have anywhere from 1 to 2000 possible rows
DECLARE @WidgetOwners TABLE (OwnerID INT PRIMARY KEY)
INSERT INTO @WidgetOwners VALUES (5)
INSERT INTO @WidgetOwners VALUES (50)
INSERT INTO @WidgetOwners VALUES (111)
INSERT INTO @WidgetOwners VALUES (12345)
INSERT INTO @WidgetOwners VALUES (6)
--etc...
SELECT w.WidgetID
FROM Widgets w
WHERE w.SellerManagerID IN (SELECT o.OwnerID FROM @WidgetOwners)
OR w.SellerID IN (SELECT o.OwnerID FROM @WidgetOwners)
OR w.BuyerManagerID IN (SELECT o.OwnerID FROM @WidgetOwners)
OR w.BuyerID IN (SELECT o.OwnerID FROM @WidgetOwners)
我的印象是上面查询中的 SUB SELECTS 的性能不会很好。有更好的方法吗?这可以通过多次 LEFT JOINING @WidgetOwner 表来完成吗?这里有人有什么建议吗?
请记住,我现在一直使用 SQL 2000,目前无法升级。
编辑 - 示例 2(这是我正在尝试的其他内容)
SELECT w2.WidgetID, w2.* -- etc
FROM (
SELECT w.WidgetID
FROM Widgets w
INNER JOIN @WidgetOwners o ON w.SellerManagerID = o.OwnerID
UNION
SELECT w.WidgetID
FROM Widgets w
INNER JOIN @WidgetOwners o ON w.SellerID = o.OwnerID
UNION
SELECT w.WidgetID
FROM Widgets w
INNER JOIN @WidgetOwners o ON w.BuyerManagerID = o.OwnerID
UNION
SELECT w.WidgetID
FROM Widgets w
INNER JOIN @WidgetOwners o ON w.BuyerID = o.OwnerID
) x
INNER JOIN Widgets w2 ON x.WidgetID = w2.WidgetID
I need to write a SQL query to filter a table on 4 potential columns from another table of the filter values.
Example of what I am trying to do:
-- This table could have anywhere from 1 to 2000 possible rows
DECLARE @WidgetOwners TABLE (OwnerID INT PRIMARY KEY)
INSERT INTO @WidgetOwners VALUES (5)
INSERT INTO @WidgetOwners VALUES (50)
INSERT INTO @WidgetOwners VALUES (111)
INSERT INTO @WidgetOwners VALUES (12345)
INSERT INTO @WidgetOwners VALUES (6)
--etc...
SELECT w.WidgetID
FROM Widgets w
WHERE w.SellerManagerID IN (SELECT o.OwnerID FROM @WidgetOwners)
OR w.SellerID IN (SELECT o.OwnerID FROM @WidgetOwners)
OR w.BuyerManagerID IN (SELECT o.OwnerID FROM @WidgetOwners)
OR w.BuyerID IN (SELECT o.OwnerID FROM @WidgetOwners)
I am under the impression that the SUB SELECTS in the query above will not perform very well. Is there a better way to do this? Could this be done by LEFT JOINING the @WidgetOwner table multiple times? Does anyone have any recommendations here?
Keep in mind that I am stuck with SQL 2000 right now and cannot upgrade at the moment.
EDIT - Example 2 (This is something else I am trying)
SELECT w2.WidgetID, w2.* -- etc
FROM (
SELECT w.WidgetID
FROM Widgets w
INNER JOIN @WidgetOwners o ON w.SellerManagerID = o.OwnerID
UNION
SELECT w.WidgetID
FROM Widgets w
INNER JOIN @WidgetOwners o ON w.SellerID = o.OwnerID
UNION
SELECT w.WidgetID
FROM Widgets w
INNER JOIN @WidgetOwners o ON w.BuyerManagerID = o.OwnerID
UNION
SELECT w.WidgetID
FROM Widgets w
INNER JOIN @WidgetOwners o ON w.BuyerID = o.OwnerID
) x
INNER JOIN Widgets w2 ON x.WidgetID = w2.WidgetID
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
构建高效查询有许多经验法则。但是,我建议,当性能成为问题时,最好尝试不同的方法并在实践中看看哪种方法表现最好。特别是当行数很大时。
There are many rules of thumb in building efficient queries. But, I would suggest, when performance is a concern, it's always best to experiment with different approaches and to see in practice what performs best. Especially when row counts are large.
建议您尝试外连接:
如果有帮助,(不知道您的要求,)
您可以使用
or
根据我的经验,外连接几乎总是与任何其他选项一样有效,特别是与相关子查询相比。
当你提到“临时表”时我也担心。
Suggest you try outer joins:
If helpful, (not knowing your requirement,)
you can use
or
In my experience outer joins are nearly always as efficient as any other option, especially compared to correlated subqueries.
I also worry when you mention "temp table".