Why is the NULL bitmap in a record an optimization?
I've had some questions sent in comments and I wanted to reply to some of them using a post so others who aren't subscribed to the comment sections can see the answers.
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? That's easy for a variable-length column - just check the length. If it's zero, then the column is NULL. [Edit] Ryan Stonecipher (the dev responsible for DBCC) pointed out that I'd forgotten the case of empty strings - thanks Ryan. In this case, an empty string also has a zero length so for varchar columns you'd need to use the fixed-length solution described below. It's not so easy for fixed-length columns, which, as their name suggests, have a fixed-length so that trick doesn't work. The only solution is to define a special 'NULL' value, which limits the effective range of the datatype being stored. Secondly, it saves CPU cycles. If there was no NULL bitmap, then there are extra instructions executed for fixed- 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. Hope this makes my comment a bit clearer. |
Comments
- Anonymous
June 28, 2006
Oh if you put it that way ^^ :-)
Thanks for the great elaboration! - Anonymous
February 18, 2009
PingBack from http://www.glorf.it/blog/2006/07/03/sql-talk/sql-server-storage-engine