supporting t-sql scripts for indexpage, datapage and indexdefragmentation

5
--DBCC PAGE --( --['database name'|database id], -- can be the actual name or id of the database --file number, -- the file number where the page is found --page number, -- the page number within the file --print option = [0|1|2|3] -- display option; each option provides differing levels of information --) --USE MASTER --GO --CREATE DATABASE MSSQLTIPS --GO --USE MSSQLTIPS --GO --CREATE TABLE DBO.EMPLOYEE --( --EMPLOYEEID INT IDENTITY(1,1), --FIRSTNAME VARCHAR(50) NOT NULL, --LASTNAME VARCHAR(50) NOT NULL, --DATE_HIRED DATETIME NOT NULL, --IS_ACTIVE BIT NOT NULL DEFAULT 1, --CONSTRAINT PK_EMPLOYEE PRIMARY KEY (EMPLOYEEID), --CONSTRAINT UQ_EMPLOYEE_LASTNAME UNIQUE (LASTNAME, FIRSTNAME) --)

Upload: mahabubur-rahaman

Post on 12-Aug-2015

20 views

Category:

Data & Analytics


0 download

TRANSCRIPT

Page 1: supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentation

--DBCC PAGE

--(

--['database name'|database id], -- can be the actual name or id of the database

--file number, -- the file number where the page is found

--page number, -- the page number within the file

--print option = [0|1|2|3] -- display option; each option provides differing levels of information

--)

--USE MASTER

--GO

--CREATE DATABASE MSSQLTIPS

--GO

--USE MSSQLTIPS

--GO

--CREATE TABLE DBO.EMPLOYEE

--(

--EMPLOYEEID INT IDENTITY(1,1),

--FIRSTNAME VARCHAR(50) NOT NULL,

--LASTNAME VARCHAR(50) NOT NULL,

--DATE_HIRED DATETIME NOT NULL,

--IS_ACTIVE BIT NOT NULL DEFAULT 1,

--CONSTRAINT PK_EMPLOYEE PRIMARY KEY (EMPLOYEEID),

--CONSTRAINT UQ_EMPLOYEE_LASTNAME UNIQUE (LASTNAME, FIRSTNAME)

--)

--GO

--INSERT INTO DBO.EMPLOYEE (FIRSTNAME,LASTNAME,DATE_HIRED)

--SELECT 'George', 'Washington', '1999-03-15'

--GO

Page 2: supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentation

--INSERT INTO DBO.EMPLOYEE (FIRSTNAME,LASTNAME,DATE_HIRED)

--SELECT 'Benjamin', 'Franklin', '2001-07-05'

--GO

--INSERT INTO DBO.EMPLOYEE (FIRSTNAME,LASTNAME,DATE_HIRED)

--SELECT 'Thomas', 'Jefferson', '2002-11-10'

--GO

--SELECT 'Benjamin', 'Franklin', '2001-07-05'

--GO

--INSERT INTO DBO.EMPLOYEE (FIRSTNAME,LASTNAME,DATE_HIRED)

--SELECT 'Thomas', 'Jefferson', '2002-11-10'

--GO

--DBCC IND

--(

--['database name'|database id], -- the database to use

--table name, -- the table name to list results

--index id, -- an index_id from sys.indexes; -1 shows all indexes and IAMs, -2 just show IAMs

--)

-- List data and index pages allocated to the EMPLOYEE table

DBCC IND('MSSQLTIPS',EMPLOYEE,-1)

GO

--PageFID PagePID IAMFID IAMPID ObjectID IndexID PartitionNumber PartitionID iam_chain_type PageType IndexLevel NextPageFID NextPagePID PrevPageFID PrevPagePID

-- 1 150 NULL NULL 2105058535 1 172057594038779904 In-row data 10

NULL 0 0 0 0

Page 3: supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentation

-- 1 147 1 150 2105058535 11 72057594038779904 In-row data

1 0 0 0 00

-- 1 154 NULL NULL 2105058535 2 172057594038845440 In-row data 10

NULL 0 0 0 0

-- 1 153 1 154 2105058535 21 72057594038845440 In-row data

2 0 0 0 00

--The columns

--PageFID : a file number where pages reside and

--PagePID representa page number within the file where data lives

--Index_id = 1 points to the clustered index where the leaf level pages are the actual data for the table

--PageType dictates the kind of page.

--Type = 1 is a data page,

--Type = 2 is an index page, and

--Type = 10 is the IAM page that maintains the index itself.

--The EMPLOYEE table has a clustered index (courtesy of the PRIMARY KEY definition)

--which means that there should be an IndexID = 1 produced by the DBCC IND output

--Looking at the output produced by DBCC IND, we can see that the clustered index,

--which is also a PageType = 1,

--can be found within file number (PageFID) = 1 and

--page number (PagePID) = 147.

--There are 4 different print options for displaying the page data.

--The one I use is print option 3 which dumps both page header information and data.

Page 4: supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentation

DBCC TRACEON(3604)

DBCC PAGE('MSSQLTIPS',1,147,3) WITH TABLERESULTS

GO

--The EMPLOYEE table also has a non-clustered index (via the UNIQUE constraint that was defined on the table).

--Let's examine the non-clustered index that was built.

--Looking at DBCC IND output again,

--we can easily determine the non-clustered index page

--since it is IndexID = 2 (PageType = 2) and

--that it can be found within file number (PageFID) = 1 and

--page number (PagePID) = 153.

--Note that if we had multiple indexes on this table,

--we could look in sys.indexes and get the index_id which we could then use to examine a specific index.

--Now let's examine the index data:

DBCC PAGE('MSSQLTIPS',1,153,3) WITH TABLERESULTS

GO

--If there was no clustered index on the table,

--this additional column would point to the actual data page instead.

--Let's rebuild the PRIMARY KEY as a non-clustered index and re-examine

--the non-clustered index created by the UNIQUE constraint.

--Note that by rebuilding the table with no clustered index,

--the underlying page data has changed. Using DBCC IND,

Page 5: supporting t-sql scripts for IndexPage, Datapage and IndexDefragmentation

--you can see how the page structures have changed.

ALTER TABLE DBO.EMPLOYEE DROP CONSTRAINT PK_EMPLOYEE

GO

ALTER TABLE DBO.EMPLOYEE ADD CONSTRAINT PK_EMPLOYEE

PRIMARY KEY NONCLUSTERED (EMPLOYEEID)

GO

DBCC IND('MSSQLTIPS',EMPLOYEE,-1)

DBCC PAGE('MSSQLTIPS',1,155,3) WITH TABLERESULTS