SLEEP()

函数说明

SLEEP() 函数将当前查询暂停(睡眠)指定的秒数。结果将返回 0。

函数语法

1. >
2. SLEEP(duration)

参数释义

参数 说明
duration 必需的。 以秒为单位的睡眠时长。它应该大于或等于 0,并且可以带有小数部分。

返回值

  • 当 SLEEP() 正常返回时(没有中断),它返回 0。
  • 当 SLEEP() 被中断的查询调用唯一的结果时,它返回 1 并且查询本身不返回错误。

示例如下:

  1. 在会话 1 中执行下面的命令,查询当前的 connection_id,并执行 SLEEP() 函数:

1. mysql> select connection_id(); 2. +-----------------+ 3. | connection_id() | 4. +-----------------+ 5. | 1476 | 6. +-----------------+ 7. 1 row in set (0.03 sec) 8. mysql> select sleep(200); 2. 此时,打开一个新的会话,中断会话 1,执行如下命令:

1. mysql> kill 1463; 2. Query OK, 0 rows affected (0.00 sec)

  1. 查看会话 1 的结果:

    1. mysql> select sleep(200); 2. +------------+ 3. | sleep(200) | 4. +------------+ 5. | 1 | 6. +------------+ 7. 1 row in set (26.50 sec) 3. 部分查询被打断时,SLEEP() 返回错误,例如:

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

示例

1. -- without interruption
2. mysql> SELECT SLEEP(1);
3. +----------+
4. | sleep(1) |
5. +----------+
6. |        0 |
7. +----------+
8. 1 row in set (1.01 sec)

10. -- without interruption
11. mysql> SELECT SLEEP(1000);
12. +-------------+
13. | sleep(1000) |
14. +-------------+
15. |           0 |
16. +-------------+
17. 1 row in set (18 min 20.87 sec)

19. create table t1 (a int,b int);
20. insert into t1 values (1,1),(1,null);
21. mysql> select sleep(a) from t1;
22. +----------+
23. | sleep(a) |
24. +----------+
25. |        0 |
26. |        0 |
27. +----------+
28. 2 rows in set (2.01 sec)