我认为这里的问题是,连接数以百万计的行总是第一个,只有在这之后才会出现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子句中使用你的表像。
这可能取决于表上的索引。另外,你有没有看过2个查询的执行计划?他们展示了什么? –