﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / Discuss Content Posted by Nicholas Cain / Article Discussions / Article Discussions by Author  / Checking Your Database Fragmentation Level / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Sun, 19 May 2013 07:29:19 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>It is possible to email using SMTP.Try [url=http://www.sqldev.net/xp/xpsmtp.htm]xpsmtp[/url].We are using it successfully.</description><pubDate>Wed, 21 May 2008 13:08:25 GMT</pubDate><dc:creator>Jason Crider</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>Hello Nicholas,I know this subject was discussed some time ago, but I am new to SQL and was wondering if you had enhanced the original script yet. I am in the process of trying to understand how it works and would value any new information about running on multiple databases. I was also wondering if it is possible to email using SMTP, when you do not have SQL configured to use a mapi applicaton (outlook e.t.c.)I am very new to becoming a SQL DB Admin. I have been around SQL for a while, but never got into the scripting sideAny help appreciated</description><pubDate>Fri, 09 May 2008 04:55:50 GMT</pubDate><dc:creator>Aspurr</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>Below is the correct code, looks like a typo in the article, somewhat embarassing, but no more than MS crashing SQL 2k5 and having to reboot in the NY launch event yesterday.-----------------------------------------------SET QUOTED_IDENTIFIER ONGOSET ANSI_NULLS ONGOCREATE PROCEDURE USP_DBshowcontig_single_db @name varchar(50)ASSET NOCOUNT ON DECLARE @tablename VARCHAR (128)DECLARE @dbname VARCHAR(20)DECLARE @sql VARCHAR(1000)DECLARE @inserttable VARCHAR(3200) -- Create the tableCREATE TABLE #DBFRAGMENT (     ObjectName VARCHAR (50),     ObjectId INT,     IndexName VARCHAR (100),     IndexId INT,     Lvl INT,     CountPages INT,     CountRows INT,     MinRecSize INT,     MaxRecSize INT,     AvgRecSize INT,               ForRecCount INT,             Extents INT,             ExtentSwitches INT,             AvgFreeBytes INT,             AvgPageDensity INT,             ScanDensity DECIMAL,     BestCount INT,             ActualCount INT,             LogicalFrag DECIMAL,             ExtentFrag DECIMAL) create table #tablename (table_name varchar(400)) --DECLARE DB CursorDECLARE databases CURSOR FOR  SELECT NAME    FROM MASTER.DBO.SYSDATABASES   WHERE NAME = @NAME --Open the cursorOPEN databasesFETCH NEXT FROM databases INTO @dbname WHILE @@FETCH_STATUS = 0 BEGIN  set @sql = 'SELECT TABLE_NAME = NAME FROM ' + @dbname + '..SYSOBJECTS WHERE XTYPE =' + '''' + 'U' + ''''  print @sql  insert into #tablename exec(@sql)  -- Declare cursor  DECLARE tables CURSOR FOR      SELECT TABLE_NAME        FROM #tablename  -- Open the cursor  OPEN tables  -- Loop through all the tables in the database  FETCH NEXT FROM tables INTO @tablename  WHILE @@FETCH_STATUS = 0   BEGIN     PRINT @TABLENAME     -- Do the showcontig of all indexes of the table     INSERT INTO #DBFRAGMENT     EXEC ('USE ' + @dbname + ' DBCC SHOWCONTIG (''' + @tablename + ''') WITH TABLERESULTS, ALL_INDEXES, NO_INFOMSGS')     FETCH NEXT FROM tables INTO @tablename   END set @inserttable ='INSERT INTO ADMINDB.DBO.INDEX_INFO_'+@NAME+'(ObjectName,   ObjectId,   IndexName,   IndexId,   Lvl,   CountPages,   CountRows,   MinRecSize,   MaxRecSize,   AvgRecSize,   ForRecCount,   Extents,   ExtentSwitches,   AvgFreeBytes,   AvgPageDensity,   ScanDensity,   BestCount,   ActualCount,   LogicalFrag,   ExtentFrag)  select ObjectName,   ObjectId,   IndexName,   IndexId,   Lvl,   CountPages,   CountRows,   MinRecSize,   MaxRecSize,   AvgRecSize,   ForRecCount,   Extents,   ExtentSwitches,   AvgFreeBytes,   AvgPageDensity,   ScanDensity,   BestCount,   ActualCount,   LogicalFrag,   ExtentFrag  FROM #DBFRAGMENT where ltrim(rtrim(#DBFRAGMENT.indexname))&lt;&gt; '''''   --PRINT @INSERTTABLE  EXEC (@inserttable)   -- Close and deallocate the cursor  CLOSE tables    DEALLOCATE tables    delete from #tablename    delete from #DBFRAGMENT     FETCH NEXT FROM databases INTO @dbname END CLOSE databasesDEALLOCATE databasesdrop table #tablename--Delete the temporary tableDROP TABLE #DBFRAGMENTGOSET QUOTED_IDENTIFIER OFFGOSET ANSI_NULLS ONGO</description><pubDate>Wed, 23 Nov 2005 05:28:00 GMT</pubDate><dc:creator>Nicholas Cain</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>&lt;P&gt; &lt;/P&gt;&lt;P&gt;Still can't get usp_dbshowcontig_single_db to work.  I added the fifth quote but am still getting this error message:  Invalid object name '#tablename' &lt;/P&gt;&lt;P&gt;Has anyone else had this error message and solved the problem?&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Tue, 22 Nov 2005 13:46:00 GMT</pubDate><dc:creator>nmargol</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>&lt;P&gt;Thanks ... i'll see how it goes when i up the backup frequency when i'm reindexing.&lt;/P&gt;&lt;P&gt;Much appreciated,&lt;/P&gt;&lt;P&gt;Stijn&lt;/P&gt;</description><pubDate>Tue, 22 Nov 2005 04:17:00 GMT</pubDate><dc:creator>Stijn Vervaet</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>&lt;P&gt;There are two schools in this matter. Pick what suit you best &lt;img src='images/emotions/smile.gif' height='20' width='20' border='0' title='Smile' align='absmiddle'&gt;&lt;/P&gt;&lt;P&gt;1. Run parallel Tlog backups at the same time the reindexing is happening eg. Run a Tlog backup every 2 min while the reindexing is going on&lt;/P&gt;&lt;P&gt;2. Make a Tlog Backup, Switch the recovery model to Bulk_logged, perform the Reindexing, switch back to Full and when performing the next TLog backup do it on a drive that have plenty of space &lt;img src='images/emotions/wink.gif' height='20' width='20' border='0' title='Wink' align='absmiddle'&gt;&lt;/P&gt;&lt;P&gt;Cheers,&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Mon, 21 Nov 2005 13:58:00 GMT</pubDate><dc:creator>noeld</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>&lt;P&gt;Hi, I am using a similar approach (running a SHOWCONTIG and use the output to decide which indexes need to be defragmented). However, i am always running into rapid growing log files once i start reindexing or DBCC INDEXDEFRAG.&lt;/P&gt;&lt;P&gt;Does anyone know a "safe" way on how to defragment those fragmentated ones without risking blowing up the logs?&lt;/P&gt;&lt;P&gt;Greets,&lt;/P&gt;&lt;P&gt;Stijn&lt;/P&gt;</description><pubDate>Mon, 21 Nov 2005 07:22:00 GMT</pubDate><dc:creator>Stijn Vervaet</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>&lt;P&gt;No luck getting this to output. Eventually got everything to run, but email output simply reads: THETEXT&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Fri, 18 Nov 2005 13:04:00 GMT</pubDate><dc:creator>RML51</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>&lt;P&gt;I'm not a fully fledged DBA, but looking at the scripts I'd assume that a scan density beloew 75 percent is bad.&lt;/P&gt;&lt;P&gt;Ken England has a good book on "Microsoft SQL Server 2000 Performance Optimization and Tuning Handbook" which I'd recommend (ISBN# 1-55558-241-9). And then searching for DBCC SHOWCONTIG in Google should give you some more information at what to look for.&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;hot2use&lt;/P&gt;</description><pubDate>Fri, 18 Nov 2005 08:27:00 GMT</pubDate><dc:creator>hot2use</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>&lt;P&gt;This is fantastic. I have a client who is having fragmentation problems in a heavily used medical records database and this could be just what I need to measure just how quickly the database is becoming fragmented so we can schedule the maintenance plans appropriately.&lt;/P&gt;&lt;P&gt;Thanks a lot!&lt;/P&gt;</description><pubDate>Fri, 18 Nov 2005 08:21:00 GMT</pubDate><dc:creator>Tatsu</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am very new to these things.. can any of you guys tell me how to identify ideal fragmentation level...and how do i identify it.. &lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;Thanks in advance..&lt;/P&gt;&lt;P&gt;Kumar&lt;/P&gt;</description><pubDate>Fri, 18 Nov 2005 08:16:00 GMT</pubDate><dc:creator>kumarkaji@yahoo.com</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>&lt;P&gt;Hiyas.&lt;/P&gt;&lt;P&gt;Had the same problem with the quotes ... noticed in QA that everything was red so I added a fifth quote. &lt;/P&gt;&lt;P&gt;Anyway. Got a small addition here for the same sproc USP_DBshowcontig_single_db :&lt;/P&gt;&lt;P&gt;set @sql = 'SELECT TABLE_NAME = ' + @dbname + '..SYSUSERS.NAME' + ' + ''.'' + ' + '' + @dbname + '..SYSOBJECTS.NAME FROM ' + @dbname + '..SYSOBJECTS JOIN ' + @dbname + '..SYSUSERS ON ' + @dbname + '..SYSOBJECTS.uid = ' + @dbname + '..SYSUSERS.uid WHERE XTYPE =' + '''' + 'U' + ''''&lt;/P&gt;&lt;P&gt;It adds the owner name of the table to TABLE_NAME, which allows you to run the script on databases which have user tables not created by sa.&lt;/P&gt;&lt;P&gt;My piece replaces the line:&lt;/P&gt;&lt;P&gt;set @sql = 'SELECT TABLE_NAME = NAME FROM ' + @dbname + '..SYSOBJECTS WHERE XTYPE =' + '''' + 'U' + ''''&lt;/P&gt;&lt;P&gt;Good luck.&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;hot2use&lt;/P&gt;</description><pubDate>Fri, 18 Nov 2005 03:08:00 GMT</pubDate><dc:creator>hot2use</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>&lt;P&gt;I got it working by adding a 5th single quote to the line:&lt;/P&gt;&lt;P&gt;FROM #DBFRAGMENT where ltrim(rtrim(#DBFRAGMENT.indexname))&amp;lt;&amp;gt; ''''&lt;/P&gt;&lt;P&gt;Dave H&lt;/P&gt;</description><pubDate>Sun, 28 Aug 2005 23:06:00 GMT</pubDate><dc:creator>Dave Henderson</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>Stll not having any luck.</description><pubDate>Mon, 20 Jun 2005 09:17:00 GMT</pubDate><dc:creator>Preston Burroughs</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>I'm also have problems with this stored procedure.  Have you had any luck with it?</description><pubDate>Wed, 15 Jun 2005 12:04:00 GMT</pubDate><dc:creator>Christi Nicholson</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>&lt;P&gt;This was a great article.  I tried implementing, but have been having problems running the stored procedure USP_DBshowcontig_single_db.  #table# name error.  I have been having a provlem with this part of the stored procedure.&lt;/P&gt;&lt;P&gt;set @inserttable = ('INSERT INTO &lt;A href="mailto:ADMINDB.DBO.INDEX_INFO_.'+@NAME"&gt;ADMINDB.DBO.INDEX_INFO_.'+@NAME&lt;/A&gt;+ 'ObjectName,   ObjectId,   IndexName,   IndexId,   Lvl,   CountPages,   CountRows,   MinRecSize,   MaxRecSize,   AvgRecSize,   ForRecCount,   Extents,   ExtentSwitches,   AvgFreeBytes,   AvgPageDensity,   ScanDensity,   BestCount,   ActualCount,   LogicalFrag,   ExtentFrag'&lt;/P&gt;&lt;P&gt; select ObjectName,   ObjectId,   IndexName,   IndexId,   Lvl,   CountPages,   CountRows,   MinRecSize,   MaxRecSize,   AvgRecSize,   ForRecCount,   Extents,   ExtentSwitches,   AvgFreeBytes,   AvgPageDensity,   ScanDensity,   BestCount,   ActualCount,   LogicalFrag,   ExtentFrag  FROM #DBFRAGMENT where ltrim(rtrim(#DBFRAGMENT.indexname))&amp;lt;&amp;gt; ''''&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Thu, 09 Jun 2005 10:12:00 GMT</pubDate><dc:creator>Preston Burroughs</dc:creator></item><item><title>RE: Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>&lt;P&gt;Hi Nicholas,&lt;/P&gt;&lt;P&gt;Nice article, I enjoyed reading it. &lt;/P&gt;&lt;P&gt;BTW, what is the benifit of using the dts vs scheduling these steps from a job? I am assuming both will be done by someone with dba rights.&lt;/P&gt;&lt;P&gt;Thanks again&lt;/P&gt;&lt;P&gt; &lt;/P&gt;</description><pubDate>Mon, 06 Jun 2005 14:59:00 GMT</pubDate><dc:creator>Bikeman</dc:creator></item><item><title>Checking Your Database Fragmentation Level</title><link>http://www.sqlservercentral.com/Forums/Topic119100-174-1.aspx</link><description>Comments posted to this topic are about the content posted at &lt;A HREF=http://www.sqlservercentral.com/columnists/ncain/checkingyourdatabasefragmentationlevel.asp&gt;http://www.sqlservercentral.com/column</description><pubDate>Thu, 03 Jun 2004 10:20:00 GMT</pubDate><dc:creator>Nicholas Cain</dc:creator></item></channel></rss>