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)
  9. -- without interruption
  10. mysql> SELECT SLEEP(1000);
  11. +-------------+
  12. | sleep(1000) |
  13. +-------------+
  14. | 0 |
  15. +-------------+
  16. 1 row in set (18 min 20.87 sec)
  17. create table t1 (a int,b int);
  18. insert into t1 values (1,1),(1,null);
  19. mysql> select sleep(a) from t1;
  20. +----------+
  21. | sleep(a) |
  22. +----------+
  23. | 0 |
  24. | 0 |
  25. +----------+
  26. 2 rows in set (2.01 sec)