Home Database Mysql Tutorial 索引Index Rebuild和Rebuild Online 详述

索引Index Rebuild和Rebuild Online 详述

Jun 07, 2016 pm 04:08 PM

在Oracle运维领域,两个围绕索引的概念一直在网络上被讨论,一个是Index定期重构的必要性,另一个对Rebuild和Rebuild Online的讨

在Oracle运维领域,两个围绕索引的概念一直在网络上被讨论,一个是Index定期重构的必要性,另一个对Rebuild和Rebuild Online的讨论。前者很多前辈在各种场合,包括Oracle MOS,都有了比较深刻的讨论。

对后者的讨论主要是集中两个方面,即:

  • 对于大数据、高可用性的系统,索引rebuild动作一定要慎用,最好选择在DML操作比较少的时间窗进行,避免影响业务系统;
  • Rebuild online和rebuild在处理上的差异。相对于rebuild,rebuild online对于DML操作的锁定动作是比较小的,但是相应操作时间也比较多。如果是高可用7*24系统,rebuild online往往是比较容易接受的一种折中策略;
  • 本篇主要从执行计划和跟踪执行两个角度,分析两种rebuild索引的特点。

    1、环境介绍

    笔者选择Oracle 11gR2进行测试,具体版本为11.2.0.4。

    SQL> select * from v$version;

     

    BANNER

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

    Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - Production

    PL/SQL Release 11.2.0.4.0 - Production

    CORE  11.2.0.4.0    Production

    TNS for Linux: Version 11.2.0.4.0 - Production

    NLSRTL Version 11.2.0.4.0 - Production

    首先创建数据表T。

    SQL> create table t as select * from dba_objects;

    Table created

     

    SQL> create index idx_t_id on t(object_id);

    Index created

    SQL> exec dbms_stats.gather_table_stats(user,'T',cascade => true);

    PL/SQL procedure successfully completed

    下面我们先从执行计划层面进行分析研究。

    2、Explain Plan研究执行计划

    Explain Plan是我们经常使用分析SQL语句执行计划的方法。笔者发现对于alert index这类DDL操作,Explain语句依然可以分析出对应的结果。

    首先测试rebuild语句。

    SQL> explain plan for alter index idx_t_id rebuild;

    Explained

     

    SQL> select * from table(dbms_xplan.display);

     

    PLAN_TABLE_OUTPUT

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

    Plan hash value: 1483129259

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

    | Id  | Operation              | Name    | Rows  | Bytes | Cost (%CPU)| Time

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

    |  0 | ALTER INDEX STATEMENT  |          | 86129 |  420K|  336  (1)| 00:00:0

    |  1 |  INDEX BUILD NON UNIQUE| IDX_T_ID |      |      |            |

    |  2 |  SORT CREATE INDEX    |          | 86129 |  420K|            |

    |  3 |    INDEX FAST FULL SCAN| IDX_T_ID |      |      |            |

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

     

    10 rows selected

     

     

    这其中,我们首先看到了Index Fast Full Scan动作。在笔者之前的文章中,曾经比较详细的分析过Index Fast Full Scan和Index Full Scan的区别。简单说两者差异如下:

    ü  Index Fast Full Scan是标准的多快读操作;Index Full Scan是单块读操作;

    ü  Index Fast Full Scan返回结果是无序结果;Index Full Scan返回有序结果集合;

    ü  Index Fast Full Scan能进行并行操作;Index Full Scan只能支持单进程读动作;

    在上面的执行计划中,我们发现rebuild操作没有以数据表为基础,而是以索引IDX_T_ID的数据(当然是叶子节点)作为创建依据。由于Index Fast Full Scan返回的无序结果集合,之后就调用了Sort Create Index动作形成新的索引对象。

    综合来看,对于rebuild动作而言,在读取索引的过程中,以索引的叶子节点数据作为数据依据。更进一步说,如果rebuild的索引和数据表已经存在不一致的情况,,那么新生成的索引也一定是不一致的。

    下面我们看rebuild online的分析:

    SQL> explain plan for alter index idx_t_id rebuild online;

    Explained

     

    SQL> select * from table(dbms_xplan.display);

    PLAN_TABLE_OUTPUT

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

    Plan hash value: 1193657316

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

    | Id  | Operation              | Name    | Rows  | Bytes | Cost (%CPU)| Time

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

    |  0 | ALTER INDEX STATEMENT  |          | 86129 |  420K|  336  (1)| 00:00:0

    |  1 |  INDEX BUILD NON UNIQUE| IDX_T_ID |      |      |            |

    |  2 |  SORT CREATE INDEX    |          | 86129 |  420K|            |

    |  3 |    TABLE ACCESS FULL  | T        | 86129 |  420K|  336  (1)| 00:00:0

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

    10 rows selected 

    从执行计划看,两者的差异主要在第三步,就是Table Access Full操作,而且是基于数据表T的操作。所以说明:rebuild online是基于对原始数据表的数据收集,而且是针对数据表进行的全表扫描操作。

    这也就部分解释了为什么rebuild online会比rebuild时间长一些,因为Table Access Full操作会访问所有的数据段结构,而Index Fast Full Scan会访问所有的索引段结构。一般而言,索引段是远远小于数据段的。

    综合来看,rebuild online基于是数据表的内容,检索时间略长,但是引起的锁定动作也相对较小。

    下面,笔者从实践跟踪角度,分析一下rebuild和rebuild online过程中数据读取的差异性。

    更多详情见请继续阅读下一页的精彩内容:

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    When might a full table scan be faster than using an index in MySQL? When might a full table scan be faster than using an index in MySQL? Apr 09, 2025 am 12:05 AM

    Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

    Can I install mysql on Windows 7 Can I install mysql on Windows 7 Apr 08, 2025 pm 03:21 PM

    Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

    Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

    InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

    Difference between clustered index and non-clustered index (secondary index) in InnoDB. Difference between clustered index and non-clustered index (secondary index) in InnoDB. Apr 02, 2025 pm 06:25 PM

    The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values ​​and pointers to data rows, and is suitable for non-primary key column queries.

    MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

    MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

    The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

    In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

    Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Explain different types of MySQL indexes (B-Tree, Hash, Full-text, Spatial). Apr 02, 2025 pm 07:05 PM

    MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

    Can mysql and mariadb coexist Can mysql and mariadb coexist Apr 08, 2025 pm 02:27 PM

    MySQL and MariaDB can coexist, but need to be configured with caution. The key is to allocate different port numbers and data directories to each database, and adjust parameters such as memory allocation and cache size. Connection pooling, application configuration, and version differences also need to be considered and need to be carefully tested and planned to avoid pitfalls. Running two databases simultaneously can cause performance problems in situations where resources are limited.

    See all articles