Performance effects of using NULL-able fields in MySQL
Sometimes an absent value can be represented (with no loss of function) without resort to a NULL-able column, e.g.:
- Zero integer in a column that references the AUTO_INCREMENT row ID of another table
- Invalid date value (0000-00-00)
- Zero timestamp value
- Empty string
On the other hand, according to Ted Codd's relational model, NULL is the marker of an absent datum. I always feel better doing something "the correct way" and MySQL supports it and the associated 3-value logic, 开发者_运维问答so why not?
A few years ago I was woking on a performance problem and found that I could resolve it simply by adding NOT NULL
to a column definition. The column was indexed but I don't remember other details. I have avoided NULL-able columns when there is an alternative since then.
But it has always bothered me that I don't properly understand the performance effects of allowing NULL in a MySQL table. Can anyone help out?
It saves 1 bit per column. http://dev.mysql.com/doc/refman/5.0/en/data-size.html Doesn't seem like much, but over millions of rows it starts to make a difference
精彩评论