17.06.2013 Views

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

Beginning Microsoft SQL Server 2008 ... - S3 Tech Training

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9: <strong>SQL</strong> <strong>Server</strong> Storage and Index Structures<br />

The way the physical data is stored varies between clustered and non-clustered indexes. The way <strong>SQL</strong><br />

<strong>Server</strong> traverses the B-Tree to get to the end data varies between all three index types.<br />

All <strong>SQL</strong> <strong>Server</strong> indexes have leaf level and non-leaf level pages. As we mentioned when we discussed B-<br />

Trees, the leaf level is the level that holds the “key” to identifying the record, and the non-leaf level<br />

pages are guides to the leaf level.<br />

The indexes are built over either a clustered table (if the table has a clustered index) or what is called a<br />

heap (what’s used for a table without a clustered index).<br />

Clustered Tables<br />

A clustered table is any table that has a clustered index on it. Clustered indexes are discussed in detail<br />

shortly, but what they mean to the table is that the data is physically stored in a designated order. Individual<br />

rows are uniquely identified through the use of the cluster key — the columns that define the clustered<br />

index.<br />

This should bring to mind the question, “What if the clustered index is not unique?” That is, how can a<br />

clustered index be used to uniquely identify a row if the index is not a unique index? The answer lies<br />

under the covers: <strong>SQL</strong> <strong>Server</strong> forces any clustered indexes to be unique — even if you don’t define them<br />

that way. Fortunately, it does this in a way that doesn’t change how you use the index. You can still<br />

insert duplicate rows if you wish, but <strong>SQL</strong> <strong>Server</strong> will add a suffix to the key internally to ensure that<br />

the row has a unique identifier.<br />

Heaps<br />

A heap is any table that does not have a clustered index on it. In this case, a unique identifier, or row ID<br />

(RID), is created based on a combination of the extent, pages, and row offset (places from the top of the<br />

page) for that row. A RID is only necessary if there is no cluster key available (no clustered index).<br />

Clustered Indexes<br />

270<br />

A clustered index is unique for any given table — you can only have one per table. You don’t have to have<br />

a clustered index, but you’ll find it to be one of the most commonly chosen types for a variety of reasons<br />

that will become apparent as we look at our index types.<br />

What makes a clustered index special is that the leaf level of a clustered index is the actual data — that<br />

is, the data is re-sorted to be stored in the same physical order of the index sort criteria state. This means<br />

that once you get to the leaf level of the index, you’re done; you’re at the data. Any new record is inserted<br />

according to its correct physical order in the clustered index. How new pages are created changes depending<br />

on where the record needs to be inserted.<br />

In the case of a new record that needs to be inserted into the middle of the index structure, a normal<br />

page split occurs. The last half of the records from the old page are moved to the new page, and the new<br />

record is inserted into the new or old page as appropriate.<br />

In the case of a new record that is logically at the end of the index structure, a new page is created, but<br />

only the new record is added to the new page, as shown in Figure 9-4.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!