Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NULL or empty string more efficient/natural? [closed]

Pretext, I am familiar with the semantical differences between a NULL value and an empty string.

I have a MySQL table where I store a lot of hostnames with their IP addresses (as a string) and wonder what would appear to be more natural (or efficient storage-wise) in case a hostname cannot be resolved.

A NULL value or an empty string (in which case it probably should be a VARCHAR and not a CHAR)

I would tend towards a NULL value but I would like to have this confirmed or disconfirmed.

like image 694
user2352129 Avatar asked May 05 '13 14:05

user2352129


People also ask

Is it better to store null or empty string?

So, NULL is better. An empty string is useful when the data comes from multiple resources. NULL is used when some fields are optional, and the data is unknown.

Does empty string take more space than null?

Learn MySQL from scratch for Data Science and Analytics In innoDB, NULL occupies less space as compared to empty string. Also, the NULL length is null while length of the empty string is 0. From the above output it is clear that the length of the empty string is 1. The above output means that count is 0 for null value.

Is a null string the same as an empty string?

An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .

Is null and empty string the same in SQL?

Much like Zero, an Empty String ("") differs from a NULL value in that the former specifically implies that the value was set to be empty, whereas NULL means that the value was not supplied or is unknown. As an example, let's consider a column that stores a cell phone number.


1 Answers

In MyISAM MYSQL you save one bit per row not using NULL. As it is stated here:

Declaring columns NULL can reduce the maximum number of columns permitted. For MyISAM tables, NULL columns require additional space in the row to record whether their values are NULL. Each NULL column takes one bit extra, rounded up to the nearest byte.

Take a look here as well:

In addition, while a NULL itself does not require any storage space, NDBCLUSTER reserves 4 bytes per row if the table definition contains any columns defined as NULL, up to 32 NULL columns. (If a MySQL Cluster table is defined with more than 32 NULL columns up to 64 NULL columns, then 8 bytes per row is reserved.)

Moreover it also makes the database work faster at it stated here (taken from stackoverflow - @DavidWinterbottom link didn't work for me, I added a different sourse)

It's harder for MySQL to optimize queries that refer to nullable coumns, because they make indexes, index statistics, and value comparisons more complicated. A nullable column uses more storage space and requires special processing inside MySQL. When a nullable column is indexed, it requires an extra byte per entry and can even cause a fixed-size inded (such as an index on a single integer column) to be converted to a variable-sized one in MyISAM.

In most of the cases non-NULL values behave more predictable when combined with COUNT() and other aggregating function but you can also see a NULL behave according to your needs.

As it is stated here, not all group (aggregate) functions ignore NULL for instance, COUNT() would give you different result that COUNT(*) for a column containing NULL values.

On the other hand as other point out NULL better reflects the meaning of entry - it is an unknown value and if you wanted to count all the hosts you would probably COUNT() to behave exactly as it does.

like image 190
Legat Avatar answered Nov 15 '22 19:11

Legat