MySQL SLEEP() Function

In MySQL, the SLEEP() function pauses (sleeps) the current query for the specified number of seconds.

SLEEP() Syntax

Here is the syntax of the MySQL SLEEP() function:

SLEEP(duration)

Parameters

duration
Required. The sleep duration in seconds. It should be greater than or equal to 0, and can have a fractional part.

Return value

The MySQL SLEEP() function pauses (sleeps) the query for the specified number of seconds, then returns 0, or returns 1 if the pause was interrupted.

The SLEEP() function will generate a warning if the argument duration is negative or NULL, or an error in strict mode.

If SLEEP() is the only thing called in a query, it returns 1 after being interrupted.

If SLEEP() is a part of a query, it returns an error after being interrupted.

SLEEP() Examples

This example is combined with the SYSDATE() function to demonstrate the functionality of SLEEP().

SELECT
    SYSDATE(),
    SLEEP(10),
    SYSDATE();
+---------------------+-----------+---------------------+
| SYSDATE()           | SLEEP(10) | SYSDATE()           |
+---------------------+-----------+---------------------+
| 2022-05-07 22:03:22 |         0 | 2022-05-07 22:03:32 |
+---------------------+-----------+---------------------+
1 row in set (10.07 sec)

Here, we saw the following things:

  • SLEEP(10) returned 0.
  • The return value of the second SYSDATE() function is 10 seconds later than the first SYSDATE() function. This is because the middle SLEEP(10) pauses the query for 10 seconds.
  • 1 row in set (10.07 sec) told us that the entire query took a total of 10.7 seconds. If there is not a SLEEP(10) in the statement, it would be fast.