Skip to content

SLEEP()

Function Description

The SLEEP() function pauses (sleep) the specified number of seconds for the current query. The result will return 0.

Function Syntax

>
SLEEP(duration)

Parameter definition

Parameters Description
duration Required. The length of sleep in seconds. It should be greater than or equal to 0 and can take a fractional part.

Return value

  • When SLEEP() returns normally (without interruption), it returns 0.

  • When the interrupted query calls unique results, it returns 1 and the query itself does not return an error.

Examples are as follows:

  1. Execute the following command in session 1, query the current connection_id, and execute the SLEEP() function:

    mysql> select connection_id();
    +-----------------------+
    | connection_id() |
    +-----------------------+
    | 1476 |
    +-----------------------+
    1 row in set (0.03 sec)
    mysql> select sleep(200);
    
  2. At this time, open a new session, interrupt session 1, and execute the following command:

    mysql> kill 1476;
    Query OK, 0 rows affected (0.00 sec)
    
    1. View the results of session 1:

      mysql> select sleep(200);
      +----------------+
      | sleep(200) |
      +----------------+
      | 1 |
      +----------------+
      1 row in set (26.50 sec)
      
  3. SLEEP() returns an error when some query is interrupted, for example:

mysql> SELECT 1 FROM t1 WHERE SLEEP(1000);
ERROR 20101 (HY000): internal error: pipeline closed unexpectedly

Example

-- without interruption
mysql> SELECT SLEEP(1);
+-------------+
| sleep(1) |
+-------------+
| 0 |
+-------------+
1 row in set (1.01 sec)

-- without interruption
mysql> SELECT SLEEP(1000);
+-----------------+
| sleep(1000) |
+-----------------+
| 0 |
+-----------------+
1 row in set (18 min 20.87 sec)

create table t1 (a int,b int);
insert into t1 values ​​(1,1),(1,null);
mysql> select sleep(a) from t1;
+-------------+
| sleep(a) |
+-------------+
| 0 |
| 0 |
+-------------+
2 rows in set (2.01 sec)