sql优化之 索引感想
1创建聚集索引
create clustered index ci_postxingming on ren(xingming)
2创建非聚集索引
create nonclustered index ci_postxingming on ren(xingming)
3禁用索引
alter index PK__ren__7C8480AE on ren disable
4启用索引
alter index IX_GG_XTRZ_2 on GG_XTRZ rebuild
5删除索引
DROP INDEX PK__ren__7C8480AE ON ren;
1 创建主键的时候一般sql会自动将主键设置为聚集索引,聚集索引是唯一的,一般不能删除,可以在经常用到的列上建立非聚集索引
2非聚集索引,在经常用到的列上建立非聚集索引,一般根据非聚集索引进行相关的数据查询,一非聚集索引的列为条件,(个人测试,一般是必须数据量非常大,至少几十万条以上,才能查出时间差距,并不一定是建立非聚集索引就会缩短时间,又是时间反而会更长(数据量不是特别大的时候测试过,2w多条数据))
下面是截取百度文库的数据显示(1000w条数据 ,三个月内的数据为25w条)
(1)仅在主键上建立聚集索引,并且不划分时间段
select gid,riqi ,tittle from biao
用时:128秒
(2)在主键建立聚集索引,在日期键上简历非聚集索引
select gid,riqi ,tittle from biao where riqi>dateadd(day,-90,getdate())
用时:54秒
(2)在主键建立聚集索引,在日期键上简历非聚集索引
select gid,riqi ,tittle from biao where riqi>dateadd(day,-90,getdate())
用时:54秒
(3) 将聚集索引简历在日期上
select gid,riqi ,tittle from biao where riqi>dateadd(day,-90,getdate())
用时:2秒
个人经过测试,以上方法很有疑问,待解决
问题1:该测试到底数据量是1000w还是25w
问题2:主键一般都会默认为聚集索引,不能随便改变的
问题3:该测试也是特例的测试,并不一定效果会这么明显,有待继续测试。