Mysql-Explain(七):輸出列-ref、rows

簡介

ref 哪些列或者常量被用做索引列上的值
rows 根據表的統計信息和索引的使用情況,大致估算查詢結果所需要讀取記錄的行數

演示

  • ref:哪些列或者常量被用做索引列上的值

    mysql> explain select * from student where school_id = 1;
    +----+-------------+---------+------------+------+---------------------+---------------------+---------+-------+------+----------+-------+
    | id | select_type | table   | partitions | type | possible_keys       | key                 | key_len | ref   | rows | filtered | Extra |
    +----+-------------+---------+------------+------+---------------------+---------------------+---------+-------+------+----------+-------+
    |  1 | SIMPLE      | student | NULL       | ref  | ik_schoolId_majorId | ik_schoolId_majorId | 5       | const | 1960 |   100.00 | NULL  |
    +----+-------------+---------+------------+------+---------------------+---------------------+---------+-------+------+----------+-------+
    1 row in set, 1 warning (0.07 sec)
    

    school_id =1,1是常量,ref=const

    mysql> explain select * from student left join school on student.school_id = school.id;
    +----+-------------+---------+------------+--------+---------------+---------+---------+------------------------+---------+----------+-------+
    | id | select_type | table   | partitions | type   | possible_keys | key     | key_len | ref                    | rows    | filtered | Extra |
    +----+-------------+---------+------------+--------+---------------+---------+---------+------------------------+---------+----------+-------+
    |  1 | SIMPLE      | student | NULL       | ALL    | NULL          | NULL    | NULL    | NULL                   | 1994142 |   100.00 | NULL  |
    |  1 | SIMPLE      | school  | NULL       | eq_ref | PRIMARY       | PRIMARY | 4       | mydb.student.school_id |       1 |   100.00 | NULL  |
    +----+-------------+---------+------------+--------+---------------+---------+---------+------------------------+---------+----------+-------+
    2 rows in set, 1 warning (0.00 sec)
    

    上面例子ref的值是mydb.student.school_id,因爲表school被關聯查詢的時候,使用了主鍵索引,而值則使用了驅動表的mydb.student.school_id列。

  • rows:根據表的統計信息和索引的使用情況,大致估算查詢結果所需要讀取記錄的行數;filtered:表示示存儲引擎返回的數據在server層過濾後,剩下多少滿足查詢的記錄數量的比例

    mysql> explain select * from student where school_id < 4 and major_id = 3;
    +----+-------------+---------+------------+------+--------------------------------+------------+---------+-------+------+----------+-------------+
    | id | select_type | table   | partitions | type | possible_keys                  | key        | key_len | ref   | rows | filtered | Extra       |
    +----+-------------+---------+------------+------+--------------------------------+------------+---------+-------+------+----------+-------------+
    |  1 | SIMPLE      | student | NULL       | ref  | ik_schoolId_majorId,ik_majorId | ik_majorId | 5       | const | 4076 |     0.77 | Using where |
    +----+-------------+---------+------------+------+--------------------------------+------------+---------+-------+------+----------+-------------+
    1 row in set, 1 warning (0.07 sec)
    

    上面explain的分析結果顯示,在存儲引擎層面上大概需要掃描4076行記錄,使用的索引是ik_majorId,所以數據在server層還需要對shcool_id條件進行過濾,過濾後的匹配比例是0.77。在我們平時的sql優化中rows一般是越小越好,代表所需掃描的表記錄少,而filtered是越大越好,表明掃描的無用數據少,沒有過多的多餘的IO消耗。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章