Cari Blog Ini

24 Juli 2012

What is B-Tree in SQL Server ?

A B-Tree (balanced-tree) is a balanced, structured object,
and it is used for all indexes in Sql Server (Clustered, Nonclustered, XML, etc.).

Think of a B-Tree as a triangle-shaped structure of pages -
* it has a single root page at the top of the triangle
with pointers to the next level down the triangle
(which is wider than the root level by the number of pages
that the root page can contain pointers to),

* each of those pages contain pointers to the next level,
and so on (these are called intermediate pages) down the triangle
until you get to the bottom of the triangle,
which is the widest and referred to as the 'leaf' level of the structure.

In ASCII x marks, a B-tree would look like the following
if it contained 2 intermediate levels
where each page in the root and intermediate levels could each could point to 3 other pages:

x
xxx
xxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxx



What resides at the 'leaf' level of the structure
depends on what type of index the B-tree supports.

If it is a clustered index,
then the leaf level of the index contains all the data
for all the columns for all the rows of the table;

if it is a non-clustered index on a clustered table
it contains the non-clustered index keys,
the included column values,
and the keys for the clustered index (uses those as a pointer to the rest of the data);

if it is a non-clustered index on a heap table,
it contains the non-clustered index keys,
the included column values,
and the physical row-id value for the record in the heap
(uses this to get a pointer to the rest of the data).


Data at the leaf-level of a B-Tree is logically ordered in order of the index key
- this applies to both clustered and non-clustered indexes.

This logical ordering is achieved
via the "row offset"/"slot array" for records on a page,
and via a doubly-linked list for pages in an index.

A doubly-linked list is basically a pointer in the header of each page
that points to the prior logical page and the next logical page in the index.

Source:
http://www.mssqltips.com/sqlservertip/2261/sql-server-fragmentation-storage-basics-and-access-methods-part-1-of-9/