Oracle rebuild the index of all tables (rebuild index)

Source: Internet
Author: User
Tags goto

CREATE OR REPLACE PROCEDURE p_rebuild_all_index (tablespace_name in VARCHAR2,-this is the table space name, and you can pass in NULL if you do not change the table space
Only_unusable in BOOLEAN)--is only an invalid index operation
As
SQLT VARCHAR (200);
BEGIN
--only non-temporary indexes
For IDX in (SELECT index_name, Tablespace_name, STATUS
From User_indexes
WHERE temporary = ' N ') LOOP
-If an invalid index is rebuilt, and if the index is not invalid, skip
IF only_unusable = TRUE and IDX. STATUS <> ' unusable ' THEN
GOTO CONTINUE;
End IF;

IF (Tablespace_name is NULL) OR IDX. STATUS = ' unusable ' THEN
--If no tablespace is specified, or if the index is not valid, rebuild in the original table space
SQLT: = ' ALTER INDEX ' | | IDX. index_name | | ' REBUILD ';
elsif UPPER (tablespace_name) <> IDX. Tablespace_name THEN
--If a different table space is specified, the index is to be built in the specified table space
SQLT: = ' ALTER INDEX ' | | IDX. index_name | | ' REBUILD tablespace ' | |
Tablespace_name;
ELSE
--If the table space is the same, skip
GOTO CONTINUE;
End IF;

Dbms_output. Put_Line (IDX. INDEX_NAME);
EXECUTE IMMEDIATE sqlt;
<<CONTINUE>>
NULL;
End LOOP;
End;
/*
Function: Rebuilds the index.
Note: If the table space parameter passes in NULL, the index is rebuilt in the original table space, otherwise the index is rebuilt in the destination table space.
Skips if the table space is the same.
Only_unusable indicates whether to rebuild only invalid indexes
Author: 81, June 26, 2007
*/

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.