Rebuilding Indexes PL/SQL Script
DBAsupport.com Forums - Powered by vBulletin
Page 1 of 3 123 LastLast
Results 1 to 10 of 26

Thread: Rebuilding Indexes PL/SQL Script

Hybrid View

  1. #1
    Join Date
    Apr 2008
    Posts
    6

    Rebuilding Indexes PL/SQL Script

    Hey Guys,

    I created Script that will rebuild indexes per schema. I wanted to post for others to enjoy.

    BEGIN
    FOR INDEx_RECORD IN (select 'SCHEMA_NAME.'||object_name as obj
    from dba_objects
    where object_type = 'INDEX' and
    owner ='SCHEMA_NAME'
    ) LOOP
    execute immediate 'ALTER INDEX '||INDEx_RECORD.obj||' REBUILD';
    END LOOP; -- implicit CLOSE occurs
    END;

  2. #2
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,554
    Thank you very much, I'm sure somebody will really enjoy it.
    so...
    ... why are you rebuilding all indexes on the schema? any special reason?
    ... since you are not doing rebuild online you are going to certainly affect business.
    ... how about partitioned indexes?
    ... how about statistics?
    ... have a couple more but I'll wait until you get these solved
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  3. #3
    Join Date
    Sep 2002
    Location
    England
    Posts
    7,333
    Quote Originally Posted by hozay
    Hey Guys,

    I created Script that will rebuild indexes per schema. I wanted to post for others to enjoy.

    BEGIN
    FOR INDEx_RECORD IN (select 'SCHEMA_NAME.'||object_name as obj
    from dba_objects
    where object_type = 'INDEX' and
    owner ='SCHEMA_NAME'
    ) LOOP
    execute immediate 'ALTER INDEX '||INDEx_RECORD.obj||' REBUILD';
    END LOOP; -- implicit CLOSE occurs
    END;
    what a dangerous script to post - i hope you feel ashamed

  4. #4
    Join Date
    Apr 2008
    Posts
    6
    I have an application that likes to delete 36 million rows out of the database per week. It's not a well written app. I really did not want to resort to rebuilding indexes but i seem to have no choice. We are not up 24 x 7. This is done with no users on. So yes please be careful. If you have any better ideas I'm certainly open to hear them.

    Thanks

    Hoz

  5. #5
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,554
    Quote Originally Posted by hozay
    I have an application that likes to delete 36 million rows out of the database per week.
    36 million rows deleted a week doesn't sound like whole bunch of them, either you are talking about a very small database or you are just panicking.

    Quote Originally Posted by hozay
    I really did not want to resort to rebuilding indexes but i seem to have no choice
    So... why are you doing it?

    Quote Originally Posted by hozay
    If you have any better ideas I'm certainly open to hear them.
    Do not give the same medicine -specially when you are using a dangerous one- to all your patients; give it only to the ones that need it.
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  6. #6
    Join Date
    Apr 2008
    Posts
    6
    There are many queries that go from running in 15mins to 6 hours after about 10 days. The queries are all based on joining of many views. Each of those views many up of many extremely wide tables.

    I took a close look at one particular query. First, I tried to make sure I had updated statistics. No affect. Next, I rebuilt the indexes for that query. It returned to a 15min query.

    I rebuilt indexes once a week for 3 weeks. The query ran consistently in 15 min. Then I rebuilt indexes for the entire schema. All queries run consistently in 15min.

    There is nothing I can do about the SQL. I've been told it cannot be changed.

    Database is size 150gb.


    Hoz

  7. #7
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,554
    Quote Originally Posted by hozay
    I took a close look at one particular query.
    Do you still have the trace? Could you please post Wait Events section of it?
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  8. #8
    Join Date
    Apr 2008
    Posts
    6
    I don't have a trace file I can post. The wait event was a db sequential read.

  9. #9
    Join Date
    Dec 2002
    Location
    Bangalore ( India )
    Posts
    2,434
    looks like you (Hoz) got into wrong site? lemme make a guess, this basic knowledge of rebuild indexes coming from Don B?
    funky...

    "I Dont Want To Follow A Path, I would Rather Go Where There Is No Path And Leave A Trail."

    "Ego is the worst thing many have, try to overcome it & you will be the best, if not good, person on this earth"

  10. #10
    Join Date
    Dec 2002
    Location
    Bangalore ( India )
    Posts
    2,434
    I keep on thinking, what a hot topic this is... rebuild index?.. or it looks like so.. lol
    funky...

    "I Dont Want To Follow A Path, I would Rather Go Where There Is No Path And Leave A Trail."

    "Ego is the worst thing many have, try to overcome it & you will be the best, if not good, person on this earth"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width