Drop and Recreate all indexes

Posted on 2005-05-11
Last Modified: 2012-08-13
Greetings, Sybase experts!

We have about 320 indexes or so in a particular database that we need to drop and recreate to clear up some space and to make them "clean" again.

We use Embarcadero's RapidSQL, and what we normally would do is to execute the code that drops and creates each index. But with 320 or so of these, it takes some time. What I'd like to be able to do is to create a stored proc that has the code to drop and recreate all 320 indexes, so we don't have to execute each one by one.

We need to repeat this every couple months or so, so having the code all in one place will be helpful going forward.

Isn't the code stored in a table somewhere? Couldn't I do a select that will dump the index drop and creation code for each index in the database and spool the results, which I could then copy and paste in a new stored proc?

If so, what would the query be? If not, is there any way to do this other than manually copying each code and pasting it into a text file?


Question by:jdrits
    LVL 24

    Accepted Solution

    The "code" per se isn't stored in any system table (unlike for stored procedures and views, where the source code is available), although in principle it can be reverse engineered (which is exactly what RapidSQL and other similar tools do). It's really up to the reverse-engineering code as to whether you get one script per index, one per table, or just one script overall.

    There are any number of ways of doing what you want, but before I discuss them, I should point out that if your Sybase is ASE 11.9.2 or above, and your tables use any of the "data-only locking schemes" (datarows or datapages locking), the "reorg rebuild" command achieves the same results but is faster and doesn't need the same amount of free space as drop & recreate index.

    I should also say in case you didn't know that you should always drop & recreate the clustered index first on any table - and often that's all you need to do, as recreating a clustered index rebuilds all non-clustered indexes on that table anyway.

    But in answer to your specific question, one simple approach is to stick with your current method and build a wrapper script that calls every individual script. You lose a little bit of overhead to connecting, logging in and parsing per index rather than once overall, but given most "create index" commands aren't superfast anyway, this won't cost you all that much.

    Another approach is to change the tool you use to one that's more friendly with its output. I'm very impressed with David Owen's "" which is a platform-independent Perl-based script, available at

    It has options to do exactly what you want - output all drop & recreate indexes into a single script.

    It requires a build of Perl and Michael Peppler's "sybperl" libraries (also linked to from the page given above). It isn't a pretty GUI like RapidSQL, DBArtisan etc, but it's probably the most powerful & flexible available.

    Even Sybase's built-in ASE plugin for Sybase Central can reverse engineer objects and databases, but again I think you don't get all indexes separated out in their own script.

    Good luck!
    LVL 2

    Author Comment

    Thanks, Joe! We actually found a way with the RapidSQL tool to do it, but you've given us some other useful information as well.

    Many thanks!

    LVL 24

    Expert Comment

    by:Joe Woodhouse
    Good on you for figuring it out anyway. Glad I could add something useful!

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
    David Varnum recently wrote up his impressions of PRTG, based on a presentation by my colleague Christian at Tech Field Day at VMworld in Barcelona. Thanks David, for your detailed and honest evaluation!
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now