HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Does setting column property to NOT NULL affect table size?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
columnnullsizeaffectsettingpropertydoesnottable

Problem

I have a bunch of columns in my MySQL database table that store booleans (0 or 1) as tinyints. They are 0 by default and are not meant to be null. I am thinking about setting the column property to NOT NULL so that there is another validation check just in case. I wonder though whether it will come at a cost of increased size of the table down the road. My fear is that, perhaps, NOT NULL property adds another bit to every data entry?

My database is new and practically empty so when I try to check the size of the table it always gives me 16kb.

Solution

From the MySQL documentation (emphasis mine):


Declare columns to be NOT NULL if possible. It makes SQL operations faster, by enabling better use of indexes and eliminating overhead for testing whether each value is NULL. You also save some storage space, one bit per column. If you really need NULL values in your tables, use them. Just avoid the default setting that allows NULL values in every column.

So, on the contrary, NOT NULL saves disk space, rather than costing more.

Context

StackExchange Database Administrators Q#182924, answer score: 6

Revisions (0)

No revisions yet.