简体   繁体   中英

MySQL not using index to sort on and select a value from an indexed column

The Table

https://codex.wordpress.org/Database_Description

CREATE TABLE wp_terms (
    term_id    bigint(20) unsigned auto_increment,
    name       varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci NOT NULL DEFAULT '',
    slug       varchar(200),
    term_group bigint(10) DEFAULT 0,

    PRIMARY KEY ( term_id )
) ENGINE=InnoDB;

The Index

MySQL> show index from wp_terms;
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| Table    | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+
| wp_terms |          1 | name     |            1 | name        | A         |      716638 |      191 |   NULL |      | BTREE      |         |               | YES     | NULL       |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+---------+------------+

The Query

MySQL> select name from wp_terms order by name limit 1;
+--------------------+
| name               |
+--------------------+
|  ****************  |
+--------------------+
1 row in set (0.83 sec)

The Explain

MySQL> explain select name from wp_terms order by name limit 1;
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+----------------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra          |
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+----------------+
|  1 | SIMPLE      | wp_terms | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 802726 |   100.00 | Using filesort |
+----+-------------+----------+------------+------+---------------+------+---------+------+--------+----------+----------------+

The Question

Why isn't the 'name' index even a possible key? Forcing it had no effect.

Thank you in advance.

Thanks to @Dai for pointing out the issue. 'name' was an index prefix, not a full index. Creating a full index on name resulted in index being used for the query.

MySQL> alter table wp_terms add index(name);
MySQL> explain select name from wp_terms order by name limit 1;
+----+-------------+----------+------------+-------+---------------+--------+---------+------+------+----------+-------------+
| id | select_type | table    | partitions | type  | possible_keys | key    | key_len | ref  | rows | filtered | Extra       |
+----+-------------+----------+------------+-------+---------------+--------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | wp_terms | NULL       | index | NULL          | name_2 | 802     | NULL |    1 |   100.00 | Using index |
+----+-------------+----------+------------+-------+---------------+--------+---------+------+------+----------+-------------+

Thank you!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM