Implementation of oracle re-Indexing

Source: Internet
Author: User

Oracle re-indexing is a frequently used operation. The following describes the implementation of oracle re-Indexing in detail, hoping to help you.

Oracle indexes can be re-built in multiple ways, such as drop and re-create, rebuild, and rebuild online. The similarities, differences, and advantages and disadvantages of these methods are compared below:

First, create a test table and data:

 
 
  1. SQL> CREATE TABLE TEST AS SELECT CITYCODE C1 FROM CITIZENINFO2;  
  2.  
  3. Table created  
  4.  
  5. SQL> ALTER TABLE TEST MODIFY C1 NOT NULL;  
  6.  
  7. Table altered  
  8.  
  9. SQL> SELECT COUNT(1) FROM TEST;  
  10.  
  11. COUNT(1)  
  12. ----------  
  13. 16000000  
  14.  

1. drop and re-create and rebuild

First, let's take a look at the locking status of the table when the index is created normally.

 
 
  1. suk@ORACLE9I> @show_sid  
  2.  
  3. SID  
  4. ----------  
  5. 14  
  6.  
  7. suk@ORACLE9I> CREATE INDEX IDX_TEST_C1 ON TEST(C1);  

The index has been created.

 
 
  1. SQL> SELECT OBJECT_NAME,LMODE FROM V$LOCK L,DBA_OBJECTS O WHERE O.OBJECT_ID=L.ID1 AND L.TYPE='TM' AND SID=14;  
  2.  
  3. OBJECT_NAME LMODE  
  4. ------------------------------ ----------  
  5. OBJ$ 3  
  6. TEST 4  

It can be seen that oracle will apply the share lock to the base table when an index is created. Because the share lock and row-X are incompatible, that is, during the index creation process, you cannot perform DML operations on the base table.

The method for deleting and re-indexing is not introduced. It is the same as the above description. Let's take a look at how to create an index using rebuild.

 
 
  1. suk@ORACLE9I> ALTER INDEX IDX_TEST_C1 REBUILD; 

The index has been changed.

Open another session to query the test lock:

 
 
  1. SQL> SELECT OBJECT_NAME,LMODE FROM V$LOCK L,DBA_OBJECTS O WHERE O.OBJECT_ID=L.ID1 AND L.TYPE='TM' AND SID=14;  
  2.  
  3. OBJECT_NAME LMODE  
  4. ------------------------------ ----------  
  5. TEST 4  

It can be seen that the rebuild method locks the base table in the same way as the CREATE method.

Start another session and execute the following SQL statement when the index is being rebuild:

 
 
  1. suk@ORACLE9I> SET AUTOTRACE TRACE  
  2. suk@ORACLE9I> SELECT /*+ INDEX(TEST) */ COUNT(1) FROM TEST WHERE ROWNUM<10; 

Execution Plan

 
 
  1. ----------------------------------------------------------  
  2. SELECT STATEMENT Optimizer=CHOOSE (Cost=26 Card=1)  
  3. 0 SORT (AGGREGATE)  
  4. 1 COUNT (STOPKEY)  
  5. 2 INDEX (FULL SCAN) OF 'IDX_TEST_C1' (NON-UNIQUE) (Cost=  
  6. 26 Card=1986621) 

You can see that the old index can still be used for queries when oracle re-creates an index. In fact, oracle does not delete the old index when creating a new index during rebuild until the new index rebuild is successful.

From this point, we can know that one of the advantages of rebuild over deleting and rebuilding is that it will not affect the original SQL query, but it is also because of this, to create an index using the rebuild method, the free space of the corresponding tablespace is twice that of the deletion and reconstruction method.
 

Using replacement variables to improve Oracle Interaction

ORACLE system table and data dictionary View

Measure the test taker's knowledge about the Oracle file system mechanism.

Detailed explanation of four categories of Oracle index Scanning

Three methods for querying duplicate records in Oracle

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.