Every once in a while it is a good idea to re-index / re-organize your SQL server tables, but if you are having a problem with a particular table then you may want to track the degree of your defragmented indexes.
Using this small snippet of code below can show you if you are having some issues.
This article does not address whether you should rebuild or reorganize and the many thoughts behind which one you should consider based on results. You could actually extend this snippet to conditionally update or reorganize your indexes based on upper and lower range.
Save this script somewhere because you will need it one day.
[code language=”sql”]<br />
USE [DatabaseName];<br />
GO<br />
— Find the average fragmentation percentage of all indexes in a particular table<br />
SELECT ps.index_id,<br />
[name] as index_name,<br />
avg_fragmentation_in_percent<br />
FROM sys.dm_db_index_physical_stats (DB_ID(N'[DatabaseName]’), OBJECT_ID(N'[TableName]’), NULL, NULL, NULL) AS ps<br />
JOIN sys.indexes AS indx ON ps.object_id = indx.object_id AND ps.index_id = indx.index_id;<br />
GO<br />
[/code]
Looking for quality web hosting? Look no further than Arvixe Web Hosting!