我怎么知道是否用上了索引?
数据库sql2005
一个表有50万条数据
其中id是主键,唯一增长的
title建了一个索引,asc排列
这个时候我测试
sql1:select * from news where title like 'Worl%'
看计划开销,提示clustered index scan 100%
问1: clustered index scan的话,就是用上聚集索引了吧?
但是我title也建了索引,为什么他不用呢?而用id的scan呢?
我再试试执行
sql2:select * from news where substring(title,1,4)='Worl'
看别人文章说,where里使用函数的话,就没法用上索引了,但是我看看计划开销,提示也是这样的,clustered index scan 100%
问2:sql2005 里,where里用函数也能用上索引?为什么会这样
我执行了sql1和sql2,时间基本是一样的
问3:
sql3:select * from news where convert(varchar(10),createddate,120)='2010-03-02'
order by createddate desc
执行这句sql,我给createddate加上索引,顺序是desc的,看看计划提示,提示的是nested loops(cost:21%)-->index scan(cost:78%)
下面还有一个分支-->clustered index scan(cost:0%)
我分析这可能是用到createddate的索引了,要不为什么会提示的是index scan(cost:78%),和clustered index scan(cost:0%)呢
然后我执行,执行的时间132499 ms
这个时候我把createddate的索引去掉,再看看计划提示,提示的是clustered index scan(cost:100%)
看来这个时候,是找不到索引,默认用clustered index scan了!
我执行,执行的时间132605 ms.
问4:为什么我加了索引,和不加索引,花费的时间几乎一样?
弄到这里我迷糊了,我到底应该怎么加索引,才能让数据库快呢,看刚才的分析,我似乎在title上,在createddate上加索引,和不加索引,花费的时间是一样的,帮忙一下,谢谢你们了!