Solved

SQL Index Fragmentation?

Posted on 2014-10-27
4
240 Views
Last Modified: 2014-10-27
Hi,

I have some fragmented indexes in SQL 2008 R2.

I have been reindexing them with:

DBCC DBREINDEX

or command like:

alter index ... on dbo... REBUILD

anyway not all indexes seem to reduce in fragmentation to 0.

So I am curious have I got an unreasonable expectation here?

what should I expect the outcome to be? And why might some
not reduce to 0?

btw here is the SQL I am running to work out fragmentation:

declare @current_time as nvarchar(20)

set @current_time = convert(varchar, getdate(), 3) + ' ' + convert(varchar, getdate(), 24)

SELECT @current_time as Timestamp,
DB_NAME(database_id) AS [Database Name], OBJECT_NAME(ps.OBJECT_ID) AS [Object Name],
i.name AS [Index Name], ps.index_id, index_type_desc,
avg_fragmentation_in_percent, fragment_count, page_count
FROM sys.dm_db_index_physical_stats(DB_ID(),NULL, NULL, NULL ,'LIMITED') AS ps
INNER JOIN sys.indexes AS i WITH (NOLOCK)
ON ps.[object_id] = i.[object_id] AND ps.index_id = i.index_id
WHERE database_id = DB_ID()
AND page_count > 1500
ORDER BY OBJECT_NAME(ps.OBJECT_ID), avg_fragmentation_in_percent DESC OPTION (RECOMPILE);

Thanks,

Ward.
0
Comment
Question by:whorsfall
  • 2
  • 2
4 Comments
 
LVL 48

Expert Comment

by:Vitor Montalvão
ID: 40406129
Your script it's only taking in consideration the page count (1500+) but Microsoft's recommendation is to check for fragmentation level. Here's the MSDN article about it where you'll see that a reindex should be performed when fragmentation level between 5% and 30% and a rebuild should be performed when fragmentation's higher than 30%.
0
 

Author Comment

by:whorsfall
ID: 40406204
Hi,

Thanks for that so I can assume after a rebuild then the fragmentation should be 0?

Plus assuming I get all the tables by changing my query.

And what about the command dbcc dbreindex?

Thanks,

Ward
0
 
LVL 48

Accepted Solution

by:
Vitor Montalvão earned 500 total points
ID: 40406218
You can't assume a fragmentation level of 0%. That depends if there's no records changes (INSERT, UPDATE and DELETE) during or immediately after the reindex. But it's almost sure that you can assume a fragmentation lower than 5%.

Don't use DBCC REINDEX because will be deprecated in the next SQL Server version. Use ALTER INDEX instead, with REORGANIZE option for avg_fragmentation_in_percent between 5% and 30% or with REBUILD option for avg_fragmentation_in_percent > 30%.
0
 

Author Comment

by:whorsfall
ID: 40406224
Hi,

Thanks for the clarification that was great :)

Ward
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

828 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