Solved

SQL Index Fragmentation?

Posted on 2014-10-27
4
233 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 45

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 45

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now