Marco Papo
2008-02-21 13:55:14 UTC
Buonasera a tutti,
sui BOL ho trovato questo script che dovrebbe riorganizzare o ricostruire
gli indici in base alla percentuale di frammentazione degli stessi.
-- ensure a USE <databasename> statement has been executed first.
SET NOCOUNT ON;
DECLARE @objectid int;
DECLARE @indexid int;
DECLARE @partitioncount bigint;
DECLARE @schemaname sysname;
DECLARE @objectname sysname;
DECLARE @indexname sysname;
DECLARE @partitionnum bigint;
DECLARE @partitions bigint;
DECLARE @frag float;
DECLARE @command varchar(8000);
-- ensure the temporary table does not exist
IF EXISTS (SELECT name FROM sys.objects WHERE name = 'work_to_do')
DROP TABLE work_to_do;
-- conditionally select from the function, converting object and index IDs
to names.
SELECT
object_id AS objectid,
index_id AS indexid,
partition_number AS partitionnum,
avg_fragmentation_in_percent AS frag
INTO work_to_do
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, 'LIMITED')
WHERE avg_fragmentation_in_percent > 10.0 AND index_id > 0;
-- Declare the cursor for the list of partitions to be processed.
DECLARE partitions CURSOR FOR SELECT * FROM work_to_do;
-- Open the cursor.
OPEN partitions;
-- Loop through the partitions.
FETCH NEXT
FROM partitions
INTO @objectid, @indexid, @partitionnum, @frag;
WHILE @@FETCH_STATUS = 0
BEGIN;
SELECT @objectname = o.name, @schemaname = s.name
FROM sys.objects AS o
JOIN sys.schemas as s ON s.schema_id = o.schema_id
WHERE o.object_id = @objectid;
SELECT @indexname = name
FROM sys.indexes
WHERE object_id = @objectid AND index_id = @indexid;
SELECT @partitioncount = count (*)
FROM sys.partitions
WHERE object_id = @objectid AND index_id = @indexid;
-- 30 is an arbitrary decision point at which to switch between reorganizing
and rebuilding
IF @frag < 30.0
BEGIN;
SELECT @command = 'ALTER INDEX ' + @indexname + ' ON ' + @schemaname + '.' +
@objectname + ' REORGANIZE';
IF @partitioncount > 1
SELECT @command = @command + ' PARTITION=' + CONVERT (CHAR, @partitionnum);
EXEC (@command);
END;
IF @frag >= 30.0
BEGIN;
SELECT @command = 'ALTER INDEX ' + @indexname +' ON ' + @schemaname + '.' +
@objectname + ' REBUILD';
IF @partitioncount > 1
SELECT @command = @command + ' PARTITION=' + CONVERT (CHAR, @partitionnum);
EXEC (@command);
END;
PRINT 'Executed ' + @command;
FETCH NEXT FROM partitions INTO @objectid, @indexid, @partitionnum, @frag;
END;
-- Close and deallocate the cursor.
CLOSE partitions;
DEALLOCATE partitions;
-- drop the temporary table
IF EXISTS (SELECT name FROM sys.objects WHERE name = 'work_to_do')
DROP TABLE work_to_do;
GO
Lo eseguito su una copia del mio DB e ho riscontrato che in taluni casi ha
deframmentato gli indici, mentre alcuni inidici o li ha tralasciati o ne ha
ridotto la frammentazione ma in maniera poco significativa.
Guardate per esempio, il prima e poi di, questa tabella
NOMESITUAZIONE PRIMA DELLO SCRIPTMESSAGGIO SCRIPTSITUAZIONE DOPO LO SCRIPT
aaaaaTBL_NC_da_clienti_PK
66,6666666666667Executed ALTER INDEX aaaaaTBL_NC_da_clienti_PK ON
dbo.TBL_NC_da_clienti REBUILD66,6666666666667
ID66,6666666666667Executed ALTER INDEX ID ON dbo.TBL_NC_da_clienti
REBUILD66,6666666666667
IDArticolo88,8888888888889Executed ALTER INDEX IDArticolo ON
dbo.TBL_NC_da_clienti REBUILD80
IDCliente80Executed ALTER INDEX IDCliente ON dbo.TBL_NC_da_clienti
REBUILD66,6666666666667
IDNC_Cliente83,3333333333333Executed ALTER INDEX IDNC_Cliente ON
dbo.TBL_NC_da_clienti REBUILD80
IDRapporto_cliente85,7142857142857Executed ALTER INDEX IDRapporto_cliente ON
dbo.TBL_NC_da_clienti REBUILD80
IDRapporto_cliente_File28,5714285714286Executed ALTER INDEX
IDRapporto_cliente_File ON dbo.TBL_NC_da_clienti REORGANIZE33,3333333333333
IDRapporto_cliente_File_Indirizzo20Executed ALTER INDEX
IDRapporto_cliente_File_Indirizzo ON dbo.TBL_NC_da_clienti
REORGANIZE22,2222222222222
di cui allego script di creazione:
/****** Object: Table [dbo].[TBL_NC_da_clienti] Script Date: 02/21/2008
14:51:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TBL_NC_da_clienti](
[IDNC] [int] IDENTITY(1,1) NOT NULL,
[IDNC_Cliente] [nvarchar](50) NULL,
[Data] [datetime] NULL,
[IDCliente] [int] NULL DEFAULT ((0)),
[Data_Ricevimento_Notifica] [datetime] NULL,
[IDRapporto_cliente] [nvarchar](20) NULL,
[Data_rapporto_cliente] [datetime] NULL,
[IDRapporto_cliente_File] [nvarchar](255) NULL,
[IDRapporto_cliente_File_Indirizzo] [nvarchar](255) NULL,
[Rif_DDT] [nvarchar](50) NULL,
[Rif_DDT_Data] [datetime] NULL,
[IDArticolo] [nvarchar](7) NULL,
[Quantità_fornitura] [int] NULL DEFAULT ((0)),
[Quantità_NC_Pz] [int] NULL DEFAULT ((0)),
[Quantità_NC_Perc] [int] NULL DEFAULT ((0)),
[Conseguenze_fornitura] [nvarchar](50) NULL,
[Causale_gestionale] [nvarchar](50) NULL,
[Notificare_AC] [bit] NULL DEFAULT ((0)),
[Notificare_a] [nvarchar](255) NULL,
[Intraprendere_AC_AP] [bit] NULL DEFAULT ((0)),
[Rif_AC_AP] [nvarchar](50) NULL,
[Esaminata] [bit] NULL DEFAULT ((0)),
[Note] [ntext] NULL,
[Riemisione_ordine] [bit] NULL DEFAULT ((0)),
[DateStamp] [timestamp] NOT NULL,
CONSTRAINT [aaaaaTBL_NC_da_clienti_PK] PRIMARY KEY NONCLUSTERED
(
[IDNC] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [ID] ON [dbo].[TBL_NC_da_clienti]
(
[IDNC] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IDArticolo] ON [dbo].[TBL_NC_da_clienti]
(
[IDArticolo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IDCliente] ON [dbo].[TBL_NC_da_clienti]
(
[IDCliente] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IDNC_Cliente] ON [dbo].[TBL_NC_da_clienti]
(
[IDNC_Cliente] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IDRapporto_cliente] ON [dbo].[TBL_NC_da_clienti]
(
[IDRapporto_cliente] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IDRapporto_cliente_File] ON
[dbo].[TBL_NC_da_clienti]
(
[IDRapporto_cliente_File] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IDRapporto_cliente_File_Indirizzo] ON
[dbo].[TBL_NC_da_clienti]
(
[IDRapporto_cliente_File_Indirizzo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
La cosa strana se eseguo il Reorganize All da Management Studio il
risultato non cambia?
Coma mai? qualche idea?
sui BOL ho trovato questo script che dovrebbe riorganizzare o ricostruire
gli indici in base alla percentuale di frammentazione degli stessi.
-- ensure a USE <databasename> statement has been executed first.
SET NOCOUNT ON;
DECLARE @objectid int;
DECLARE @indexid int;
DECLARE @partitioncount bigint;
DECLARE @schemaname sysname;
DECLARE @objectname sysname;
DECLARE @indexname sysname;
DECLARE @partitionnum bigint;
DECLARE @partitions bigint;
DECLARE @frag float;
DECLARE @command varchar(8000);
-- ensure the temporary table does not exist
IF EXISTS (SELECT name FROM sys.objects WHERE name = 'work_to_do')
DROP TABLE work_to_do;
-- conditionally select from the function, converting object and index IDs
to names.
SELECT
object_id AS objectid,
index_id AS indexid,
partition_number AS partitionnum,
avg_fragmentation_in_percent AS frag
INTO work_to_do
FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, 'LIMITED')
WHERE avg_fragmentation_in_percent > 10.0 AND index_id > 0;
-- Declare the cursor for the list of partitions to be processed.
DECLARE partitions CURSOR FOR SELECT * FROM work_to_do;
-- Open the cursor.
OPEN partitions;
-- Loop through the partitions.
FETCH NEXT
FROM partitions
INTO @objectid, @indexid, @partitionnum, @frag;
WHILE @@FETCH_STATUS = 0
BEGIN;
SELECT @objectname = o.name, @schemaname = s.name
FROM sys.objects AS o
JOIN sys.schemas as s ON s.schema_id = o.schema_id
WHERE o.object_id = @objectid;
SELECT @indexname = name
FROM sys.indexes
WHERE object_id = @objectid AND index_id = @indexid;
SELECT @partitioncount = count (*)
FROM sys.partitions
WHERE object_id = @objectid AND index_id = @indexid;
-- 30 is an arbitrary decision point at which to switch between reorganizing
and rebuilding
IF @frag < 30.0
BEGIN;
SELECT @command = 'ALTER INDEX ' + @indexname + ' ON ' + @schemaname + '.' +
@objectname + ' REORGANIZE';
IF @partitioncount > 1
SELECT @command = @command + ' PARTITION=' + CONVERT (CHAR, @partitionnum);
EXEC (@command);
END;
IF @frag >= 30.0
BEGIN;
SELECT @command = 'ALTER INDEX ' + @indexname +' ON ' + @schemaname + '.' +
@objectname + ' REBUILD';
IF @partitioncount > 1
SELECT @command = @command + ' PARTITION=' + CONVERT (CHAR, @partitionnum);
EXEC (@command);
END;
PRINT 'Executed ' + @command;
FETCH NEXT FROM partitions INTO @objectid, @indexid, @partitionnum, @frag;
END;
-- Close and deallocate the cursor.
CLOSE partitions;
DEALLOCATE partitions;
-- drop the temporary table
IF EXISTS (SELECT name FROM sys.objects WHERE name = 'work_to_do')
DROP TABLE work_to_do;
GO
Lo eseguito su una copia del mio DB e ho riscontrato che in taluni casi ha
deframmentato gli indici, mentre alcuni inidici o li ha tralasciati o ne ha
ridotto la frammentazione ma in maniera poco significativa.
Guardate per esempio, il prima e poi di, questa tabella
NOMESITUAZIONE PRIMA DELLO SCRIPTMESSAGGIO SCRIPTSITUAZIONE DOPO LO SCRIPT
aaaaaTBL_NC_da_clienti_PK
66,6666666666667Executed ALTER INDEX aaaaaTBL_NC_da_clienti_PK ON
dbo.TBL_NC_da_clienti REBUILD66,6666666666667
ID66,6666666666667Executed ALTER INDEX ID ON dbo.TBL_NC_da_clienti
REBUILD66,6666666666667
IDArticolo88,8888888888889Executed ALTER INDEX IDArticolo ON
dbo.TBL_NC_da_clienti REBUILD80
IDCliente80Executed ALTER INDEX IDCliente ON dbo.TBL_NC_da_clienti
REBUILD66,6666666666667
IDNC_Cliente83,3333333333333Executed ALTER INDEX IDNC_Cliente ON
dbo.TBL_NC_da_clienti REBUILD80
IDRapporto_cliente85,7142857142857Executed ALTER INDEX IDRapporto_cliente ON
dbo.TBL_NC_da_clienti REBUILD80
IDRapporto_cliente_File28,5714285714286Executed ALTER INDEX
IDRapporto_cliente_File ON dbo.TBL_NC_da_clienti REORGANIZE33,3333333333333
IDRapporto_cliente_File_Indirizzo20Executed ALTER INDEX
IDRapporto_cliente_File_Indirizzo ON dbo.TBL_NC_da_clienti
REORGANIZE22,2222222222222
di cui allego script di creazione:
/****** Object: Table [dbo].[TBL_NC_da_clienti] Script Date: 02/21/2008
14:51:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TBL_NC_da_clienti](
[IDNC] [int] IDENTITY(1,1) NOT NULL,
[IDNC_Cliente] [nvarchar](50) NULL,
[Data] [datetime] NULL,
[IDCliente] [int] NULL DEFAULT ((0)),
[Data_Ricevimento_Notifica] [datetime] NULL,
[IDRapporto_cliente] [nvarchar](20) NULL,
[Data_rapporto_cliente] [datetime] NULL,
[IDRapporto_cliente_File] [nvarchar](255) NULL,
[IDRapporto_cliente_File_Indirizzo] [nvarchar](255) NULL,
[Rif_DDT] [nvarchar](50) NULL,
[Rif_DDT_Data] [datetime] NULL,
[IDArticolo] [nvarchar](7) NULL,
[Quantità_fornitura] [int] NULL DEFAULT ((0)),
[Quantità_NC_Pz] [int] NULL DEFAULT ((0)),
[Quantità_NC_Perc] [int] NULL DEFAULT ((0)),
[Conseguenze_fornitura] [nvarchar](50) NULL,
[Causale_gestionale] [nvarchar](50) NULL,
[Notificare_AC] [bit] NULL DEFAULT ((0)),
[Notificare_a] [nvarchar](255) NULL,
[Intraprendere_AC_AP] [bit] NULL DEFAULT ((0)),
[Rif_AC_AP] [nvarchar](50) NULL,
[Esaminata] [bit] NULL DEFAULT ((0)),
[Note] [ntext] NULL,
[Riemisione_ordine] [bit] NULL DEFAULT ((0)),
[DateStamp] [timestamp] NOT NULL,
CONSTRAINT [aaaaaTBL_NC_da_clienti_PK] PRIMARY KEY NONCLUSTERED
(
[IDNC] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [ID] ON [dbo].[TBL_NC_da_clienti]
(
[IDNC] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IDArticolo] ON [dbo].[TBL_NC_da_clienti]
(
[IDArticolo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IDCliente] ON [dbo].[TBL_NC_da_clienti]
(
[IDCliente] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IDNC_Cliente] ON [dbo].[TBL_NC_da_clienti]
(
[IDNC_Cliente] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IDRapporto_cliente] ON [dbo].[TBL_NC_da_clienti]
(
[IDRapporto_cliente] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IDRapporto_cliente_File] ON
[dbo].[TBL_NC_da_clienti]
(
[IDRapporto_cliente_File] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IDRapporto_cliente_File_Indirizzo] ON
[dbo].[TBL_NC_da_clienti]
(
[IDRapporto_cliente_File_Indirizzo] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF,
IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS =
ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO
La cosa strana se eseguo il Reorganize All da Management Studio il
risultato non cambia?
Coma mai? qualche idea?
--
Marco Papetti
www.shortchannel.it Il software di commercio elettronico OpenSource
www.mondoideale.com - Prodotti naturali e biologici.
Marco Papetti
www.shortchannel.it Il software di commercio elettronico OpenSource
www.mondoideale.com - Prodotti naturali e biologici.