pepe
pepe

Reputation: 9919

MySQL - Usage of 'Allow NULL'

Does usage of NULL to fill empty cells on my tables make searches / queries faster?

For example is this

90  2   65  2011-04-08  NULL    NULL    
134 2   64  2011-04-13  NULL    07:00:00    
135 2   64  2011-04-13  NULL    07:00:00    
136 2   64  2011-04-13  NULL    22:45:00    
137 2   64  2011-04-14  NULL    19:30:00

better than

90  2   65  2011-04-08          
134 2   64  2011-04-13           07:00:00   
135 2   64  2011-04-13           07:00:00   
136 2   64  2011-04-13           22:45:00   
137 2   64  2011-04-14           19:30:00

If anyone could tell me any specific advantage to using NULL (performance, good practice, etc) that would be much appreciated.

Upvotes: 1

Views: 1665

Answers (1)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385325

There is a semantic difference.

  • NULL means "value unknown or not applicable".

If this describes the data for that column, then use it.

  • The empty string means "value known, and it's 'nothingness'".

If this describes the data for that column, then use it. Of course, this applies only to strings; it's unusual to have such a scenario for other data types, but usually for numeric fields a value of 0 would be appropriate here.

In short, it depends mostly on what your fields mean. Worry about performance and profiling later, when you know what data you're representing. (NULL vs "" is never going to be your bottleneck, though.)

Upvotes: 4

Related Questions