27,579
社区成员
发帖
与我相关
我的任务
分享
大侠们好,不是说 sqlserver自动选择最优化的方案执行吗?
看下面情况
select * from ta a join tb b on a.c1=b.c1 join tx c on b.c2=c.c2
where a.c3 between '2' and '8'
-- and b.c3 between '2' and '8'
ta(c1,c2,c3) c3上聚集索引
tb(c1,c2,c3) c3上聚集索引 c2上非聚集索引
tx(c2) 无索引
其中 a.c3也 b.c3的内容完全一样对应的。
b与c联接操作时,可能用到c2上的索引,也可能用到条件里的b.c3上 但是我发现sqlserver只要加上了c3列它就会自动用聚集索引,如果数据分布按一定的格式,这样反而更慢了?
这是什么情况必须自己注意吗?如果数据变动了,又有可能加上条件更好。sqlserver不能自动选择最优的方案吗?
--大侠们好,不是说 sqlserver自动选择最优化的方案执行吗?看下面情况
select * from ta a join tb b on a.c1=b.c1 join tx c on b.c2=c.c2
where a.c3 between '2' and '8'
-- and b.c3 between '2' and '8'
/*
ta(c1,c2,c3) c3上聚集索引
tb(c1,c2,c3) c3上聚集索引
c2上非聚集索引tx(c2) 无索引
其中 a.c3也 b.c3的内容完全一样对应的。
b与c联接操作时,可能用到c2上的索引,也可能用到条件里的b.c3上
但是我发现sqlserver只要加上了c3列它就会自动用聚集索引,如果数据分布按一定的格式,这样反而更慢了?
这是什么情况必须自己注意吗?如果数据变动了,又有可能加上条件更好。sqlserver不能自动选择最优的方案吗?
*/
--大侠们好,不是说 sqlserver自动选择最优化的方案执行吗?看下面情况
select * from ta a join tb b on a.c1=b.c1 join tx c on b.c2=c.c2
where a.c3 between '2' and '8'
-- and b.c3 between '2' and '8'
/*
ta(c1,c2,c3) c3上聚集索引
tb(c1,c2,c3) c3上聚集索引
c2上非聚集索引tx(c2) 无索引
其中 a.c3也 b.c3的内容完全一样对应的。 b与c联接操作时,可能用到c2上的索引,也可能用到条件里的b.c3上 但是我发现sqlserver只要加上了c3列它就会自动用聚集索引,如果数据分布按一定的格式,这样反而更慢了?这是什么情况必须自己注意吗?如果数据变动了,又有可能加上条件更好。sqlserver不能自动选择最优的方案吗?
*/
use tempdb
go
if object_id('ta') Is not null Drop Table ta
if object_id('tb') Is not null Drop Table tb
if object_id('tx') Is not null Drop Table tx
create table ta(c1 nvarchar(50),c2 nvarchar(50),c3 nvarchar(50));create clustered index ix_ta_c3 on ta(c3);create index ix_ta_c2 on ta(c2);
create table tb(c1 nvarchar(50),c2 nvarchar(50),c3 nvarchar(50));create clustered index ix_tb_c3 on tb(c3);create index ix_tb_c2 on tb(c2);
create table tx(c1 nvarchar(50),c2 nvarchar(50),c3 nvarchar(50));
go
insert into ta(c1,c2,c3) values
('1','2','3'),
('2','3','4'),
('3','4','5'),
('4','5','6'),
('5','6','7');
insert into tb(c1,c2,c3) values
('1','2','3'),
('2','3','4'),
('3','4','5'),
('4','5','6'),
('5','6','7');
insert into tx(c1,c2,c3) values
('1','2','3'),
('2','3','4'),
('3','4','5'),
('4','5','6'),
('5','6','7');
--1
select *
from ta a
join tb b on a.c1 = b.c1
join tx c on b.c2 = c.c2
where a.c3 between '2' and '8'
--2
select *
from ta a
join tb b on a.c1 = b.c1
join tx c on b.c2 = c.c2
where a.c3 between '2' and '8'
and b.c3 between '1' and '1'
--3
select *
from ta a
join tb b on a.c1 = b.c1
join tx c on b.c2 = c.c2
where a.c3 between '2' and '8'
and b.c3 between '2' and '8'