zoukankan      html  css  js  c++  java
  • MySQL--SELECT语句中SLEEP函数执行时机

    问题描述

    当SELECT语句中使用SLEEP时,何时触发SLEEP操作?

    模拟测试

    mysql> show create table tb1001 G
    *************************** 1. row ***************************
           Table: tb1001
    Create Table: CREATE TABLE `tb1001` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `c1` int(11) NOT NULL,
      `c2` int(11) NOT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `UNI_C1` (`c1`)
    ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8
    1 row in set (0.01 sec)
    
    
    mysql> select sleep(1),id,c1 from tb1001 where c2<2 limit 3;
    +----------+----+----+
    | sleep(1) | id | c1 |
    +----------+----+----+
    |        0 |  1 |  1 |
    +----------+----+----+
    1 row in set (1.01 sec)
    
    mysql> select sleep(1),id,c1 from tb1001 where c2<1;
    Empty set (0.00 sec)
    
    mysql> select sleep(1),id,c1 from tb1001 where c2<2;
    +----------+----+----+
    | sleep(1) | id | c1 |
    +----------+----+----+
    |        0 |  1 |  1 |
    +----------+----+----+
    1 row in set (1.00 sec)
    
    mysql> select sleep(1),id,c1 from tb1001 where c2<3;
    +----------+----+----+
    | sleep(1) | id | c1 |
    +----------+----+----+
    |        0 |  1 |  1 |
    |        0 |  2 |  2 |
    +----------+----+----+
    2 rows in set (2.00 sec)
    
    mysql> select sleep(1),id,c1 from tb1001 where c2<4;
    +----------+----+----+
    | sleep(1) | id | c1 |
    +----------+----+----+
    |        0 |  1 |  1 |
    |        0 |  2 |  2 |
    |        0 |  3 |  3 |
    +----------+----+----+
    3 rows in set (3.00 sec)
    
    
    

    测试分析

    从上面的测试结果看,由于C2上没有索引,需要全表扫描,查询执行时间和查询返回的数据行记录成正比,与查询扫描的记录行数无关。

    在<深入理解MySQL主从原理>书中描述:

    • 每当InnoDB层返回一行数据经过WHERE条件判断后,都会触发Sleep函数,也就是经过WHERE条件过滤的数据,在发送给客户端前都会进行异常SLEEP操作。

    扩展知识

    可以通过LIMIT来限制发送给客户端的操作,因此可以通过SLEEP+LIMIT操作来控制语句执行时间,方便模拟执行时间较长的SQL。

    mysql> select sleep(1),id from tb1001 limit 1;
    +----------+----+
    | sleep(1) | id |
    +----------+----+
    |        0 |  1 |
    +----------+----+
    1 row in set (1.01 sec)
    
    mysql> select sleep(1),id from tb1001 limit 2;
    +----------+----+
    | sleep(1) | id |
    +----------+----+
    |        0 |  1 |
    |        0 |  2 |
    +----------+----+
    2 rows in set (2.00 sec)
    
    
  • 相关阅读:
    【转】全文检索引擎Sphinx配置文件详细介绍
    【转】构建不依赖于cookie的手机端用户登录机制
    Sphinx在window下的初步安装和配置
    Zend Framework 在.htaccess中修改RewriteRule实现url重写
    做后台的程序猿的一点心得
    [Leetcode 75] 96 Unique Binary Search Trees
    [Leetcode 74] 92 Restore IP Addresses
    [Leetcode 73] 92 Reverse Linked List II
    [Leetcode 72] 91 Decode Ways
    [Leetcode 71] 86 Partition List
  • 原文地址:https://www.cnblogs.com/gaogao67/p/14655347.html
Copyright © 2011-2022 走看看