重建索引:ALTER INDEX..REBUILD ONLINE vs ALTER INDEX..REBUILD
什么时候需要重建索引 1、 删除的空间没有重用,导致 索引出现碎片
2、 删除大量的表数据后,空间没有重用,导致 索引"虚高"
3、索引的 clustering_facto 和表不一致
也有人认为当索引树高度超过4的时候需要进行重建,但是如果表数量级较大,自然就不会有较高的树,而且重建不会改变索引树高度,除非是由于大量引起的索引树“虚高”,重建才会改善性能,当然这又回到了索引碎片的问题上了。 关于索引是否需要重建,Oracle有这么一句话:
Generally speaking, the need to rebuild b-tree indexes is very rare, basically because a b-tree index is largely self-managed or self-balanced. 另外找到了一篇《When should one perform a rebuild?》分析的比较好的文章 Firstly, if the index value were to have monotonically increasing values
then any deleted space could be a problem as this space may not be reused
(making feature 3 above redundant). However, if sufficient entries are
deleted resulting in index nodes being fully emptied (say via a bulk delete)
then feature 4 would kick in and the deleted space could be reused. The
question now becomes one of *when* would the equivalent amount of index
entries be reinserted from the time of the deletions, as index scans (in all
it's manifestations) would be impacted during this interim period. So
monotonically increasing values *and* sparse deletions would present one
case for an index rebuild. These types of indexes can be identified as
having predominately 90-10 splits rather than the usual 50-50 split. Another case would be an index that has deletions without subsequent inserts
or inserts within an acceptable period of time. Such a case would result in
wasted space that can't be effectively reused as there's not the sufficient
insert activity to reclaim the space. However, in this scenario, it's really
the *table* itself rather than the indexes directly that should be rebuilt.
Because such "shrinkage" results in both the table and associated indexes
being fragmented with HWMs that need resetting (to prevent performance
issues with Full Table Scans and all types of Index Scans). Yes the index
needs rebuilding but only as a result of the dependent table being rebuilt
as well. ALTER INDEX..REBUILD ONLINE vs ALTER INDEX..REBUILD alter index rebuild online实质上是扫描表而不是扫描现有的索引块来实现索引的重建. alter index rebuild 只扫描现有的索引块来实现索引的重建。 rebuild index online在执行期间不会阻塞DML操作,但在开始和结束阶段,需要请求模式为4的TM锁。因此,如果在rebuild index online开始前或结束时,有其它长时间的事物在运行,很有可能就造成大量的锁等待。也就是说在执行前仍会产生阻塞, 应该避免排他锁.
而rebuild index在执行期间会阻塞DML操作, 但速度较快. Online Index Rebuild Features:
+ ALTER INDEX REBUILD ONLINE;
+ DMLs are allowed on the base table
+ It is comparatively Slow
+ Base table is referred for the new index
+ Base table is locked in shared mode and DDLs are not possible
+ Intermediate table stores the data changes in the base table, during the index rebuild to update the new index later Offline Index Rebuild Features:
+ ALTER INDEX REBUILD; (Default)
+ Does not refer the base table and the base table is exclusively locked
+ New index is created from the old index
+ No DML and DDL possible on the base table
+ Comparatively faster 两者重建索引时的扫描方式不同,rebuild用的是“INDEX FAST FULL SCAN”,rebuild online用的是“TABLE ACCESS FULL”; 即rebuild index是扫描索引块,而rebuild index online是扫描全表的数据块. 测试过程 SQL> create table t1 as select * From emp;
Table created SQL> CREATE INDEX i_empno on T1 (empno);
Index created SQL> CREATE INDEX i_deptno on T1 (deptno);
Index created SQL> explain plan for alter index i_empno rebuild;
Explained alter index xxx rebuild使用的是INDEX FAST FULL SCAN
SQL> select * from table (dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1909342220
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
--------------------------------------------------------------------------------
| 0 | ALTER INDEX STATEMENT | | 327 | 4251 | 3 (0)| 00:00:01
| 1 | INDEX BUILD NON UNIQUE| I_EMPNO | | | |
| 2 | SORT CREATE INDEX | | 327 | 4251 | |
| 3 | INDEX FAST FULL SCAN| I_EMPNO | | | |
--------------------------------------------------------------------------------
10 rows selected alter index xxx rebuild online使用的是TABLE ACCESS FULL
SQL> explain plan for alter index i_empno rebuild online;
Explained SQL> select * from table (dbms_xplan.display);
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------
Plan hash value: 1499455000
--------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
--------------------------------------------------------------------------------
| 0 | ALTER INDEX STATEMENT | | 327 | 4251 | 3 (0)| 00:00:01
| 1 | INDEX BUILD NON UNIQUE| I_EMPNO | | | |
| 2 | SORT CREATE INDEX | | 327 | 4251 | |
| 3 | TABLE ACCESS FULL | T1 | 327 | 4251 | 3 (0)| 00:00:01
--------------------------------------------------------------------------------
10 rows selected SQL>
转自<http://blog.csdn.net/pan_tian/article/details/46563897>
重建索引:ALTER INDEX..REBUILD ONLINE vs ALTER INDEX..REBUILD的更多相关文章
- 曲演杂坛--重建索引后,还使用混合分区么?(Are mixed pages removed by an index rebuild?)
原文来自:http://www.sqlskills.com/blogs/paul/mixed-pages-removed-index-rebuild/ 在SQL SERVER 中,区是管理空间的基本单 ...
- MSSQL Rebuild(重建)索引
MSSQL Rebuild(重建)索引 前的项目是做数据库的归档,在每次archive后都需要对原数据库的索引进行rebuild,以减少索引碎片,于是乎就自己写了一段sql: DECLARE @tab ...
- 记录一则ORACLE MOVE操作后重建索引过程被强制中断导致的ORA-8104案例
环境:SunOS + Oracle 11.2.0.3 对部分表进行Move操作之后,未重建对应的索引,会导致ORA-1502 索引不可用. 此时需要用下面的查询拼接出重建不可用索引的sql语句: ...
- SQL Server 2012 批量重建索引
关于索引的概念可以看看宋大牛的博客 T-SQL查询高级—SQL Server索引中的碎片和填充因子 整个数据库的索引很多,索引碎片多了,不可能一个个的去重建,都是重复性的工作,所以索性写了个存储过程, ...
- SQL Server通过整理索引碎片和重建索引提高速度
本文章转载:http://database.51cto.com/art/201108/282408.htm SQL Server数据库中,当索引碎片太多时,就会拖慢数据库查询的速度.这时我们可以通过整 ...
- 11G在线重建索引
SQL> select count(*) from test_idx; COUNT(*) ---------- 19087751 SQL> select segment_name,segm ...
- Oracle 重建索引脚本
该指数是一个有力的武器,以提高数据库的查询性能. 没有索引,喜欢同样的标签库没有书籍,找书,他们想预订比登天还难.中,尤其是在批量的DML的情形下会产生对应的碎片.以及B树高度会发生对应变化.因此能够 ...
- SQL Server 索引碎片产生原理重建索引和重新组织索引
数据库存储本身是无序的,建立了聚集索引,会按照聚集索引物理顺序存入硬盘.既键值的逻辑顺序决定了表中相应行的物理顺序 多数情况下,数据库读取频率远高于写入频率,索引的存在 为了读取速度牺牲写入速度 页 ...
- SQL Server 锁实验(重建索引)
昨晚某现场报一个重建索引失败的问题,远程查看后发现是自动收缩的内部会话引发的锁申请超时,突然想起来自己的加锁实验还没完成索引重建部分,今天有空正好做一下: USE [数据库名] GO ALTER IN ...
随机推荐
- S 参数说明
微波系统主要研究信号和能量两大问题:信号问题主要是研究幅频和相频特性:能量问题主要是研究能量如何有效地传输.微波系统是分布参数电路,必须采用场分析法,但场分析法过于复杂,因此需要一种简化的分析方法. ...
- 使用iftop监控网卡实时流量
Iftop工具主要用来显示本机网络流量情况及各相互通信的流量集合,如单独同哪台机器间的流量大小,非常适合于代理服务器和iptables服务器使用,这样可以方便的查看各客户端流量情况.iftop可以在类 ...
- idea中maven导入jar包
导入servlet和jsp的jar包 要想在pom.xml中配置一个依赖,必须要知道这个依赖库的坐标:groupId,artifacatId,version 1.可以通过查阅依赖库的资料获取坐标,然后 ...
- Java项目性能持续优化中……
尽量使用StringBuilder和StringBuffer进行字符串连接, 参考链接: Java编程中“为了性能”尽量要做到的一些地方
- Java反射 Introspector
一.解释 Introspector 内省,自我检查. 位于java中的java.beans包中,其原文说明文为: The Introspector class provides a standard ...
- 图像处理、计算机视觉与模式识别“SCI期刊和顶级会议”总结
期刊: best (1) IEEE Transactions on Pattern Analysis and Machine Intelligence,IEEE模式分析与机器智能汇刊,简称PAMI,是 ...
- semi-global matching 算法总结
semi-global matching(缩写SGM)是一种用于计算双目视觉中disparity的半全局匹配算法.在OpenCV中的实现为semi-global block matching(SGBM ...
- Bulestacks模拟器Bulestacks.prop文件里中英文对照表
打开“Bulestacks.prop”文件后可以看到以下内容,根据中英文对照表来修改即可. # begin build properties (开始设置系统性能)# autogenerated by ...
- Android开发中常用的库总结(持续更新)
这篇文章用来收集Android开发中常用的库,都是实际使用过的.持续更新... 1.消息提示的小红点 微信,微博消息提示的小红点. 开源库地址:https://github.com/stefanjau ...
- QT QGraphicsProxyWidget对象可选择或移动的一些tricks
我在QT图形视图框架中使用QGraphicsProxyWidget嵌入widget,但是无法使其和其它的QGraphicsItem一样可以选择或移动,使用如下语句无效: C++ Code 1234 ...