日期:2014-05-18  浏览次数:20487 次

关于sql server索引的建立
假设我在表testtable上建立了非聚集索引

create index ix_testtable_1 on testtable (medicalNUmber,name) 

因为表单独使用这两个字段查 和组合查的频率都蛮高,表的数据大概有500W左右

我以后查询表
select * from testtable where name='test'
这样会使用到我的索引吗?
 

------解决方案--------------------
好像不会,你看下查询计划就清楚了
------解决方案--------------------
探讨
假设我在表testtable上建立了非聚集索引

create index ix_testtable_1 on testtable (medicalNUmber,name)

因为表单独使用这两个字段查 和组合查的频率都蛮高,表的数据大概有500W左右

我以后查询表
select * from testtable where name='test'
这样会使用到我的索引吗?
……

------解决方案--------------------
建议采用dawugui的办法。
------解决方案--------------------
http://blog.csdn.net/arrow_gx/article/details/2469555
------解决方案--------------------
SQL SERVER 性能优化(强制使用索引查询)

情况1:
如果在 where 子句中使用参数,也会导致全表扫描。因为SQL只有在运行时才会解析局部变量,但优化程序不能将访问计划的选择推迟到运行时;它必须在编译时进行选择。然而,如果在编译时建立访问计划,变量的值还是未知的,因而无法作为索引选择的输入项。如下面语句将进行全表扫描:
select id from t where num=@num
可以改为强制查询使用索引:
select id from t with(index(索引名)) where num=@num

情况2:
使用索引查询:
select * from 表名(index =索引名) where 索引字段=50100
------解决方案--------------------
你试试
select * from testtable with(index =ix_testtable_1) where name='test'
------解决方案--------------------
探讨

建立3个索引 ,这个那我的表的更新、新增操作会变得很慢,
我的这个testtable是个查询,更新都很频繁的业务表

------解决方案--------------------
你创建的这个索引,因为第一列不是name
所以查询语句 where name = XXX是用不上的

即使有索引 也不是一定会用
假如说你这个where条件的结果占整张表的80%
那显然sql server会自动选择全表扫描
所以也要考虑索引列的选择性