我认为这里的问题是连接 - 有数百万行 - 总是先出现,然后才会出现 where 子句。请在您的表格上尝试此操作,并在消息选项卡中查看时间戳:
declare @t1 table (id int, name nvarchar(100));
declare @t2 table (id int, name nvarchar(100));
insert into @t1 (id, name) values (1, 'a')
insert into @t1 (id, name) values (2, 'b')
insert into @t1 (id, name) values (3, 'c')
insert into @t1 (id, name) values (4, 'd')
insert into @t1 (id, name) values (5, 'e')
insert into @t2 (id, name) values (5, 'e')
insert into @t2 (id, name) values (5, 'f')
insert into @t2 (id, name) values (5, 'g')
insert into @t2 (id, name) values (5, 'h')
insert into @t2 (id, name) values (5, 'i')
insert into @t2 (id, name) values (6, 'j')
insert into @t2 (id, name) values (7, 'k')
insert into @t2 (id, name) values (8, 'l')
print getdate()
-- this is your select statement
select * from @t1 t1 inner join @t2 t2 on t1.id = t2.id where t1.id = 5;
print getdate()
-- this is your select statement
select * from @t1 t1 inner join @t2 t2 on t1.id = t2.id where t2.id = 5;
print getdate()
-- this is done with a WITH to do the filtering beforehand
-- of course, indices will affect the performance a lot
with w2 (id, name) as (select * from @t2 where id = 5)
select * from w2 inner join @t1 t1 on w2.id = t1.id
print getdate()
当然,忽略我的示例数据并像 WITH 子句一样使用您的表。