zoukankan      html  css  js  c++  java
  • 100W数据,测试索引

    两张表,结构相同,数据内容相同。唯一不同的就是是否包含索引。tf_user_index表中包含索引。
    这100w数据,我造了近一天时间。

    mysql> select count(*) from tf_user_index where score = 30;
    +----------+
    | count(*) |
    +----------+
    |   126306 |
    +----------+
    1 row in set (0.03 sec)
    
    mysql> select count(*) from tf_user where score = 30;
    +----------+
    | count(*) |
    +----------+
    |   126306 |
    +----------+
    1 row in set (1.94 sec)
    
    

    score字段,加了索引。

    mysql> select count(*) from tf_user_index where score > 80;
    +----------+
    | count(*) |
    +----------+
    |  2525607 |
    +----------+
    1 row in set (0.49 sec)
    
    mysql> select count(*) from tf_user where score > 80;
    +----------+
    | count(*) |
    +----------+
    |  2525607 |
    +----------+
    1 row in set (2.02 sec)
    
    

    很明显,加了索引的查询要快很多。
    sql会根据条件去索引中查找,然后去表中查找。如果在索引中匹配的越多,那么查找的时间就越长,索引的意义就越小。
    看下面的例子,

    mysql> select count(*) from tf_user_index where score > 0;
    +----------+
    | count(*) |
    +----------+
    | 10000000 |
    +----------+
    1 row in set (1.91 sec)
    
    mysql> select count(*) from tf_user where score > 0;
    +----------+
    | count(*) |
    +----------+
    | 10000000 |
    +----------+
    1 row in set (2.03 sec)
    
    

    大于0,基本上匹配了所有的数据,索引的意义就不大了。

    大数据实验,才能更好的看出效果。让微观世界宏观化。

    下面看看,sex性别这个字段。

    mysql> select count(*) from tf_user where sex = 1;
    +----------+
    | count(*) |
    +----------+
    |  5001500 |
    +----------+
    1 row in set (2.29 sec)
    
    mysql> select count(*) from tf_user_index where sex = 1;
    +----------+
    | count(*) |
    +----------+
    |  5001500 |
    +----------+
    1 row in set (1.04 sec)
    
    

    加索引也还是有点提升速度的。但是意义不是很大,因为每次查询,基本上都是要扫描一半的数据表。总体来说,还是有一定的提升的。效果不是那么明显,比如上面的score快起来,只有几毫秒的查询时间。

  • 相关阅读:
    [PHP] class_exists类不存在时会调用__autoload函数
    [Redis] Redis的消息机制- 发布订阅
    [发电] 现在正式入驻爱发电平台
    [MySQL] PHP IP登录限制的实现
    [Redis] 哈希表的Rehash机制
    [Redis] redis的hash类型底层结构哈希表
    [Linux] ls命令的几个常用参数实现按时间/文件大小排序
    [Go] 在gin框架gorm下查询一对多的数据
    [Redis] list底层的数据结构
    [GO]go redis实现滑动窗口限流-redis版
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/10135777.html
Copyright © 2011-2022 走看看