Clustered Index
Creating a Table
To better explain how SQL Server creates clustered indexes; let’s start by creating a new table and populating it with some sample data using the following scripts. You can use the same sample database as before.
Create Table DummyTable2
(
EmpId Int,
EmpName Varchar(8000)
)
EmpId Int,
EmpName Varchar(8000)
)
As in the previous example, when you first create a new table, there is no index created by default, and a heap is created. As before, we can confirm the fact that this new table doesn’t have an index by taking a look at the sysindexes system table, which contains one for this table with an of indid = 0. The sysindexes table, which exists in every database, tracks table and index information. “Indid” refers to Index ID, and is used to identify indexes. An indid of 0 means that a table does not have an index, and is stored by SQL Server as a heap.
Now let’s add a few records in this table using this script:
Insert Into DummyTable2 Values (4, Replicate ('d',2000))
GO
GO
Insert Into DummyTable2 Values (6, Replicate ('f',2000))
GO
GO
Insert Into DummyTable2 Values (1, Replicate ('a',2000))
GO
GO
Insert Into DummyTable2 Values (3, Replicate ('c',2000))
GO
GO
Now, let’s view the contents of the table by executing the following command in Query Analyzer for our new table.
Select EmpID From DummyTable2
GO
GO
| Empid |
| 4 |
| 6 |
| 1 |
| 3 |
As you would expect, the data we inserted has been displayed. Note that the order of the results is in the same order that I inserted them in, which is in no order at all.
Now, let’s execute the following commands to display the actual page information for the table we created and is now stored in SQL Server.
DBCC ind(dbid, tabid, -1) – It is an undocumented command.
DBCC TRACEON (3604)
GO
GO
Declare @DBID Int, @TableID Int
Select @DBID = db_id(), @TableID = object_id('DummyTable2')
DBCC ind(@DBID, @TableID, -1)
GO
Select @DBID = db_id(), @TableID = object_id('DummyTable2')
DBCC ind(@DBID, @TableID, -1)
GO
This script will display many columns, but we are only interested in three of them, as shown below.
Here are the results:
| PagePID | IndexID | PageType |
| 26408 | 0 | 10 |
| 26255 | 0 | 1 |
| 26409 | 0 | 1 |
Here’s what the information displayed means:
PagePID is the physical page numbers used to store the table. In this case, three pages are currently used to store the data.
IndexID is the type of index,
Where:
0 – Datapage
1 – Clustered Index
2 – Greater and equal to 2 is an Index page (Non-Clustered Index and ordinary index)
PageType tells you what kind of data is stored in each database
Where:
10 – IAM (Index Allocation MAP)
1 – Datapage
2 – Index page
Now, let us execute DBCC PAGE command.
DBCC page(dbid, fileno, pageno, option)
Where:
dbid = database id.
Fileno = fileno of the page. Usually it will be 1, unless we use more than one file for a database.
Pageno = we can take the output of the dbcc ind page no.
Option = it can be 0, 1, 2, 3. I use 3 to get a display of the data. You can try yourself for the other options.
Run this script to execute the command:
DBCC TRACEON (3604)
GO
GO
DBCC page(@DBID, 1, 26408, 3)
GO
GO
The output will be page allocation details.
DBCC TRACEON (3604)
GO
GO
DBCC page(@DBID, 1, 26255, 3)
GO
GO
The output will display the data however it was entered in the table. This is how SQL stores the data in pages. Actually, 26255 & 26409 will display the data page.
I have displayed the data page information for page 26255 only. This is how MS-SQL stores the contents in data pages as such column name with its respective value.
Record Type = PRIMARY_RECORD
EmpId = 4
EmpName = dddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
dddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
Record Type = PRIMARY_RECORD
EmpId = 6
EmpName = ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
Record Type = PRIMARY_RECORD
EmpId = 1
EmpName = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
This displays the exact data storage in SQL without any index on table. Now, let’s go and create a Unique Clustered Index on EmpID column.
Create a Clustered Index
Now, let us create a unique clustered index on empid column to see how it affects the data that is stored in SQL Server.
CREATE UNIQUE CLUSTERED INDEX DummyTable2_EmpIndex
ON DummyTable2 (EmpID)
GO
ON DummyTable2 (EmpID)
GO
Execute:
Select EmpID From DummyTable2
Here are the results:
| Empid |
| 1 |
| 3 |
| 4 |
| 6 |
Now, execute the DBCC ind (dbid, tabid, -1)
DBCC TRACEON (3604)
GO
GO
Declare @DBID Int, @TableID Int
Select @DBID = db_id(), @TableID = object_id('DummyTable2')
DBCC ind(@DBID, @TableID, -1)
GO
GO
Here are the results:
| PagePID | IndexID | PageType |
| 26411 | 1 | 10 |
| 26410 | 0 | 1 |
| 26412 | 1 | 2 |
MS SQL generates a page (pagetype = 10) for an index and explains the page allocation details for an index. It shows the number of index page have been occupied for an index.
Now, let us view the page 26410 and 26412 and see the page details.
DBCC TRACEON (3604)
GO
GO
DBCC page(10, 1, 26412, 3)
GO
GO
Here are the results:
| FileID | PageID | ChildPageID | EMPID |
| 1 | 26412 | 26410 | 0 |
The output display many columns, but we are only interested in four of them as shown above.
This will display the output of the index page, which has got link to data page (ChildPageID). EMPID is an index column will contain the starting row of the page.
Now, let us view the page 26410 and see the page details.
DBCC TRACEON (3604)
GO
GO
DBCC page (10, 1, 26410, 3)
GO
GO
Here are the results:
Record Type = PRIMARY_RECORD
EmpId = 1
EmpName = aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Record Type = PRIMARY_RECORD
EmpId = 2
EmpName = bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
Record Type = PRIMARY_RECORD
EmpId = 3
EmpName = cccccccccccccccccccccccccccccccccccccccccccccccc
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
Though I have added disorder records, SQL has displayed the data page in sequence because we have got a clustered index on empid. This is absolutely great! Adding a clustered index to the table has physically reordered the data pages, putting them in physical order based on the indexed column.
Now, let’s insert some more rows in our table and view the data and index page storage of our clustered index.
Insert Into DummyTable2 Values (10, Replicate ('j',2000))
GO
GO
Insert Into DummyTable2 Values (2, Replicate ('b',2000))
GO
GO
Insert Into DummyTable2 Values (5, Replicate ('e',2000))
GO
GO
Insert Into DummyTable2 Values (8, Replicate ('h',2000))
GO
GO
Insert Into DummyTable2 Values (9, Replicate ('i',2000))
GO
GO
Insert Into DummyTable2 Values (7, Replicate ('g',2000))
GO
GO
Now, execute the DBCC ind (dbid, tabid, -1)
DBCC TRACEON (3604)
GO
GO
Declare @DBID Int, @TableID Int
Select @DBID = db_id(), @TableID = object_id('DummyTable2')
DBCC ind(@DBID, @TableID, -1)
GO
GO
Here are the results:
| PagePID | IndexID | PageType |
| 26411 | 1 | 10 |
| 26410 | 0 | 1 |
| 26412 | 1 | 2 |
| 26255 | 0 | 1 |
| 26408 | 0 | 1 |
| 26409 | 0 | 1 |
Now, we see few more rows than before. Page 26411 displays the page allocation details, and pages 26408, 26409, 26410 and 26255 display the data page details, as before.
In regard to the new pages, page 26411 displays the page allocation details of an index page and 26412 displays the index page details.
MS-SQL generates a page (pagetype = 10) for an index and explains the page allocation details for an index. It shows the number of index page have been occupied for an index.
Let us see what would be the output for page 26411, that is page type = 10.
DBCC TRACEON (3604)
GO
GO
dbcc page(10, 1, 26411, 3)
GO
GO
Here are the results:
IAM: Single Page Allocations @0x30A5C08E
-----------------------------------------
Slot 0 = (1:26410)
Slot 1 = (1:26412)
Slot 2 = (1:26255)
Slot 3 = (1:26408)
Slot 4 = (1:26409)
Let us view page 26412 to see the index page details.
DBCC TRACEON (3604)
GO
GO
DBCC page(10, 1, 26412, 3)
GO
GO
Here are the results:
| FileID | PageID | ChildPageID | EMPID |
| 1 | 26412 | 26410 | 0 |
| 1 | 26412 | 26408 | 4 |
| 1 | 26412 | 26255 | 6 |
| 1 | 26412 | 26409 | 9 |
This helps us to get an idea to decide the need of clustered index. It is really useful to have a clustered index when retrieve many rows of data, ranges of data, and when BETWEEN is used in the WHERE clause. Because, the leaf level of the clustered index is the data. It should be used to save many I/Os. So, it is better to use clustered indexes to solve queries asking for ranges of data, not one row.
For example, to search for an employee ID (empid between 3 and 9) in a table that has a clustered index on the empid column.
Select EmpID, EmpName From DummyTable1 WHEREEMPID Between 3 And 9
No comments:
Post a Comment