SQL Server 2012, rebuild not lowering avg fragmentation I have a script identifying what indices to rebuild. 

select 
    'alter index ' + name + ' on ' + @dbname + '.dbo.' + OBJECT_NAME(a.object_id) + ' rebuild;'
from sys.dm_db_index_physical_stats (DB_ID(@dbname), NULL, NULL, NULL, NULL) AS a
inner join sys.indexes AS b ON a.object_id = b.object_id AND a.index_id = b.index_id
where avg_fragmentation_in_percent &gt; 30


Which, e.g generates:

alter index FooIndex on FooDb.dbo.FooTable rebuild;


But after I execute the alter statement the index still has a high fragmentation value and if I execute it again it don't get any lower (50%). Any input on what could be wrong would be highly appreciated.

UPDATED: I manually increased the size of the DB which managed to lower the fragmentation on some of the indices, but still not all. Still have a couple around 50%.

//Daniel
Is it a small index? Small indexes will always show high levels of fragmentation. You can read more at Ola Hallengren's site which has a highly rated SQL Server maintenance package - look at the PageCountLevel setting, which specifies that Microsoft recommends leaving indexes under 1000 pages - with the link.

http://ola.hallengren.com/sql-server-index-and-statistics-maintenance.html