Methods for rebuilding indexes by user name in an Oracle database

Source: Internet
Author: User
Tags chr oracle database

If you manage some application projects under the Oracle database that have a large number of modifications to delete operations, data indexing needs to be periodically rebuilt.

It can not only improve query performance, but also increase the amount of free space in index table space.

After a large number of records were deleted in Oracle, the block space occupied by the tables and indexes was not released.

Rebuilding an index frees up the data block space used by the deleted records index.

Transfer data, renaming methods can rearrange the data in the table.

The following is a SQL script that can generate a rebuild index by Oracle user name:

SET ECHO off;
SET FEEDBACK off;
SET VERIFY off;
SET PAGESIZE 0;
SET termout on;
SET HEADING off;
ACCEPT username CHAR PROMPT ' Enter the index username: ';
spool/oracle/rebuild_&username.sql;
SELECT
' REM +-----------------------------------------------+ ' | | chr (10) | |
' REM | INDEX NAME: ' | | Owner | | '.' || Segment_name
| | lpad (' | ')-(length (owner) + length (segment_name))
| | chr (10) | |
' REM | BYTES: ' | | Bytes
| | lpad (' | ', 34-(length (bytes)) | | chr (10) | |
' REM | Extents: ' | | Extents
| | lpad (' | ', 34-(length (extents)) | | chr (10) | |
REM +-----------------------------------------------+ ' | | chr (10) | |
' ALTER INDEX ' | |-owner | | '.' || Segment_name | | Chr (10) | |
' REBUILD ' | | chr (10) | |
' tablespace ' | | tablespace_name | | | chr (10) | |
' STORAGE (' | | | chr (10) | | |
' INITIAL ' | | initial_extent | | | chr (10) | |
' NEXT ' | | next_extent | | chr (10) | |
' minextents ' | | | min_extents | | Chr (10) | |
' maxextents ' | | max_extents | | | chr (10) | |
' Pctincrease ' | | pct_increase | | | chr (10) | |
'); ' || Chr (10) | | Chr (a)
from dba_segments
WHERE segment_type = ' INDEX '
and owner= ' &username '
Order by owner, bytes DES C
spool off;

If you are using a Windows system and want to change the storage directory of the output file, modify the path following the spool:

Spool c:\oracle\rebuild_&username.sql;

If you only want to rebuild the index for an index larger than max_bytes, you can modify the SQL statement above:

After and owner= ' &username ', add a restriction condition and bytes> &max_bytes

If you want to modify the index's storage parameters, you can rebuild_&username.sql the rebuild index.

For example, the value of pctincrease not equal to zero is changed to zero.

Generated rebuild_&username.sql files We need to analyze whether they are up to the degree that they need to be rebuilt:

Analyze indexes to see if fragmentation is seriousSQL>ANALYZE INDEX &index_name VALIDATE STRUCTURE;
col name heading 'Index Name' format a30
col del_lf_rows heading 'Deleted|Leaf Rows' format 99999999
col lf_rows_used heading 'Used|Leaf Rows' format 99999999
col ratio heading '% Deleted|Leaf Rows' format 999.99999
SELECT name,
del_lf_rows,
lf_rows - del_lf_rows lf_rows_used,
to_char(del_lf_rows / (lf_rows)*100,'999.99999') ratio
FROM index_stats where name = upper('&index_name');

When the rate of deletion is greater than 15-20%, it is definitely necessary to rebuild the index.

After the deleted Rebuild_&username.sql file we can put it into Oracle's timed work:

For example, one months or two months running during off-peak hours.

If you encounter a ORA-00054 error that indicates that the index has lock information on the table, the index cannot be rebuilt.

Then ignore the mistake and see if it succeeds next time.

For those particularly busy tables to be treated differently, not in the way described here,

They also have to delete their index from the Rebuild_&username.sql.

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.