Depends on your design
for read only tables and data warehousing go for 100 percent
for heavily used tables (insert and updates) I would keep it at 70-80% and then monitor these tables, check fragmentation levels periodically and defragment if necessary
I think by default SQL server will always keep enough space to insert 1 row if you select 0
In the article, the author mentions that he run the DBCC SHOWCONTIG on his tables, then loads the data into a table for easy reference... is there an easy way to do this?
DBCC SHOWCONTIG normally returns data as messages and so you can't capture that data. However if you use the WITH TABLERESULTS option it will instead return the data as a results set that you can capture:
dbcc showcontig (YourTableName) with tableresults
All you need to do is create a table with the matching columns and then do the following:
declare @command varchar(255)
set @command = 'dbcc showcontig (YourTableName) with tableresults'
insert into YourPreDefinedContigResultsTable
exec (@command)
And voila, the command will be run and its values are inserted into your table instead of being displayed. You can still use all of your other WITH options like ALL_INDEXES or FAST etc.
icemel,
if you lookup dbcc showcontig in BOL you will see the following code that you can use under section E
E. Use DBCC SHOWCONTIG and DBCC INDEXDEFRAG to defragment the indexes in a database
This example shows a simple way to defragment all indexes in a database that is fragmented above a declared threshold.
/*Perform a 'USE <database name>' to select the database in which to run the script.*/
-- Declare variables
SET NOCOUNT ON
DECLARE @tablename VARCHAR (128)
DECLARE @execstr VARCHAR (255)
DECLARE @objectid INT
DECLARE @indexid INT
DECLARE @frag DECIMAL
DECLARE @maxfrag DECIMAL
-- Decide on the maximum fragmentation to allow
SELECT @maxfrag = 30.0
-- Declare cursor
DECLARE tables CURSOR FOR
SELECT TABLE_NAME
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE'
-- Loop through all the tables in the database
FETCH NEXT
FROM tables
INTO @tablename
WHILE @@FETCH_STATUS = 0
BEGIN
-- Do the showcontig of all indexes of the table
INSERT INTO #fraglist
EXEC ('DBCC SHOWCONTIG (''' + @tablename + ''')
WITH FAST, TABLERESULTS, ALL_INDEXES, NO_INFOMSGS')
FETCH NEXT
FROM tables
INTO @tablename
END
-- Close and deallocate the cursor
CLOSE tables
DEALLOCATE tables
-- Declare cursor for list of indexes to be defragged
DECLARE indexes CURSOR FOR
SELECT ObjectName, ObjectId, IndexId, LogicalFrag
FROM #fraglist
WHERE LogicalFrag >= @maxfrag
AND INDEXPROPERTY (ObjectId, IndexName, 'IndexDepth') > 0
-- Open the cursor
OPEN indexes
-- loop through the indexes
FETCH NEXT
FROM indexes
INTO @tablename, @objectid, @indexid, @frag
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.