我有一个 SQL 表,我想按 ID 选择多行。例如,我想从我的表中获取 ID 为 1、5 和 9 的行。
我一直在使用类似于以下的 WHERE IN 语句来执行此操作:
SELECT [Id]
FROM [MyTable]
WHERE [Id] IN (1,5,9)
但是,对于“IN”子句中的大量项目,这非常慢
以下是使用 where in 从具有 1,000,000 行的表中选择行的一些性能数据
Querying for 1 random keys (where in) took 0ms
Querying for 1000 random keys (where in) took 46ms
Querying for 2000 random keys (where in) took 94ms
Querying for 3000 random keys (where in) took 249ms
Querying for 4000 random keys (where in) took 316ms
Querying for 5000 random keys (where in) took 391ms
Querying for 6000 random keys (where in) took 466ms
Querying for 7000 random keys (where in) took 552ms
Querying for 8000 random keys (where in) took 644ms
Querying for 9000 random keys (where in) took 743ms
Querying for 10000 random keys (where in) took 853ms
有没有比使用 WHERE IN 更快的方法来执行此操作。
我们不能进行连接,因为这是在断开连接的系统之间。
我听说在 MYSQL 中加入数据的内存临时表可能会更快,但根据我的研究,MSSQL 没有内存表选项,即使这样,插入时也不会出现完全相同的索引扫描作为 WHERE IN 的临时表?
编辑:
该表将 ID 作为 PK,因此具有默认的 PK 索引,cf
CREATE TABLE [dbo].[Entities](
[Id] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_dbo.Entities] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
执行计划
这是生成这些性能结果的控制台应用程序的 GIST https://gist.github.com/lukemcgregor/5914774
编辑 2 我创建了一个函数,它从逗号分隔的字符串创建一个临时表,然后加入该表。它更快,但我认为主要是因为解析查询的问题
Querying for 1 random keys took 1ms
Querying for 1000 random keys took 34ms
Querying for 2000 random keys took 69ms
Querying for 3000 random keys took 111ms
Querying for 4000 random keys took 143ms
Querying for 5000 random keys took 182ms
Querying for 6000 random keys took 224ms
Querying for 7000 random keys took 271ms
Querying for 8000 random keys took 315ms
Querying for 9000 random keys took 361ms
Querying for 10000 random keys took 411ms