MariaDB SLEEP() Function
In MariaDB, SLEEP() is a built-in function that pauses (sleeps) the current query for a specified number of seconds.
MariaDB SLEEP() Syntax
Here is the syntax of the MariaDB SLEEP() function:
SLEEP(duration)
Parameters
duration-
Optional. Sleep duration in seconds. It should be greater than or equal to 0 and can have a fractional part.
If you supply the wrong number of arguments, MariaDB will report an error: ERROR 1582 (42000): Incorrect parameter count in the call to native function 'SLEEP'.
Return value
The MariaDB SLEEP() function will pause (sleep) the query for the specified number of seconds before returning 0. The function will return 1 if the pause was interrupted.
If the argument duration is negative or NULL, the SLEEP() function will not work.
MariaDB SLEEP() Examples
Basic examples
This statement shows the usage of the MariaDB SLEEP() function:
SELECT
SYSDATE(),
SLEEP(5),
SYSDATE();
Output:
+---------------------+----------+---------------------+
| SYSDATE() | SLEEP(5) | SYSDATE() |
+---------------------+----------+---------------------+
| 2023-02-03 15:41:18 | 0 | 2023-02-03 15:41:23 |
+---------------------+----------+---------------------+
1 row in set (5.013 sec)In this example, SLEEP(5) we paused the query for 5 seconds with , so the output of the second is 5 seconds behind SYSDATE() the output of the first.
Fractional seconds
The MariaDB SLEEP() function allows you to provide an argument with a decimal,
SELECT
SYSDATE(6),
SLEEP(0.1),
SYSDATE(6);
Output:
+----------------------------+------------+----------------------------+
| SYSDATE(6) | SLEEP(0.1) | SYSDATE(6) |
+----------------------------+------------+----------------------------+
| 2023-02-03 15:46:18.142459 | 0 | 2023-02-03 15:46:18.251445 |
+----------------------------+------------+----------------------------+NULL or negative number
If the argument is negative or NULL, the SLEEP() function will not stall.
SELECT SLEEP(NULL), SLEEP(-1);
Output:
+-------------+-----------+
| SLEEP(NULL) | SLEEP(-1) |
+-------------+-----------+
| 0 | 0 |
+-------------+-----------+
1 row in set (0.000 sec)Conclusion
In MariaDB, SLEEP() is a built-in function that pauses (sleeps) the current query for a specified number of seconds.