gpt4 book ai didi

sql - T-SQL : Why is my query faster, 如果我使用表变量?

转载 作者:行者123 更新时间:2023-12-04 22:10:57 25 4
gpt4 key购买 nike

谁能解释一下为什么这个查询需要 13 秒:

SELECT   Table1.Location, Table2.SID, Table2.CID, Table1.VID, COUNT(*)
FROM Table1 INNER JOIN
Table2 AS ON Table1.TID = Table2.TID
WHERE Table1.Last = Table2.Last
GROUP BY Table1.Location, Table2.SID, Table2.CID, Table1.VID

而这个只有 1 秒:

DECLARE @Test TABLE (Location INT, SID INT, CID INT, VID INT)

INSERT INTO @Test
SELECT Table1.Location, Table2.SID, Table2.CID, Table1.VID
FROM Table1 INNER JOIN
Table2 AS ON Table1.TID = Table2.TID
WHERE Table1.Last = Table2.Last

SELECT Location, SID, CID, VID, COUNT(*)
FROM @Test
GROUP BY Location, SID, CID, VID

当我从第一个查询中删除 GROUP BY 时,它也只需要 1 秒。我也尝试编写一个子选择并对结果进行分组,但也需要 13 秒。我不明白这一点。

最佳答案

比较execution plans对于这两个查询。

关于sql - T-SQL : Why is my query faster, 如果我使用表变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1599889/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com