为优化数据库中的查询性能,将某张大表进行了分区管理,并且在各分区内分别建立了索引,但每次新导入一批数据后,这批数据上的索引无效。需要在Oracle DBA Studio中对该分区选择“重建本地无效索引”。示例如下:
--建立分区表
create table A(
month varchar2(6) not null,
col1 char2,
col2 number)
partition by range(month)(
partition par_1 value less than('200301'),
partition par_2 value less than('200302'),
partition par_3 value less than('200303'),
partition par_4 value less than('200304'),
partition par_5 value less than('200305'),
partition par_6 value less than('200306')
);
--建立索引
create index idx_a on A(col1) local(
partition par_1,
partition par_2,
partition par_3,
partition par_4,
partition par_5,
partition par_6
);在建立以上表及索引后,导入各月份数据(数据量较大,每月超过300万),然后进行查询,发现单独对A表查询某条记录时,分区内索引是有效的;但若关联其它表查询时,发现A表的分区内索引无效,执行时仍然时进行全表扫描!--查询语句
select count(*) from A,B
where B.month='200305' and A.month = B.month
and A.col1 = B.col1;
--建立分区表
create table A(
month varchar2(6) not null,
col1 char2,
col2 number)
partition by range(month)(
partition par_1 value less than('200301'),
partition par_2 value less than('200302'),
partition par_3 value less than('200303'),
partition par_4 value less than('200304'),
partition par_5 value less than('200305'),
partition par_6 value less than('200306')
);
--建立索引
create index idx_a on A(col1) local(
partition par_1,
partition par_2,
partition par_3,
partition par_4,
partition par_5,
partition par_6
);在建立以上表及索引后,导入各月份数据(数据量较大,每月超过300万),然后进行查询,发现单独对A表查询某条记录时,分区内索引是有效的;但若关联其它表查询时,发现A表的分区内索引无效,执行时仍然时进行全表扫描!--查询语句
select count(*) from A,B
where B.month='200305' and A.month = B.month
and A.col1 = B.col1;
CREATE INDEX a_global_part_idx ON a(month)
GLOBAL PARTITION BY RANGE(month)
(partition par_1 value less than('200301'),
partition par_2 value less than('200302'),
partition par_3 value less than('200303'),
partition par_4 value less than('200304'),
partition par_5 value less than('200305'),
partition par_6 value less than(MAXVALUE)
);你的上面是个交叉记录集,打断了分区索引对于以下这句话不太相信:
“但每次新导入一批数据后,这批数据上的索引无效。”
若是这样,或者可以指定分区表空间
create table A(
month varchar2(6) not null,
col1 char2,
col2 number)
partition by range(month)(
partition par_1 value less than('200301') TABLESPACE part1,
partition par_2 value less than('200302') TABLESPACE part2,
partition par_3 value less than('200303') TABLESPACE part3,
partition par_4 value less than('200304') TABLESPACE part4,
partition par_5 value less than('200305') TABLESPACE part5,
partition par_6 value less than('200306') TABLESPACE part6
);
还有一点可提出,混合分区性能比单获围分区可取
“你的上面是个交叉记录集,打断了分区索引”另外:对于索引无效的问题,我也很难相信,但事实确实如此!而且单查这一张表的话索引是有效的,但若关联其它表查询的话,就会对该分区表进行全表扫描!!!目前我只能这样解决:在DBA中对该分区执行:“重建本地无效索引”
不过对于这样大量的数据,我建议使用hint指示,手工调整执行计划