mysql alter index rebuild_Oracle alter index rebuild 说明

一.官网说明

在MOS上的一篇文章讲到了rebuild online和offline的区别:

Index Rebuild Is Hanging Or Taking Too Long [ID 272762.1]

Symptoms:=========

Performance issues while rebuilding very large indexes. The offline rebuilds of their index is relatively quick -finishes in 15 minutes. Issuing index rebuild ONLINE statement => finishes in about an hour. This behavior of ONLINE index rebuilds makes it a non-option for large tables as it just takes too long to scan the table to rebuild the index. The offline may not be feasible due to due to the 24/7 nature of the database. This may be a loss of functionality for such situations. If we attempt to simultaneously ONLINE rebuild the same indexes we may encounter hanging behavior indefinitely (or more than 6 hours).

DIAGNOSTIC ANALYSIS:

--------------------

We can trace the sessions rebuilding the indexes with 10046 level 12. Comparing the IO reads for the index-rebuild and the index-rebuild-online reveals the following:

ONLINE index rebuilds:It scans the base table and it doesn't scan the blocks of the index.

OFFLINE index rebuilds:It scans the index for the build operation.

This behaviour is across all versions.

Cause/Explanation=============

When you rebuild index online, it will do a full table scan on the base table. At the same time it will maintain a journal table for DML data, which has changed during this index rebuilding operation. So it should take longer time, specially if you do lots of DML on the same table,while rebuilding index online.

-- rebuild index online的时候,会选择全表扫描,同时会维护一个中间日志表,用来记录在rebuild期间的增量数据,原理类似于物化视图日志,日志表是一个索引组织表(IOT),这张中间表只有插入,不会有删除和修改操作,而且只有主键条件查询,正是IOT最合适的场景。

On the other hand, while rebuilding the index without online option, Oracle will grab the index in X-mode and rebuild a new index segment by selecting the data from the old index. So here we are not allowing any DML on the table hence there is no journal table involved and it is doing an index scan.Hence it will be pretty fast.

--rebuild offline时,选择的6模式的X锁,它根据old index来rebuild。因此不允许进行DML,也就没有中间表。因此也比较块。

Solution/Conclusion:===========

- The ONLINE index rebuild reads the base table, and this is by design.

- Rebuilding index ONLINE is pretty slow.

- Rebuilding index offline is very fast, but it prevents any DML on the base table.

二.rebuild index说明

有关锁的模式信息如下:

锁模式

锁描述

解释

SQL操作

0

none

1

NULL

Select

2

SS(Row-S)

行级共享锁,其他对象只能查询这些数据行

Select for update、Lock for update、Lock row share

3

SX(Row-X)

行级排它锁,在提交前不允许做DML操作

Insert、Update、Delete、Lock row share

4

S(Share)

共享锁:阻止其他DML操作

Create index、Lock share

5

SSX(S/Row-X)

共享行级排它锁:阻止其他事务操作

Lock share row exclusive

6

X(Exclusive)

排它锁:独立访问使用

Alter table、Drop able、Drop index、Truncate table、Lock exclusive

DML操作一般要加两个锁,一个是对表加模式为3的TM锁,一个是对数据行的模式为6的TX锁。只要操作的不是同一行数据,是互不阻塞的。

在rebuild index online的开始和结束阶段时,需要短暂的对表持有模式为4的TM锁的,当获取到4级别的锁之后,才降为2级。如果rebuild online一直没获取到4级别的锁,那么相关的DML全部产生等待。 在执行期间只持有模式2的TM锁,不会阻塞DML操作。 在Oracle 11g之后,oracle做了特殊处理,后续的dml不会被rebuild online的4级别锁阻塞.

所以如果在执行rebuild index online前长事务,并且并发量比较大,则一旦执行alter index rebuild online,可能因为长事务阻塞,可能导致系统瞬间出现大量的锁,对于压力比较大的系统,这是一个不小的风险。这是需要迅速找出导致阻塞的会话kill掉,rebuild index online一旦执行,不可轻易中断,否则可能遇到ORA-08104。

MOS的文档:

Session Was Killed During The Rebuild Of Index ORA-08104 [ID 375856.1]

While running an online index rebuild your sessionwas killed or otherwise terminated abnormally. You are now attempting to run the index rebuild again and is throwing the error:

ORA-08104: this index object ##### is being online built or rebuilt

根据以上说明,我们可以知道在进行online rebuild的时候,Oracle会修改如下信息:

(1)修改ind$中索引的flags,将该flags+512.关于这个flags的含义,在下面的实验中进行说明。

(2)在该用户下创建一个journal table来保存在rebuild期间的增量数据。 该表明名称: sys_journal_.

如果异常结束online rebuild操作,那么oracle就没及时清理journal table和ind$的flags标志位,系统会认为online rebuild还在操作。

当然SMON进程会来处理这些临时段。 在maclean同学(10g,11g OCM)的Blog里提到了功能:

对于永久表空间上的temporary segment,SMON会三分钟清理一次(前提是接到post),如果SMON过于繁忙那么可能temporary segment长期不被清理。temporary segment长期不被清理可能造成一个典型的问题是:在rebuild index online失败后,后续执行的rebuild index命令要求之前产生的temporary segment已被cleanup,如果cleanup没有完成那么就需要一直等下去。

如果SMON不能及时清理,在操作时就会报ORA-08104的错误。

在Oracle10gR2中可以使用dbms_repair.online_index_clean手工清理这些信息,在Oracle 9i下,需要打Bug 3805539后才可以使用该工具。

手工处理的步骤如下:

(1)先查看ind$ flags标志,如果不正确,就减去512.

sql>update ind$ set flags=flags-512 where obj#=;

(2)drop journal table,这个步骤可能会报资源忙,因为有大量的日志正在插入,可以反复重试一下。

sql>drop table .sys_journal_;

注意:

这个步骤不能反,如果先删除sys_journal_临时表,然后再修改index的flags状态,则会报出ora-600 [4610]号错误,即数据字典不一致的错误。

官网关于dbms_repair.online_index_clean的说明:

ONLINE_INDEX_CLEAN Function

This function performs a manual cleanup of failed or interrupted online index builds or rebuilds. This action is also performed periodically by SMON, regardless of user-initiated cleanup.

This function returns TRUE if all indexes specified were cleaned up and FALSE if one or more indexes could not be cleaned up.

Syntax

DBMS_REPAIR.ONLINE_INDEX_CLEAN (

object_idIN BINARY_INTEGER DEFAULT ALL_INDEX_ID,

wait_for_lockIN BINARY_INTEGER DEFAULT LOCK_WAIT)

RETURN BOOLEAN;

Parameters

Parameter

Description

object_id

Object id of index to be cleaned up. The default cleans up all object ids that qualify.

wait_for_lock

This parameter specifies whether to try getting DML locks on underlying table [[sub]partition] object. The default retries up to an internal retry limit, after which the lock get will give up. If LOCK_NOWAIT is specified, then the lock get does not retry.

因此在做rebuild index online的时候,一定要在开始和结束阶段观察系统中是否有长事务的存储,对于并发量较大的系统,最严重的后果,可能在这两个关键点导致数据库产生大量锁等待,系统负载飙升,甚至宕机。0b1331709591d260c1c78e86d0c51c18.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值