Skip to content

Added postmeta index for meta_key and meta_value to improve lookup query performance.#84

Draft
sun wants to merge 1 commit into
masterfrom
fix/postmeta-lookup-query-performance-sun
Draft

Added postmeta index for meta_key and meta_value to improve lookup query performance.#84
sun wants to merge 1 commit into
masterfrom
fix/postmeta-lookup-query-performance-sun

Conversation

@sun

@sun sun commented Jun 30, 2023

Copy link
Copy Markdown
Member

Task

Description

  • Lookup queries on specific meta fields in a very large database can be slow.
  • core-standards already adds the meta_value index, but it only covers some strange cases in core and contributed plugins that are filtering by meta_value column only (e.g. to retrieve all distinct values) or the value is so unique among all meta fields that it doesn't require further filtering by key; e.g., a URL.
  • The much more common case are lookup queries checking a particular meta_key for a particular meta_value.
  • MySQL/MariaDB still uses the meta_value index for those, but EXPLAIN states that it still has to go through thousands of rows to reduce them to the given meta key, which is slow.

Examples

TODO: Need to find the actual queries that triggered this – for now here's a trivial:

$ wp db query "EXPLAIN SELECT post_id FROM wp_postmeta WHERE meta_key = '_pub_id' AND meta_value = '12345'"
+------+-------------+-------------+------+---------------------+------------+---------+-------+------+-------------+
| id   | select_type | table       | type | possible_keys       | key        | key_len | ref   | rows | Extra       |
+------+-------------+-------------+------+---------------------+------------+---------+-------+------+-------------+
|    1 | SIMPLE      | wp_postmeta | ref  | meta_key,meta_value | meta_value | 243     | const | 1    | Using where |
+------+-------------+-------------+------+---------------------+------------+---------+-------+------+-------------+

("using where" should ideally say "using index"; the real cases had 4k rows, the new index reduced them to <100)

We can see that MariaDB 10.4+ already has a built-in optimization, which doesn't need the new index, because it's using a second index to filter the first:

$ wp db query "EXPLAIN SELECT post_id FROM wp_postmeta WHERE meta_key = '_pub_id' AND meta_value = '12345'"
+------+-------------+-------------+------------+------------------------------------+---------------------+---------+-------+--------+---------------------------------+
| id   | select_type | table       | type       | possible_keys                      | key                 | key_len | ref   | rows   | Extra                           |
+------+-------------+-------------+------------+------------------------------------+---------------------+---------+-------+--------+---------------------------------+
|    1 | SIMPLE      | wp_postmeta | ref|filter | meta_key,meta_value,meta_key_value | meta_key|meta_value | 767|243 | const | 1 (0%) | Using where; Using rowid filter |
+------+-------------+-------------+------------+------------------------------------+---------------------+---------+-------+--------+---------------------------------+

@sun sun self-assigned this Jun 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant