You are here:   Home
Register   |  Login

Captured Technology - Blog

Minimize

Reindexing SQL Server Tables

Feb 13

Written by:
2/13/2009 11:53 AM  RssIcon

I had the need to reindex all the tables in a SQL Server 2005 database this week and found some very good scripts that loop through the list of user tables using a cursor and reindex each table to improve performance and reduce the size of the data file. In addition I used the dbcc command to shrink the database after the reindexing had completed. Below is the code used.

USE [dbname]

DECLARE @TableName varchar(255)

DECLARE TableCursor CURSOR FOR
SELECT table_name FROM information_schema.tables
WHERE table_type = 'base table'

OPEN TableCursor

FETCH NEXT FROM TableCursor INTO @TableName
WHILE @@FETCH_STATUS = 0
    BEGIN
    DBCC DBREINDEX(@TableName,' ',90)
    FETCH NEXT FROM TableCursor INTO @TableName
    END

CLOSE TableCursor

DEALLOCATE TableCursor

EXEC sp_updatestats

DBCC SHRINKDATABASE ([dbname], 10);
GO

Tags:
Categories:

3 comment(s) so far...


Gravatar

Re: Reindexing SQL Server Tables

Steve, this is old style code. ALTER index is a much better choice in SQL 2005/2008.

Also, why are you shrinking the database after reindexing?

By Michael John on   4/16/2009 10:59 AM
Gravatar

Re: Reindexing SQL Server Tables

I think this code would help a lot in reducing the size of data file which could later be useful and not complicate any process. I will make a try of it.
Thanks for sharing.

By david on   11/23/2009 2:41 AM
Gravatar

Re: Reindexing SQL Server Tables

Discussed about the SQL server and more interesting matter discussed in the post because in the post define about the SQL server table so that can be know that and able to understand regarding post.

By Server Hosting on   2/3/2012 2:15 AM

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Security Code
CAPTCHA image
Enter the code shown above in the box below
Add Comment   Cancel 

FaceBook

Minimize

Mobile Version

Minimize
Add CapturedTech - Technology Mippin widget

Translate

Minimize

Sponsors

Minimize

Recent Comments

Minimize
Re: Three Benefits to Using a Royalty-Free Image on Your Website or Blog
Don't pay for royalty free images. They are free after all! I offer links to 96 websites that have tens of thousands (maybe even hundreds of thousands) of absolutely free images, including ones for commercial use! All you need to do is subscribe to my blog at www.davesinternetmarketing.info/royalty-free-images-free-for-commercial-use/
Re: Buzzom Offers Featured User Options
This is when discovered this great new way to grow your follow list. You can pay between $10 and $100 for the number of times you want to app ar at the top of the list.
Re: Microsoft Test Bounties
I had spent a good deal of my time looking for someone to explain this subject clearly and I have really enjoyed reading your blog posts and I collected a lot of interesting things as well as I done a research on the subject and learn most peoples will agree with your blog.
Re: 10 Ways to Source Great Sales Leads
Hoovers.com, that have contact information for tens of millions of businesses. If necessary, you can work with these companies' research teams to order lists specifically tailored to your organization's sales needs.
Re: Microsoft Test Bounties
To find bugs and showing it to microsoft is quite admirable. Keep blogging more i will appreciate.

u comment, i follow