<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://kb.rvmgroup.it/index.php?action=history&amp;feed=atom&amp;title=Ricostruzione_indici_di_Database_Adiuto</id>
	<title>Ricostruzione indici di Database Adiuto - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://kb.rvmgroup.it/index.php?action=history&amp;feed=atom&amp;title=Ricostruzione_indici_di_Database_Adiuto"/>
	<link rel="alternate" type="text/html" href="https://kb.rvmgroup.it/index.php?title=Ricostruzione_indici_di_Database_Adiuto&amp;action=history"/>
	<updated>2026-08-04T01:00:50Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.44.2</generator>
	<entry>
		<id>https://kb.rvmgroup.it/index.php?title=Ricostruzione_indici_di_Database_Adiuto&amp;diff=11515&amp;oldid=prev</id>
		<title>Gabriele.vivinetto: Created page with &quot;Per migliorare le prestazioni è utile eseguire nottetempo queste query:&lt;pre&gt; EXEC sp_updatestats &lt;/pre&gt;&lt;pre&gt; SET NOCOUNT ON; DECLARE @db_id int; DECLARE @objectid int; DECLARE @indexid int; DECLARE @partitioncount bigint; DECLARE @schemaname nvarchar(130);  DECLARE @objectname nvarchar(130);  DECLARE @indexname nvarchar(130);  DECLARE @partitionnum bigint; DECLARE @partitions bigint; DECLARE @frag float; DECLARE @command nvarchar(4000);    SET @db_id = DB_ID(); -- Condi...&quot;</title>
		<link rel="alternate" type="text/html" href="https://kb.rvmgroup.it/index.php?title=Ricostruzione_indici_di_Database_Adiuto&amp;diff=11515&amp;oldid=prev"/>
		<updated>2026-07-14T15:51:56Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;Per migliorare le prestazioni è utile eseguire nottetempo queste query:&amp;lt;pre&amp;gt; EXEC sp_updatestats &amp;lt;/pre&amp;gt;&amp;lt;pre&amp;gt; SET NOCOUNT ON; DECLARE @db_id int; DECLARE @objectid int; DECLARE @indexid int; DECLARE @partitioncount bigint; DECLARE @schemaname nvarchar(130);  DECLARE @objectname nvarchar(130);  DECLARE @indexname nvarchar(130);  DECLARE @partitionnum bigint; DECLARE @partitions bigint; DECLARE @frag float; DECLARE @command nvarchar(4000);    SET @db_id = DB_ID(); -- Condi...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Per migliorare le prestazioni è utile eseguire nottetempo queste query:&amp;lt;pre&amp;gt;&lt;br /&gt;
EXEC sp_updatestats&lt;br /&gt;
&amp;lt;/pre&amp;gt;&amp;lt;pre&amp;gt;&lt;br /&gt;
SET NOCOUNT ON;&lt;br /&gt;
DECLARE @db_id int;&lt;br /&gt;
DECLARE @objectid int;&lt;br /&gt;
DECLARE @indexid int;&lt;br /&gt;
DECLARE @partitioncount bigint;&lt;br /&gt;
DECLARE @schemaname nvarchar(130); &lt;br /&gt;
DECLARE @objectname nvarchar(130); &lt;br /&gt;
DECLARE @indexname nvarchar(130); &lt;br /&gt;
DECLARE @partitionnum bigint;&lt;br /&gt;
DECLARE @partitions bigint;&lt;br /&gt;
DECLARE @frag float;&lt;br /&gt;
DECLARE @command nvarchar(4000); &lt;br /&gt;
 &lt;br /&gt;
SET @db_id = DB_ID();&lt;br /&gt;
-- Conditionally select tables and indexes from the sys.dm_db_index_physical_stats function &lt;br /&gt;
-- and convert object and index IDs to names.&lt;br /&gt;
SELECT&lt;br /&gt;
    object_id AS objectid,&lt;br /&gt;
    index_id AS indexid,&lt;br /&gt;
    partition_number AS partitionnum,&lt;br /&gt;
    avg_fragmentation_in_percent AS frag&lt;br /&gt;
