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快起来,只有几毫秒的查询时间。

  • 相关阅读:
    执行eclipse,迅速failed to create the java virtual machine。
    hdu4000 && hrbust1625
    linux高级技巧:heartbeat+lvs(一)
    Android-它们的定义Dialog
    @repository注解
    常用myeclipse的快捷键,对菜鸟超有用的
    myEclipse快捷键
    JDK 1.6.0和 6.0 有啥区别,JavaTM SE 6 的平台名字和版本号的说明(转)
    Cannot return from outside a function or method
    eclipse报错 com/genuitec/eclipse/j2eedt/core/J2EEProjectUtil 转
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/10135777.html
Copyright © 2011-2022 走看看