2

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.

9
  • What is the exact column-type specification of wp_terms.name? (including NULL / NOT NULL) Commented Sep 23, 2022 at 2:37
  • What version of MySQL are you using? And what storage-engine are you using for the wp_terms table? Commented Sep 23, 2022 at 2:38
  • I have no explanation: Large table, index on name, with high cardinality should mean the index is being used. Commented Sep 23, 2022 at 2:39
  • 2
    @TimBiegeleisen I noticed the index's sub_part is 191 (and not NULL), which means the column is only partially indexed, could that be why? Commented Sep 23, 2022 at 2:42
  • @Dai 8.0.mysql_aurora.3.02.1 innodb Commented Sep 23, 2022 at 2:42

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

10 Comments

How did you create the prefix-index in the first place?
@Dai I inherited the DB... just working on optimizing it.
Working on a heavily-customized WordPress install? You have my sympathy.
Haha, thanks! Getting it done though. Amazing what can be achieved with a few well targeted indexes! This one had me stumped though. TIL about index prefixes!
I avoid MySQL as much as I can, so pardon my ignorance, but just what is an index-prefix? (I honestly don't know, I used this QA as an opportunity to read-up on what EXPLAIN reports, my (upvoted) comments were from just reading the docs) - I don't think I've ever come across anything similar in any other RDBMS.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.