This week I'm going to post a bunch of info on the basic structures used to store data and track allocations in SQL Server. A bunch of this was posted back when I started blogging at TechEd 2006 but I want to consolidate/clarify info and add more about using DBCC PAGE to examine the various structures.
So, what are records? At the simplest level, a record is the physical storage associated with a table or index row. Of course, it gets much more complicated than that...
Data records
Forwarded/Forwarding records
Index records
Text records
Ghost records
Other record types
Record structure
All records have the same structure, regardless of their type and use, but the number and type of columns will be different. For instance, a data record from a table with a complex schema may have hundreds of columns of various types whereas an allocation bitmap record will have a single column, filling up the whole page.
The record structure is as follows:
NULL bitmap optimization
So why is the NULL bitmap an optimization?
Firstly, having a null bitmap removes the need for storing special 'NULL' values for fixed-length datatypes. Without the null bitmap, how can you tell whether a column is NULL? For fixed-length columns you'd need to define a special 'NULL' value, which limits the effective range of the datatype being stored. For varchar columns, the value could be a zero-length empty string, so just checking the length doesn't work - you'd need the special value again. For all other variable-length data types you can just check the length. So, we nede the NULL bitmap.
Secondly, it saves CPU cycles. If there was no NULL bitmap, then there are extra instructions executed for fixed-length and variable-length columns.
For fixed-length:
For variable-length:
But with a NULL bitmap, all you have to do is:
So, its about even for a lookup of a single fixed-length column, but for variable-length columns, and for multiple row selects, there's a clear advantage to having the NULL bitmap.
Using DBCC IND and DBCC PAGE to examine a row in detail
Let's create an example table to look at:
USE MASTER; GO IF DATABASEPROPERTY (N'recordanatomy', 'Version') > 0 DROP DATABASE recordanatomy; GO CREATE DATABASE recordanatomy; GO USE recordanatomy; GO CREATE TABLE example ( destination VARCHAR(100), activity VARCHAR(100), duration INT);
USE
GO
IF
CREATE
destination
activity
duration
INSERT
DBCC IND ('recordanatomy', 'example', 1); GO
DBCC
The output tells us the data page is (1:143) so we can dump it with DBCC PAGE, using option 3 to get a fully interpreted dump of each record.
DBCC TRACEON (3604); GO DBCC PAGE ('recordanatomy', 1, 143, 3); GO
Remember we need the trace-flag to make the DBCC PAGE output go to the console instead of the error log. The output will contain something like the following:
Slot 0 Offset 0x60 Length 33 Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS Memory Dump @0x5C76C060 00000000: 30000800 05000000 0300f802 00160021 †0..............! 00000010: 0042616e 66667369 67687473 6565696e †.Banffsightseein 00000020: 67†††††††††††††††††††††††††††††††††††g Slot 0 Column 0 Offset 0x11 Length 5 destination = Banff Slot 0 Column 1 Offset 0x16 Length 11 activity = sightseeing Slot 0 Column 2 Offset 0x4 Length 4 duration = 5
Slot 0 Offset 0x60 Length 33
Record Type = PRIMARY_RECORD Record Attributes = NULL_BITMAP VARIABLE_COLUMNS
Memory Dump @0x5C76C060
00000000: 30000800 05000000 0300f802 00160021 †0..............! 00000010: 0042616e 66667369 67687473 6565696e †.Banffsightseein 00000020: 67†††††††††††††††††††††††††††††††††††g
Slot 0 Column 0 Offset 0x11 Length 5
destination = Banff
Slot 0 Column 1 Offset 0x16 Length 11
activity = sightseeing
Slot 0 Column 2 Offset 0x4 Length 4
duration = 5
Let's use the record structure I listed above to go through this record and see how things are stored.
And that's it.
Some of the features of SQL Server 2008 will introduce changes to the record structure - more on those when the features are available in CTPs.
Remember Me
a@href@title, strike
Theme design by Jelle Druyts
Pick a theme: BlogXP sqlx BlogXP sqlx
Powered by: newtelligence dasBlog 2.0.7226.0
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.
© Copyright 2008, Paul S. Randal
E-mail