1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > mysql 分区索引失效_数据库分区表全局索引失效故障

mysql 分区索引失效_数据库分区表全局索引失效故障

时间:2021-02-27 03:27:08

相关推荐

mysql 分区索引失效_数据库分区表全局索引失效故障

现场实施反馈一个分区表的几个全局索引老是失效。

通过视图v$sql, v$sqlarea等视图查看,对这张分区表执行过的操作有drop partition和truncate partition操作,但是我查询了

跟SQL相关视图(v$sql, v$sqlarea,还是几张历史记录的视图),但是查询不到操作的具体时间,用这个方法定位今天分区表是否有drop partition和truncate partition

操作行不通了。又询问了现场实施,也没有对这张表有drop partition和truncate partition操作。

之前听说分区表有个job调度定期维护分区表,我找到这个维护分区表的脚本,查看日志发现夜间2点有对分区表执行truncate partition 操作,成功定位问题原因。

/xiaohe/article/386

下面详细描述下哪些操作会对分区表的索引产生影响和如何避免索引失效。

1、监控索引失效的脚本

-- 当前用户的普通索引

select t.index_name,t.table_name,blevel,t.num_rows,t.leaf_blocks,t.distinct_keys

from user_indexes t

where status = 'INVALID';

-- 当前用户的分区索引

/xiaohe/article/386

select t1.blevel,t1.leaf_blocks,t1.INDEX_NAME,t2.table_name,t1.PARTITION_NAME,t1.STATUS

from user_ind_partitions t1, user_indexes t2/xiaohe/article/386

where t1.index_name = t2.index_name

and t1.STATUS = 'UNUSABLE';

2、哪些情况会导致索引失效?

(1)、全局索引失效:对分区执行split、drop、truncate和exchange操作,会导致全局索引失效。

(2)、局部索引失效:对分区执行split操作, 会导致局部索引失效。对分区执行exchange操作,可能会导致局部索引失效。

split会影响新生成的分区局部索引,对已经存在的局部索引不会影响。

exchange要分几种情况:

如果交换的临时表没有索引,或者有索引但是没有使用including indexes关键字,这个交换分区的局部索引失效。

如果交换的临时表有索引,且使用了including indexes关键字, 交换分区局部索引不会受到影响。

/xiaohe/article/386

各种语句如下:

split: alter table t_partition split partition p_max at (30000) into (partition p10,partition p_max) update global indexes;

drop: alter table t_partition drop partition p1 ;

add: alter table t_partition add partition p6 values less than (60000);

alter table t_partition add partition p_max values less than (maxvalue);

truncate: alter table t_partition truncate partition p1 ;

exchange: alter table t_partition exchange partition p1 with table normal_tab;

alter table t_partition exchange partition p1 with table normal_tab including indexes;

3、如何避免索引失效

(1)、全局索引:对于split、drop、truncate和exchange操作,加上update global indexes可以保证全局索引不会失效。

类似如:alter table t_partition truncate partition p1 update global indexes;

对于split操作,新分区的 局部索引失效无法避免。

对于exchange, 需要加上 including indexes,可以避免局部索引不受影响。完整的语句:

alter table t_partition exchange partition p1 with table normal_tab including indexes update global indexes;

/xiaohe/article/386/xiaohe/article/386

4、索引重建

全局索引:

/xiaohe/article/386

create index idx_t_partition_colname_global on t_partition (colname1);

alter index idx_t_partition_colname_global rebuild;

局部索引:

create index idx_t_partition_colname_local on t_partition (colname2) local;

重建某个分区的局部索引:

alter index index_name rebuild partition p1;

/xiaohe/article/386/xiaohe/article/386

版权声明:本文为博主原创文章,未经博主允许。

SQL优化 分区 索引 失效

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。