zoukankan      html  css  js  c++  java
  • 复合索引,排第一的效果

    mysql> select count(*) from tf_user_index where  age = 30;
    +----------+
    | count(*) |
    +----------+
    |   196477 |
    +----------+
    1 row in set (1.66 sec)
    
    mysql> select count(*) from tf_user_index where  score = 30;
    +----------+
    | count(*) |
    +----------+
    |   126306 |
    +----------+
    1 row in set (0.03 sec)
    
    mysql> explain select count(*) from tf_user_index where  age = 30;
    +----+-------------+---------------+-------+---------------+---------------+---------+------+---------+--------------------------+
    | id | select_type | table         | type  | possible_keys | key           | key_len | ref  | rows    | Extra                    |
    +----+-------------+---------------+-------+---------------+---------------+---------+------+---------+--------------------------+
    |  1 | SIMPLE      | tf_user_index | index | NULL          | score_sex_age | 6       | NULL | 8962455 | Using where; Using index |
    +----+-------------+---------------+-------+---------------+---------------+---------+------+---------+--------------------------+
    1 row in set (0.00 sec)
    
    mysql> explain select count(*) from tf_user_index where  score = 30;
    +----+-------------+---------------+------+---------------+---------------+---------+-------+--------+-------------+
    | id | select_type | table         | type | possible_keys | key           | key_len | ref   | rows   | Extra       |
    +----+-------------+---------------+------+---------------+---------------+---------+-------+--------+-------------+
    |  1 | SIMPLE      | tf_user_index | ref  | score_sex_age | score_sex_age | 4       | const | 241442 | Using index |
    +----+-------------+---------------+------+---------------+---------------+---------+-------+--------+-------------+
    1 row in set (0.01 sec)
    
    

    score排第一,查询时间很快。age排第三,查询要1秒多时间。

    mysql> select count(*) from tf_user where  score = 30;
    +----------+
    | count(*) |
    +----------+
    |   126306 |
    +----------+
    1 row in set (3.37 sec)
    
    mysql> select count(*) from tf_user where  age = 30;
    +----------+
    | count(*) |
    +----------+
    |   196477 |
    +----------+
    1 row in set (2.44 sec)
    
    

    没有复合索引,都需要好几秒时间。复合索引有一定的效果,但是单独查询时,对排第一的更有效。

    调换一下顺序,现在将age排第一。

    mysql> select count(*) from tf_user_index where  age = 30;
    +----------+
    | count(*) |
    +----------+
    |   196477 |
    +----------+
    1 row in set (0.05 sec)
    
    mysql> select count(*) from tf_user_index where  score = 30;
    +----------+
    | count(*) |
    +----------+
    |   126306 |
    +----------+
    1 row in set (1.82 sec)
    
    mysql> explain select count(*) from tf_user_index where  score = 30;
    +----+-------------+---------------+-------+---------------+---------------+---------+------+---------+--------------------------+
    | id | select_type | table         | type  | possible_keys | key           | key_len | ref  | rows    | Extra                    |
    +----+-------------+---------------+-------+---------------+---------------+---------+------+---------+--------------------------+
    |  1 | SIMPLE      | tf_user_index | index | NULL          | age_score_sex | 6       | NULL | 8962455 | Using where; Using index |
    +----+-------------+---------------+-------+---------------+---------------+---------+------+---------+--------------------------+
    1 row in set (0.00 sec)
    
    mysql> explain select count(*) from tf_user_index where  age = 30;
    +----+-------------+---------------+------+---------------+---------------+---------+-------+--------+-------------+
    | id | select_type | table         | type | possible_keys | key           | key_len | ref   | rows   | Extra       |
    +----+-------------+---------------+------+---------------+---------------+---------+-------+--------+-------------+
    |  1 | SIMPLE      | tf_user_index | ref  | age_score_sex | age_score_sex | 1       | const | 408456 | Using index |
    +----+-------------+---------------+------+---------------+---------------+---------+-------+--------+-------------+
    1 row in set (0.00 sec)
    
    

    排第一很重要。

    mysql> explain select count(*) from tf_user_index where  age = 30 and score =30;
    +----+-------------+---------------+------+---------------+---------------+---------+-------------+------+-------------+
    | id | select_type | table         | type | possible_keys | key           | key_len | ref         | rows | Extra       |
    +----+-------------+---------------+------+---------------+---------------+---------+-------------+------+-------------+
    |  1 | SIMPLE      | tf_user_index | ref  | age_score_sex | age_score_sex | 5       | const,const | 2467 | Using index |
    +----+-------------+---------------+------+---------------+---------------+---------+-------------+------+-------------+
    1 row in set (0.00 sec)
    
    mysql> select count(*) from tf_user_index where  age = 30 and score =30;
    +----------+
    | count(*) |
    +----------+
    |     2468 |
    +----------+
    1 row in set (0.00 sec)
    
    mysql> select count(*) from tf_user_index where  score =30 and age = 30;
    +----------+
    | count(*) |
    +----------+
    |     2468 |
    +----------+
    1 row in set (0.01 sec)
    
    mysql> select count(*) from tf_user_index where  score =30 and sex = 2;
    +----------+
    | count(*) |
    +----------+
    |    63031 |
    +----------+
    1 row in set (2.06 sec)
    
    

    只要带到age,查询就很快。

    联合索引中,第一个位置很重要。

  • 相关阅读:
    牛券
    探险
    雷达安装
    智力大冲浪
    奶牛玩杂技
    BJWC2008 秦腾与教学评估
    JSOI2010 部落划分
    作诗
    ASP.NET MVC4系列验证机制、伙伴类共享源数据信息(数据注解和验证)
    正则表达式
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/10137051.html
Copyright © 2011-2022 走看看