473,594 Members | 2,839 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Script to Rebuild the indexes that really needs to be rebuild .

debasisdas
8,127 Recognized Expert Expert
This code genenates a script to rebuld all the indexes with height greater than 3 for all the users except for SYS user. The script needs to be executed from SYS schema. This can also be executed from individual schemas by minor changes. If you need to exclude indxes of some other users that can also be added/changed in cGetIdx cursor. To execute this code from any schema other than SYS. Execute on DBMS_SQL must be granted from SYS(not system) to the the specified user.

Indexes with height less than three (3) should not be rebuilded. If you want to go for indexes with less heights that can also be customized by changing the height parameter.

Set serveroutput on size 100000
Expand|Select|Wrap|Line Numbers
  1. DECLARE
  2.   vOwner   dba_indexes.owner%TYPE;            /* Index Owner            */
  3.   vIdxName dba_indexes.index_name%TYPE;       /* Index Name             */
  4.   vAnalyze VARCHAR2(100);                     /* String of Analyze Stmt */
  5.   vCursor  NUMBER;                            /* DBMS_SQL cursor        */
  6.   vNumRows INTEGER;                           /* DBMS_SQL return rows   */
  7.   vHeight  index_stats.height%TYPE;           /* Height of index tree   */
  8.   vLfRows  index_stats.lf_rows%TYPE;          /* Index Leaf Rows        */
  9.   vDLfRows index_stats.del_lf_rows%TYPE;      /* Deleted Leaf Rows      */
  10.   vDLfPerc   NUMBER;                          /* Del lf Percentage      */
  11.   vMaxHeight NUMBER;                          /* Max tree height        */
  12.   vMaxDel    NUMBER;                          /* Max del lf percentage  */
  13.   CURSOR cGetIdx IS SELECT owner,index_name
  14.      FROM dba_indexes WHERE OWNER NOT LIKE 'SYS%';
  15. BEGIN
  16.   /* Define maximums. This section can be customized. */
  17.   vMaxHeight := 3;
  18.   vMaxDel    := 20;
  19.  
  20.   /* For every index, validate structure */
  21.   OPEN cGetIdx;
  22.   LOOP
  23.      FETCH cGetIdx INTO vOwner,vIdxName;
  24.      EXIT WHEN cGetIdx%NOTFOUND;
  25.      /* Open DBMS_SQL cursor */
  26.      vCursor := DBMS_SQL.OPEN_CURSOR;
  27.      /* Set up dynamic string to validate structure */
  28.      vAnalyze := 'ANALYZE INDEX ' || vOwner || '.' || vIdxName || ' VALIDATE STRUCTURE';
  29.      DBMS_SQL.PARSE(vCursor,vAnalyze,DBMS_SQL.V7);
  30.      vNumRows := DBMS_SQL.EXECUTE(vCursor);
  31.      /* Close DBMS_SQL cursor */
  32.      DBMS_SQL.CLOSE_CURSOR(vCursor);
  33.      /* Does index need rebuilding?  */
  34.      /* If so, then generate command */
  35.      SELECT height,lf_rows,del_lf_rows INTO vHeight,vLfRows,vDLfRows
  36.         FROM INDEX_STATS; 
  37.      IF vDLfRows = 0 THEN         /* handle case where div by zero */
  38.         vDLfPerc := 0;
  39.      ELSE
  40.         vDLfPerc := (vDLfRows / vLfRows) * 100;
  41.      END IF;
  42.      IF (vHeight > vMaxHeight) OR (vDLfPerc > vMaxDel) THEN 
  43.         DBMS_OUTPUT.PUT_LINE('ALTER INDEX ' || vOwner || '.' || vIdxName || ' REBUILD;');
  44.      END IF;
  45.  
  46.   END LOOP;
  47.   CLOSE cGetIdx;
  48. END;
Aug 1 '08 #1
0 17462

Sign in to post your reply or Sign up for a free account.

Similar topics

10
3086
by: Don | last post by:
I want the server-side php script to return a browser page that is essentially a copy of the original client page that contained the <form> which referenced the php script in the first place. However, the php script will need to change a couple html lines in that returned page. I sure could you some help on how to go about this. Thanks in advance, Don ----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
1
4750
by: xo55ox | last post by:
Hi, I want to find out what is a good way to identify indexes that need rebuilding. I tried to run DBCC showcontig and identify them based on the scan density %. And according to a Microsoft Tech Net - All About SQL Server Indexes discussion, the expert stated that based on the Density (below 20%), I can pick out those indexes associated to the table and run DBCC INDEXDEFRAG. But instead, I ran DBCC DBREINDEX to rebuild all indexes...
3
3115
by: Matt | last post by:
Hello I am trying to rebuild indexes on our DB, but I am running into an error I am not able to solve. Server: Msg 1105, Level 17, State 2, Line 1 Could not allocate space for object 'ft' in database 'HastaDemo' because the 'PRIMARY' filegroup is full. The statement has been terminated.
4
1637
by: trint | last post by:
Ok, This script is something I wrote for bringing up a report in reporting services and it is really slow...Is their any problems with it or is their better syntax to speed it up and still provide the same report results?: SELECT t1.MemberId, t1.PeriodID, t8.start_date, t6.amount_type_id, t6.amount_type, SUM(CASE WHEN t2.amountTypeId = 7 THEN t2.amount WHEN t2.amountTypeId = 23 THEN - t2.amount END) AS Purchase,
10
8874
by: wackyphill | last post by:
After rebuilding an index, it still shows as the same amount of fragmentation. ANy ideas what's wrong? I'm determining which indexes to rebuild using the following query: SELECT OBJECT_NAME(i.object_id) AS TableName, i.name AS IndexName, ips.avg_fragmentation_in_percent FROM sys.dm_db_index_physical_stats(DB_ID(), NULL, NULL, NULL,
1
3234
by: Dhungu Dhumbak | last post by:
Dear All, SOS Please Help. I have a MS-SQL DB with 4 .ndf files. One (first) .ndf file is missing. somehow got deleted??. Is there any way can rebuild my DB. The .MDF and .LDF files are in tact. Please help asap. Dhumbak
1
1546
by: digitalox | last post by:
All efforts to rebuild indexes fails. Not with an error, but they still show high fragmentation. The environment: SQL Server 2005 in 2000 compatibility mode non-clustered indexes on tables that have clustered indexes with unique constraint Tried so far: Rebuild, reorganize, manual drop and recreate, setting the db in 2005 mode This only seems to happen on the tables that also have the clustered index with a unique contraint ( which...
1
7166
by: dsdevonsomer | last post by:
Hello all, I am new to managing indexes on large tables and need some help. Hopefully, I am not repeating question here. I searched as much as I can, but not finding relatively best answer.. Here is my scenario. I have 2 tables with more than 4 mil rows (1 - 2.8 Mil, 2 2.1 mil). Of these tables, there are about 25 ( select ) queries run to help generate reports every week.
16
4828
by: Okonita via DBMonster.com | last post by:
Hi all, I am comming along with all this Linus/DB2/scripting business...I am no longer scared of it!! (LOL). But, I need to create a .ksh script that does a REORGCHK and output only tables recommended for reorg. My goal is to reorgchk and run reorgs based on entries in this reorg file as shown in the example below. I have tried my hand at the following failing script and hope that gurus here can throw me a lifeline of examples on how to...
0
7946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8253
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8374
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8009
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6661
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5739
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
3867
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3903
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2389
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.