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

Empty LONGTEXT or NULL?

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

Problem

In my MySQL DB I have one field called html_contents that contains all the html to be shown in a webpage. Obviously the html could be huge, and certaintly bigger than 64KB, therefore I decided to use LONGTEXT rather than VARCHAR.

When the field is not set or left empty would you say it is better (alias more efficient for the DB) to set it to NULL or to empty string?

I read this: When to use NULL and when to use an empty string? but it talks about empty strings in general (probably small strings and not LONGTEXT).

I was wondering if with LONGTEXT is a different story, whether it saves a lot of space or execution time to use NULL instead of leaving empty LONGTEXT around.

Solution

A good starting point to make a decision in order to use or not this kind of value is the MySQL official manual.

In short:

Optimization


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.

Problems with NULL values


In SQL, the NULL value is never true in comparison to any other value,
even NULL. An expression that contains NULL always produces a NULL
value unless otherwise indicated in the documentation for the
operators and functions involved in the expression


When reading data with LOAD DATA INFILE, empty or missing columns are
updated with ''. To load a NULL value into a column, use \N in the
data file

Context

StackExchange Database Administrators Q#28086, answer score: 3

Revisions (0)

No revisions yet.