Pete Freitag Pete Freitag

Insert Delayed with MySQL

Updated on March 06, 2024
By Pete Freitag
databases

Update 2024

This post about MySQL's INSERT DELAYED feature was written back in 2005, now nearly 20 years later, I'd like to provide some updates on where this feature stands.

As of MySQL version 5.6 delayed inserts have been deprecated. MySQL version 8.3 has removed support for the DELAYED keyword. While it does not throw a syntax error, the keyword is ignored and the mysql server just performs a regular insert. It will also trigger a warning:

ER_WARN_LEGACY_SYNTAX_CONVERTED warning: INSERT DELAYED is no longer supported. The statement was converted to INSERT

Keep in mind that future versions of MySQL may remove the insert delated feature.

Now here's some info about the feature, that I wrote a long long time ago.

I just discovered that MySQL has some handy optimization hints, and commands that you can pass in your SQL statements. Since there is a lot to cover with each tip, I will be posting them in separate blog entries. I have already written about the MySQL Query Cache optimization hints.

Delayed Inserts

You can delay INSERT's from happening until the table is free by using the DELAYED hint in your SQL statement. For example:

INSERT DELAYED INTO table (col) VALUES ('val');

The above SQL statement will return quickly, and it stores the insert statement in a memory queue until the table your inserting into is free from reads. This means that if there are multiple inserts in the queue they can be written in one block, which is a more optimal use of IO.

The downside to this is that it is not transactionally safe at all. You don't really know how long its going to take for your INSERT to happen. If the server crashes, or is forcefully shutdown you will loose your INSERTs. So don't use this on any critical information that would suck to loose.

One great use for the DELAYED keyword would be for storing web stats in a database. You don't want the client waiting for the stats to insert, and its not that big of a deal if you loose a few stats (for most people).

Status Variables

The following status variables can be used to monitor your delayed inserts:

Delayed_errors
Delayed_insert_threads
Delayed_writes
Not_flushed_delayed_rows

You can get the values by running the SQL statement: SHOW STATUS



mysql insert delayed performance sql optimization databases

Insert Delayed with MySQL was first published on August 02, 2005.

If you like reading about mysql, insert, delayed, performance, sql, optimization, or databases then you might also like:

Discuss / Follow me on Twitter ↯