Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: How much space does a NULL field use?

Tags:

mysql

Does making a datum NULL save any memory versus its normal size?

like image 292
nerfist Avatar asked Jun 10 '11 21:06

nerfist


People also ask

How much space does NULL take up?

A NULL value in databases is a system value that takes up one byte of storage and indicates that a value is not present as opposed to a space or zero or any other default value.

Do NULL columns take up space?

Using Sparse Columns, NULL value will not consume any space regardless of using fixed-length or variable-length columns.

IS NULL same as space?

It is very important to understand that a NULL value is different than a zero value or a field that contains spaces, spaces are considered as values because they are strings (and sql can't tell what the value of a string means to the user per se), but a NULL signifies a missing value, and hence has no value associated ...

How is NULL stored in MySQL?

Since MySQL 5.0. 3 InnoDB uses COMPACT row format which uses only one bit to store a NULL (of course one byte is the minimum), therefore: Space Required for NULLs = CEILING(N/8) bytes where N is the number of NULL columns in a row.


1 Answers

This is discussed in the MySQL manual chapter on Storage Requirements. There is no simple answer; it depends on the data type of the column, whether the column is indexed; and the storage engine. The impact of using NULL for a column can vary from nothing to several bytes (depending on how many other columns are also NULL-able.)

The storage impact of declaring a column as accepting NULL, and of actually storing a NULL value, is probably minor. Having a column with lots of NULL values often indicates a need for (further) normalization.

The basic rule is, design your schema based on the properties of the data, not on the storage impact. Fix things only if they turn out to be a problem.

like image 80
Ted Hopp Avatar answered Oct 04 '22 14:10

Ted Hopp