With feelings and dry goods, WeChat search [Desolate Ancient Legend] to pay attention to this different programmer.
During normal development, MySQL fields are generally set to NOT NULL for the following reasons:
1. The difference between empty value ("") and "NULL":
- Null values take no space
- NULL in MySQL actually takes up space
NULL columns require additional space in the row to record whether their values are NULL. For MyISAM tables, each NULL column takes one bit extra, rounded up to the nearest byte.
The so-called NULL means nothing, not even \0, \0 is the terminator in the string, but it takes up space in physical memory, equal to one byte, and NULL means that there is not even this byte.
2. Not conducive to query optimization
If the query contains NULL columns, it is more difficult for MySQL to optimize, because the NULL columns will make the index, index statistics and value comparison more complicated.
When NULLable columns are indexed, each index record requires an extra byte, which in MyISAM can even cause a fixed-size index (such as an index with only one integer column) to become a variable-size index.
References:
- MySQL database optimization fields try to avoid NULL
- MySQL field attributes should be set to NOT NULL as much as possible
The article is continuously updated, you can search for "Desolate Ancient Legend" on WeChat to read it as soon as possible.