Sql 服务器性能:table 可变内部联接与 where 子句中的多个条件

Sql Server Performance: table variable inner join vs multiple conditions in where clause

在 MS Sql 服务器中,什么更快,一个带有多个条件的 where 子句或创建一个 table 变量后的内部连接?例如:

select A.* from A where A.fk='one  ' or A.fk='two  ' or A.fk='three' ...ect.

declare @temp (key as char(matchingWidth)) table;
insert into @temp values ('one  ');
insert into @temp values ('two  ');
insert into @temp values ('three');
select A.* from A inner join @temp t on A.fk=t.key;

我知道通常差异可以忽略不计;然而,遗憾的是我正在查询的数据库使用 char 类型作为主键...

如果有帮助,在我的特定情况下,table A 有几百万条记录,通常我会查询大约一百个 ID。该列已编入索引,但不是聚集索引。

编辑:我也对 temp table 同样的事情持开放态度......虽然我的印象是 temp table 和 table 变量实际上性能相同。

谢谢!

在大多数情况下,第一种方法会获胜,因为 table 变量不使用统计信息。您会注意到大量数据会导致性能大幅下降。当您只有很少的值时,应该不会有任何明显的差异。