Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do null values save storage space? [duplicate]

Possible Duplicate:
Space used by nulls in database

What's more efficient in terms of storage space?

  • A nullable int column full of nulls.
  • An int column full of zeroes.

I know a nullable column takes up 1 extra bit of information to store its null state, but do null values save the database from allocating 32 bits for an int that is null?

(null semantics and the meaning of null are not important for this question)

like image 310
Alex J Avatar asked Dec 28 '10 13:12

Alex J


3 Answers

For fixed width fields like nullable int the storage space required is always the same regardless of whether the value is null or not.

For variable width nullable fields the value ’NULL‘ takes zero bytes of storage space (ignoring the bit to store whether the value is null or not).

like image 105
Mark Byers Avatar answered Sep 20 '22 16:09

Mark Byers


If you are using SQL Server 2008, and expect to have a significant number of NULL values in columns, I would suggest you investigate Sparse Columns. They are optimized for storing NULL values.

like image 5
Randy Minder Avatar answered Sep 23 '22 16:09

Randy Minder


SQL Server 2008 introduced Sparse columns for columns that are primarily NULL.

A SPARSE column for a row, which is NULL uses 0 bits of storage. But you pay a 4 byte penalty if there is a value.

like image 4
Mitch Wheat Avatar answered Sep 24 '22 16:09

Mitch Wheat