SLEEP( )

This MySQL function pauses the execution of an SQL statement in which it is given, for the number of seconds given.

Syntax

Below is the syntax for this command. Text within square brackets (i.e, [ and ] ) are optional. Choices are separated by bars (i.e, | ). Ellipses preceded by a comma indicates a repeating pattern. Ellipses before or after syntax are used to highlight a relevant except from the larger syntax. Text displayed in italic letters represent text that should be replaced with the specific names related to the database (e.g., column with the name of the column).

SLEEP(seconds)

Explanation

This function pauses the execution of an SQL statement in which it is given, for the number of seconds given. It returns 0 in the results if successful; 1 if not. This function became available as of version 5.0.12 of MySQL. It's not exactly a time and date function, but it's included here due to it's true time aspects.

Examples

SELECT SYSDATE( ) AS 'Start',
SLEEP(5) AS 'Pause',
SYSDATE( ) AS 'End';

+---------------------+-------+---------------------+
| Start               | Pause | End                 |
+---------------------+-------+---------------------+
| 2008-07-16 13:50:20 |     0 | 2008-07-16 13:50:25 | 
+---------------------+-------+---------------------+
1 row in set (5.13 sec)

The SYSDATE( ) function returns the time when it is executed, not necessarily when the statement started or finished. You can see that in the first field, the time is five seconds different from the results in the third field due to the use of SLEEP( ). Notice also that the statement took a little over five seconds to execute.

If you type CTRL-C one time before an SQL statement containing SLEEP( ) is completed, it will return 1 for the SLEEP( ) field and MySQL will then go on to execute the rest of the SQL statement. In that case, in the previous example, the third field would show less than a five-second difference from the first.