Posts

Showing posts with the label indexes

Defrag Your Indexes Script - Version 1.1

As it's getting kinda large now, version 1.1. of the script is attached as a link on the right. Constructive comments/suggestions welcomed. Also, let me know if it's useful for you. :)

Yay - It works! Rebuild/Reorganize all indexes in one go - woo hoo

/*Version 1 of my script to fix all fragged indexes on a database */ /*Must be logged in as [sa] for this to work, if not, you will get permissions errors*/ /*You should be able to copy and paste this script in its entirity to your SQL 2005 or higher query window and run it without altering anything other than the USE .*/ /*Perform a 'USE ' to select the database in which to run the script.*/ USE AdventureWorks GO -- Make sure temp table doesn't exist IF EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[tmp_fraglist]') AND type in (N'U')) DROP TABLE [dbo].[tmp_fraglist] GO -- Declare variables SET NOCOUNT ON DECLARE @tablename NVARCHAR (255) DECLARE @indexname NVARCHAR (255) DECLARE @indextype NVARCHAR (255) DECLARE @execstr NVARCHAR (255) DECLARE @pagecounts INT DECLARE @frag DECIMAL DECLARE @reorgFragSize DECIMAL DECLARE @rebldFragSize DECIMAL DECLARE @numrecords INT DECLARE @rowcount INT DECLARE @errorMessage ...