<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Indexes &#8211; SQLpowered.com</title>
	<atom:link href="https://sqlpowered.com/script-category/indexes/feed/" rel="self" type="application/rss+xml" />
	<link>https://sqlpowered.com</link>
	<description>SQL Server + BI</description>
	<lastBuildDate>Sun, 18 Jan 2026 11:45:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://sqlpowered.com/wp-content/uploads/2020/07/FavIcon-e1594067873682-99x100.png</url>
	<title>Indexes &#8211; SQLpowered.com</title>
	<link>https://sqlpowered.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Rebuild all indexes in database with fragmentation level and pages count set</title>
		<link>https://sqlpowered.com/script/rebuild-all-indexes-with-fragmentation-level-and-pages-count-set/</link>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Sun, 18 Jan 2026 11:44:56 +0000</pubDate>
				<guid isPermaLink="false">https://sqlpowered.com/?post_type=script&#038;p=5782</guid>

					<description><![CDATA[SET NOCOUNT ON DROP TABLE IF EXISTS [#Fragmented_Indexes] DECLARE @SchemaName sysname, @TableName sysname, @IndexName sysname, @AvgFragmentationInPercent INT, @PageCount INT, @Fragmentation INT, @Stmt NVARCHAR(MAX); SET @PageCount = 1000 SET @AvgFragmentationInPercent = 10 SELECT [s].[name] AS [SchemaName], [t].[name] AS [TableName], [i].[name] AS [IndexName], [ps].[avg_fragmentation_in_percent] [Fragmentation] INTO [#Fragmented_Indexes] FROM [sys].[tables] [t] INNER JOIN...]]></description>
										<content:encoded><![CDATA[<pre class="EnlighterJSRAW" data-enlighter-language="sql">SET NOCOUNT ON

DROP TABLE IF EXISTS [#Fragmented_Indexes]

DECLARE
	@SchemaName sysname,
	@TableName  sysname,
	@IndexName  sysname,
	@AvgFragmentationInPercent INT,
	@PageCount INT,
	@Fragmentation INT,
	@Stmt		NVARCHAR(MAX);

SET @PageCount = 1000
SET @AvgFragmentationInPercent = 10

SELECT
	[s].[name]  AS [SchemaName], [t].[name]  AS [TableName], [i].[name] AS [IndexName], [ps].[avg_fragmentation_in_percent] [Fragmentation]
	INTO [#Fragmented_Indexes]
FROM [sys].[tables] [t]
	INNER JOIN [sys].[schemas] [s] ON [s].[schema_id] = [t].[schema_id]
	INNER JOIN [sys].[indexes] [i] ON [i].[object_id] = [t].[object_id]
	CROSS APPLY [sys].dm_db_index_physical_stats(DB_ID(), [t].[object_id], [i].[index_id], NULL, 'SAMPLED') [ps]
WHERE [i].[index_id] &gt; 0 AND [i].[is_disabled] = 0 AND [i].[is_hypothetical] = 0 AND [ps].[alloc_unit_type_desc] = 'IN_ROW_DATA' AND
	  [ps].[avg_fragmentation_in_percent] &gt; @AvgFragmentationInPercent AND
	  [ps].[page_count] &gt; @PageCount

RAISERROR('%i indexes to be rebuilded', 10, 1, @@ROWCOUNT) WITH NOWAIT

WHILE EXISTS (SELECT * FROM [#Fragmented_Indexes])
BEGIN
	
	SELECT TOP(1)
		@SchemaName = [SchemaName],
		@TableName = [TableName],
		@IndexName = [IndexName],
		@Fragmentation = [Fragmentation]
	FROM [#Fragmented_Indexes]
	ORDER BY [SchemaName], [TableName]

	SET @Stmt =
		N'ALTER INDEX ' + QUOTENAME(@IndexName) +
		N' ON ' + QUOTENAME(@SchemaName) + N'.' + QUOTENAME(@TableName) + 
		N'REBUILD;'
  
	RAISERROR('%s.%s =&gt; %s =&gt; %i to %i', 10, 1, @SchemaName, @TableName, @IndexName, @Fragmentation, @AvgFragmentationInPercent) WITH NOWAIT

	EXEC [sys].[sp_executesql] @Stmt;
	
	DELETE FROM [#Fragmented_Indexes] WHERE [SchemaName] = @SchemaName AND [TableName] = @TableName AND [IndexName] = @IndexName

END
GO</pre>
<p>&nbsp;</p>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Rebuilding all indexes in database and statistics update</title>
		<link>https://sqlpowered.com/script/rebuilding-all-indexes-in-database-and-statistics-update/</link>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Fri, 09 Mar 2018 14:09:29 +0000</pubDate>
				<guid isPermaLink="false">https://sqlpowered.com/?post_type=script&#038;p=2394</guid>

					<description><![CDATA[EXEC sp_MSforeachtable @command1 = "print '?' DBCC DBREINDEX ('?', ' ', 50)" GO EXEC sp_updatestats GO]]></description>
										<content:encoded><![CDATA[<pre class="EnlighterJSRAW" data-enlighter-language="sql">EXEC sp_MSforeachtable @command1 = "print '?' DBCC DBREINDEX ('?', ' ', 50)"
GO
EXEC sp_updatestats
GO</pre>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Rebuild all indexes on table</title>
		<link>https://sqlpowered.com/script/rebuild-all-indexes-on-table/</link>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Fri, 09 Mar 2018 14:07:23 +0000</pubDate>
				<guid isPermaLink="false">https://sqlpowered.com/?post_type=script&#038;p=2391</guid>

					<description><![CDATA[ALTER INDEX ALL ON dbo.SampleTable REBUILD WITH (FILLFACTOR = 70)]]></description>
										<content:encoded><![CDATA[<pre class="EnlighterJSRAW" data-enlighter-language="sql">ALTER INDEX ALL ON dbo.SampleTable REBUILD WITH (FILLFACTOR = 70)</pre>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Rebuilding all indexes in database (cursor version)</title>
		<link>https://sqlpowered.com/script/rebuilding-all-indexes-in-database-cursor-version-fillfactor/</link>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Fri, 09 Mar 2018 11:57:07 +0000</pubDate>
				<guid isPermaLink="false">https://sqlpowered.com/?post_type=script&#038;p=2374</guid>

					<description><![CDATA[Script to rebuild all indexes in the database. Indexes are iterated using the cursor and so it can be easily extended with other features. DECLARE @Database NVARCHAR(128) DECLARE @Table NVARCHAR(128) DECLARE @Stmt NVARCHAR(MAX) DECLARE curTable CURSOR FOR SELECT '[' + sch.name + '].[' + t.name + ']' as TableName FROM...]]></description>
										<content:encoded><![CDATA[<p>Script to rebuild all indexes in the database. Indexes are iterated using the cursor and so it can be easily extended with other features.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="sql">DECLARE @Database NVARCHAR(128)   
DECLARE @Table NVARCHAR(128)  
DECLARE @Stmt NVARCHAR(MAX)  
 
DECLARE curTable CURSOR FOR 
    SELECT '[' + sch.name + '].[' + t.name + ']' as TableName   
    FROM sys.tables t
		inner join sys.schemas sch on sch.schema_id = t.schema_id
	ORDER BY t.name
   
OPEN curTable   
FETCH NEXT FROM curTable INTO @Table   
WHILE @@FETCH_STATUS = 0 
BEGIN   
        
	PRINT '--&gt;' + @Table
 
	SET @Stmt = 'ALTER INDEX ALL ON ' + @Table + ' REBUILD;'
        
	PRINT @Stmt
	EXECUTE(@Stmt)  
 
	FETCH NEXT FROM curTable INTO @Table   
END   
 
CLOSE curTable  
DEALLOCATE curTable  
GO</pre>
<p>Same version as the above on but it also includes the option to set different <em>fillfactor</em>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="sql">DECLARE @Database NVARCHAR(128)   
DECLARE @Table NVARCHAR(128)  
DECLARE @FillFactor INT 
DECLARE @Stmt NVARCHAR(MAX)  
 
SET @FillFactor = 100
 
DECLARE curTable CURSOR FOR 
    SELECT '[' + sch.name + '].[' + t.name + ']' as TableName   
    FROM sys.tables t
		inner join sys.schemas sch on sch.schema_id = t.schema_id
	ORDER BY t.name
   
OPEN curTable   
FETCH NEXT FROM curTable INTO @Table   
WHILE @@FETCH_STATUS = 0 
BEGIN   
        
	PRINT '--&gt;' + @Table
 
	SET @Stmt = 'ALTER INDEX ALL ON ' + @Table + ' REBUILD ' +
				'WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3), @FillFactor) + ')'  
        
	PRINT @Stmt
	EXECUTE(@Stmt)  
 
	FETCH NEXT FROM curTable INTO @Table   
END   
 
CLOSE curTable  
DEALLOCATE curTable  
GO</pre>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Generate indexes REBUILD/REORGANIZE script based on fragmentation level</title>
		<link>https://sqlpowered.com/script/generate-indexes-rebuild-reorganize-script-based-on-fragmentation-level/</link>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Fri, 09 Mar 2018 08:15:48 +0000</pubDate>
				<guid isPermaLink="false">https://sqlpowered.com/?post_type=script&#038;p=2365</guid>

					<description><![CDATA[SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED SELECT CONCAT('/* ', [n].[SchemaName], '.', [n].[TableName], ' (', [d].[record_count], ') [', [d].[page_count] , '] */ ') IndexInfo, CONCAT ('ALTER INDEX ', QUOTENAME([i].[name]), ' ON ', QUOTENAME([n].[SchemaName]), '.', QUOTENAME([n].[TableName]), CASE WHEN [d].[avg_fragmentation_in_percent] &#62; 30 THEN ' REBUILD WITH (ONLINE=ON);' ELSE ' REORGANIZE;' END), ISNULL( [i].[name],...]]></description>
										<content:encoded><![CDATA[<pre class="EnlighterJSRAW" data-enlighter-language="sql">SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED

SELECT 
    CONCAT('/* ', [n].[SchemaName], '.', [n].[TableName], ' (', [d].[record_count], ') [', [d].[page_count] , '] */ ') IndexInfo,
    CONCAT ('ALTER INDEX ', QUOTENAME([i].[name]), ' ON ', QUOTENAME([n].[SchemaName]), '.', QUOTENAME([n].[TableName]),
			 CASE 
                WHEN [d].[avg_fragmentation_in_percent] &gt; 30 THEN ' REBUILD WITH (ONLINE=ON);'
                ELSE ' REORGANIZE;'
            END),           
    ISNULL( [i].[name], '{HEAP}' ) [IndexName],
    [d].[avg_fragmentation_in_percent] [AvgFragmentationPercent], 
    [d].[page_count] [PageCount],     
    [d].[record_count] [RowCount]
FROM [sys].[dm_db_index_physical_stats](DB_ID(), NULL, NULL, NULL, 'SAMPLED') [d]
	INNER JOIN [sys].[objects] [o] ON [o].[object_id] = [d].[object_id]
	INNER JOIN [sys].[indexes] [i] ON [i].[object_id] = [o].[object_id] AND [i].[index_id] = [d].[index_id]
	OUTER APPLY	( SELECT OBJECT_SCHEMA_NAME([d].[object_id], [d].[database_id]) [SchemaName],
						 OBJECT_NAME([d].[object_id], [d].[database_id]) [TableName] ) [n]
WHERE 
	[d].[database_id] = DB_ID() AND 
	[d].[page_count] &gt; 64 AND 
	[d].[avg_fragmentation_in_percent] &gt; 5
ORDER BY 
	OBJECT_SCHEMA_NAME([d].[object_id], [d].[database_id]),
	OBJECT_NAME([d].[object_id], [d].[database_id])</pre>
]]></content:encoded>
					
		
		
			</item>
		<item>
		<title>Missing indexes by average estimated impact</title>
		<link>https://sqlpowered.com/script/missing-indexes-by-average-estimated-impact/</link>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Fri, 09 Mar 2018 00:01:01 +0000</pubDate>
				<guid isPermaLink="false">https://sqlpowered.com/?post_type=script&#038;p=2364</guid>

					<description><![CDATA[SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED GO SELECT [migs].[avg_user_impact] * ( [migs].[user_seeks] + [migs].[user_scans] ) [avg_estimated_impact], [user_scans], [user_seeks], [migs].[last_user_seek] [last_user_seek], OBJECT_NAME([mid].[object_id], [mid].[database_id]) [table_name], 'CREATE INDEX [IX_' + OBJECT_NAME([mid].[object_id], [mid].[database_id]) + '_' + REPLACE(REPLACE(REPLACE(ISNULL([mid].[equality_columns], ''), ', ', '_'), '[', ''), ']', '') + CASE WHEN [mid].[equality_columns] IS NOT NULL AND [mid].[inequality_columns]...]]></description>
										<content:encoded><![CDATA[<pre class="EnlighterJSRAW" data-enlighter-language="sql">SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
GO

SELECT
	[migs].[avg_user_impact] * ( [migs].[user_seeks] + [migs].[user_scans] ) [avg_estimated_impact],
	[user_scans], [user_seeks], [migs].[last_user_seek] [last_user_seek],
	OBJECT_NAME([mid].[object_id], [mid].[database_id]) [table_name],
	'CREATE INDEX [IX_' + OBJECT_NAME([mid].[object_id], [mid].[database_id]) + '_' + 
	REPLACE(REPLACE(REPLACE(ISNULL([mid].[equality_columns], ''), ', ', '_'), '[', ''), ']', '')  + 
	CASE WHEN [mid].[equality_columns] IS NOT NULL AND [mid].[inequality_columns] IS NOT NULL THEN '_'  ELSE '' END + 
	REPLACE(REPLACE(REPLACE(ISNULL([mid].[inequality_columns], ''), ', ', '_'), '[', ''), ']', '') + ']' +
	' ON ' + [mid].[statement] + ' (' + ISNULL([mid].[equality_columns], '') + 
	CASE WHEN [mid].[equality_columns] IS NOT NULL AND [mid].[inequality_columns] IS NOT NULL THEN ',' ELSE '' END + 
	ISNULL([mid].[inequality_columns], '') + ')' + ISNULL(' INCLUDE (' + [mid].[included_columns] + ')', '') [create_statement]
FROM    [sys].[dm_db_missing_index_groups] [mig]
	INNER JOIN [sys].[dm_db_missing_index_group_stats] [migs] ON [migs].[group_handle] = [mig].[index_group_handle]
	INNER JOIN [sys].[dm_db_missing_index_details] [mid] ON [mig].[index_handle] = [mid].[index_handle]
WHERE [mid].[database_id] = DB_ID()
ORDER BY [avg_estimated_impact] DESC
GO</pre>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
