我有一张很大的表(t),对它按照时间顺序建立了分区(每月一个分区)。并且在关键字段(code)上建立了索引。我用查询计划观看执行计划时,发现如果用这样的语句,则会使用到索引
select * from t where code='111'而如果我在分区上执行如下的语句,则用不到索引
select * from t(partition p201107) where code='111'请问该如何在分区上使用索引呢?
select * from t where code='111'而如果我在分区上执行如下的语句,则用不到索引
select * from t(partition p201107) where code='111'请问该如何在分区上使用索引呢?
select * from t(partition p201107) (index index_name) where code='111'
这个我也试过了,这样写直接运行就报错,
可是如果按下面的方式写就可以正常运行
select * from t (index index_name) where code='111'感觉指定了分区就不能指定索引,真是奇怪
select * from t where code='111'这个用的是全局索引
select * from t(partition p201107) where code='111'这个应该用分区索引的。
create table t(
code varchar(10),
name varchar(100),
sd date
) partition by sd(
p201101 values<('20110201'),
p201102 values<('20110301'),
...
)--建立分区索引
create index idx_code on t(code) local index
我就是用这个语句建立了分区索引了:create index idx_code on t(code) local index现在关键问题是,我在全表上查询时系统会使用到索引,而在分区表上单独查询时则不会用到索引
code varchar(10),
name varchar(100),
sd date
) ON 分区函数(code) --这里认为code是分区列
--建立聚集索引
alter table t
add CONSTRAINT idx_code
PRIMARY KEY CLUSTERED (code)
ON 分区函数(code)