INTO #work_to_do&lt;br /&gt;
FROM sys.dm_db_index_physical_stats (@db_id, NULL, NULL , NULL, &amp;#039;LIMITED&amp;#039;)&lt;br /&gt;
WHERE avg_fragmentation_in_percent &amp;gt; 10.0 AND index_id &amp;gt; 0;&lt;br /&gt;
 &lt;br /&gt;
-- Declare the cursor for the list of partitions to be processed.&lt;br /&gt;
DECLARE partitions CURSOR FOR SELECT * FROM #work_to_do;&lt;br /&gt;
 &lt;br /&gt;
-- Open the cursor.&lt;br /&gt;
OPEN partitions;&lt;br /&gt;
 &lt;br /&gt;
-- Loop through the partitions.&lt;br /&gt;
WHILE (1=1)&lt;br /&gt;
    BEGIN;&lt;br /&gt;
        FETCH NEXT&lt;br /&gt;
           FROM partitions&lt;br /&gt;
           INTO @objectid, @indexid, @partitionnum, @frag;&lt;br /&gt;
        IF @@FETCH_STATUS &amp;lt; 0 BREAK;&lt;br /&gt;
        SELECT @objectname = QUOTENAME(o.name), @schemaname = QUOTENAME(s.name)&lt;br /&gt;
        FROM sys.objects AS o&lt;br /&gt;
        JOIN sys.schemas as s ON s.schema_id = o.schema_id&lt;br /&gt;
        WHERE o.object_id = @objectid;&lt;br /&gt;
        SELECT @indexname = QUOTENAME(name)&lt;br /&gt;
        FROM sys.indexes&lt;br /&gt;
        WHERE  object_id = @objectid AND index_id = @indexid;&lt;br /&gt;
        SELECT @partitioncount = count (*)&lt;br /&gt;
        FROM sys.partitions&lt;br /&gt;
        WHERE object_id = @objectid AND index_id = @indexid;&lt;br /&gt;
 &lt;br /&gt;
-- 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding.&lt;br /&gt;
        IF @frag &amp;lt; 30.0&lt;br /&gt;
            SET @command = N&amp;#039;ALTER INDEX &amp;#039; + @indexname + N&amp;#039; ON &amp;#039; + @schemaname + N&amp;#039;.&amp;#039; + @objectname + N&amp;#039; REORGANIZE&amp;#039;;&lt;br /&gt;
        IF @frag &amp;gt;= 30.0&lt;br /&gt;
            SET @command = N&amp;#039;ALTER INDEX &amp;#039; + @indexname + N&amp;#039; ON &amp;#039; + @schemaname + N&amp;#039;.&amp;#039; + @objectname + N&amp;#039; REBUILD&amp;#039;;&lt;br /&gt;
        IF @partitioncount &amp;gt; 1&lt;br /&gt;
            SET @command = @command + N&amp;#039; PARTITION=&amp;#039; + CAST(@partitionnum AS nvarchar(10));&lt;br /&gt;
        EXEC (@command);&lt;br /&gt;
        PRINT N&amp;#039;Executed: &amp;#039; + @command+ N&amp;#039; &amp;#039; + cast(@frag as nvarchar(25)) ;&lt;br /&gt;
    END;&lt;br /&gt;
 &lt;br /&gt;
-- Close and deallocate the cursor.&lt;br /&gt;
CLOSE partitions;&lt;br /&gt;
DEALLOCATE partitions;&lt;br /&gt;
 &lt;br /&gt;
-- Drop the temporary table.&lt;br /&gt;
DROP TABLE #work_to_do;&lt;br /&gt;
GO&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Gabriele.vivinetto</name></author>
	</entry>
</feed>