在Oracle运维领域,两个围绕索引的概念一直在网络上被讨论,一个是Index定期重构的必要性,另一个对Rebuild和Rebuild Online的讨
在Oracle运维领域,两个围绕索引的概念一直在网络上被讨论,一个是Index定期重构的必要性,另一个对Rebuild和Rebuild Online的讨论。前者很多前辈在各种场合,包括Oracle MOS,都有了比较深刻的讨论。
对后者的讨论主要是集中两个方面,即:
本篇主要从执行计划和跟踪执行两个角度,分析两种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过程中数据读取的差异性。
更多详情见请继续阅读下一页的精彩内容:

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment