gpt4 book ai didi

sql - 哪种 SQL 查询速度更快?按连接条件或Where 子句过滤?

转载 作者:行者123 更新时间:2023-12-01 17:13:08 25 4
gpt4 key购买 nike

比较这 2 个查询。将过滤器放在连接条件上或放在 WHERE 子句中更快吗?我一直觉得它在连接标准上更快,因为它会尽快减少结果集,但我不确定。

我将构建一些测试来查看,但我也想获得关于哪个更易于阅读的意见。

查询 1

SELECT      *
FROM TableA a
INNER JOIN TableXRef x
ON a.ID = x.TableAID
INNER JOIN TableB b
ON x.TableBID = b.ID
WHERE a.ID = 1 /* <-- Filter here? */

查询 2

SELECT      *
FROM TableA a
INNER JOIN TableXRef x
ON a.ID = x.TableAID
AND a.ID = 1 /* <-- Or filter here? */
INNER JOIN TableB b
ON x.TableBID = b.ID

编辑

我运行了一些测试,结果表明它实际上非常接近,但是 WHERE 子句实际上稍微快一些! =)

我绝对同意在 WHERE 子句上应用过滤器更有意义,我只是对性能影响感到好奇。

标准运行时间: 143016 毫秒
已用时间加入标准: 143256 毫秒

测试

SET NOCOUNT ON;

DECLARE @num INT,
@iter INT

SELECT @num = 1000, -- Number of records in TableA and TableB, the cross table is populated with a CROSS JOIN from A to B
@iter = 1000 -- Number of select iterations to perform

DECLARE @a TABLE (
id INT
)

DECLARE @b TABLE (
id INT
)

DECLARE @x TABLE (
aid INT,
bid INT
)

DECLARE @num_curr INT
SELECT @num_curr = 1

WHILE (@num_curr <= @num)
BEGIN
INSERT @a (id) SELECT @num_curr
INSERT @b (id) SELECT @num_curr

SELECT @num_curr = @num_curr + 1
END

INSERT @x (aid, bid)
SELECT a.id,
b.id
FROM @a a
CROSS JOIN @b b

/*
TEST
*/
DECLARE @begin_where DATETIME,
@end_where DATETIME,
@count_where INT,
@begin_join DATETIME,
@end_join DATETIME,
@count_join INT,
@curr INT,
@aid INT

DECLARE @temp TABLE (
curr INT,
aid INT,
bid INT
)

DELETE FROM @temp

SELECT @curr = 0,
@aid = 50

SELECT @begin_where = CURRENT_TIMESTAMP
WHILE (@curr < @iter)
BEGIN
INSERT @temp (curr, aid, bid)
SELECT @curr,
aid,
bid
FROM @a a
INNER JOIN @x x
ON a.id = x.aid
INNER JOIN @b b
ON x.bid = b.id
WHERE a.id = @aid

SELECT @curr = @curr + 1
END
SELECT @end_where = CURRENT_TIMESTAMP

SELECT @count_where = COUNT(1) FROM @temp
DELETE FROM @temp

SELECT @curr = 0
SELECT @begin_join = CURRENT_TIMESTAMP
WHILE (@curr < @iter)
BEGIN
INSERT @temp (curr, aid, bid)
SELECT @curr,
aid,
bid
FROM @a a
INNER JOIN @x x
ON a.id = x.aid
AND a.id = @aid
INNER JOIN @b b
ON x.bid = b.id

SELECT @curr = @curr + 1
END
SELECT @end_join = CURRENT_TIMESTAMP

SELECT @count_join = COUNT(1) FROM @temp
DELETE FROM @temp

SELECT @count_where AS count_where,
@count_join AS count_join,
DATEDIFF(millisecond, @begin_where, @end_where) AS elapsed_where,
DATEDIFF(millisecond, @begin_join, @end_join) AS elapsed_join

最佳答案

在性能方面,它们是相同的(并产生相同的计划)

从逻辑上讲,如果将 INNER JOIN 替换为 LEFT JOIN,您应该进行仍然有意义的操作。

根据您的情况,这将如下所示:

SELECT  *
FROM TableA a
LEFT JOIN
TableXRef x
ON x.TableAID = a.ID
AND a.ID = 1
LEFT JOIN
TableB b
ON x.TableBID = b.ID

或者这个:

SELECT  *
FROM TableA a
LEFT JOIN
TableXRef x
ON x.TableAID = a.ID
LEFT JOIN
TableB b
ON b.id = x.TableBID
WHERE a.id = 1

前一个查询不会返回除 1 之外的 a.id 的任何实际匹配项,因此后一个语法(使用 WHERE)为逻辑上更加一致。

关于sql - 哪种 SQL 查询速度更快?按连接条件或Where 子句过滤?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2509987/

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