Wednesday, February 22, 2012

SQL server index fragmentation – reorganize or rebuild example


SQL server index fragmentation – reorganize or rebuild


Below are some SQL scripts to reorganize or rebuild an index. 
DECLARE @dbname nvarchar(50);
DECLARE @tblname nvarchar(100);

DECLARE @dbid int;
DECLARE @tblid int;

SET @dbname = 'AdventureWorks2008R2';
SET @tblname = @dbname + N'.dbo.Customer'
SELECT @dbid = DB_ID(@dbname);
SELECT @dbid  AS [Database ID];

SELECT @tblid = OBJECT_ID(@tblname);
SELECT @tblid AS [Object ID];
GO
Database ID
-----------
44

(1 row(s) affected)

Object ID
-----------
1605580758

(1 row(s) affected)

USE master;
GO
DECLARE @dbname nvarchar(50);
DECLARE @tblname nvarchar(100);

SET @dbname = 'AdventureWorks2008R2';
SET @tblname = @dbname + N'.dbo.Customer'

SELECT * FROM sys.dm_db_index_physical_stats (DB_ID(@dbname), OBJECT_ID(@tblname), NULL, NULL , 'DETAILED')
WHERE avg_fragmentation_in_percent > 30;
GO


--- defragmentation of the index , reorganize ----------
USE AdventureWorks2008R2
GO
ALTER INDEX PK_Customer ON dbo.Customer
      REORGANIZE WITH ( LOB_COMPACTION = OFF );
GO
--- defragmentation of the index, rebuild ------------------------------------
USE AdventureWorks2008R2
GO
ALTER INDEX PK_Customer ON dbo.Customer
      REBUILD;
GO


3 comments:

Anonymous said...

louis vuitton outlet, sac longchamp, ralph lauren pas cher, replica watches, louboutin outlet, louboutin shoes, christian louboutin outlet, prada outlet, ugg boots, longchamp pas cher, tiffany and co, louis vuitton, louboutin, air jordan pas cher, tory burch outlet, cheap oakley sunglasses, nike outlet, polo ralph lauren outlet, ray ban sunglasses, polo ralph lauren outlet, louis vuitton outlet, nike air max, replica watches, louis vuitton, air max, longchamp outlet, michael kors, oakley sunglasses, chanel handbags, nike free, nike roshe run, oakley sunglasses, burberry, tiffany jewelry, ray ban sunglasses, kate spade outlet, prada handbags, oakley sunglasses, nike air max, louis vuitton, louboutin pas cher, uggs on sale, ray ban sunglasses, oakley sunglasses, longchamp, jordan shoes, gucci outlet, ugg boots, nike free, longchamp outlet

Anonymous said...

lancel, celine handbags, jimmy choo shoes, bottega veneta, nike roshe, asics running shoes, gucci, giuseppe zanotti, nike huarache, mcm handbags, herve leger, hollister, hollister, new balance, ray ban, p90x workout, soccer shoes, babyliss, longchamp, mac cosmetics, nike trainers, chi flat iron, mont blanc, vans, vans shoes, ghd, iphone cases, nike air max, hollister, ferragamo shoes, ralph lauren, louboutin, nike air max, beats by dre, valentino shoes, converse outlet, lululemon, north face outlet, instyler, soccer jerseys, birkin bag, insanity workout, baseball bats, north face outlet, abercrombie and fitch, timberland boots, reebok shoes, nfl jerseys, oakley, wedding dresses

Anonymous said...

hollister, louis vuitton, canada goose outlet, moncler, pandora jewelry, swarovski, louis vuitton, bottes ugg, coach outlet, moncler, supra shoes, montre pas cher, moncler, ugg,uggs,uggs canada, ugg,ugg australia,ugg italia, canada goose, links of london, pandora jewelry, karen millen, doudoune canada goose, juicy couture outlet, moncler, pandora charms, marc jacobs, swarovski crystal, moncler, ugg pas cher, thomas sabo, louis vuitton, moncler outlet, moncler, canada goose, canada goose uk, canada goose outlet, ugg boots uk, juicy couture outlet, wedding dresses, moncler, canada goose, toms shoes, louis vuitton, replica watches, sac louis vuitton pas cher, pandora charms, canada goose