Fetching of attributes in WordPress (using MySQL) seems to be slower than necessary.
(This is a self-answered question, so proceed to my answer.)
Fetching of attributes in WordPress (using MySQL) seems to be slower than necessary.
(This is a self-answered question, so proceed to my answer.)
The standard schema for
wp_postmeta
provides poor indexes. This leads to performance problems.By changing the schema to this, most references to meta data will be faster:
Notes:
AUTO_INCREMENT
column is a waste of space, and slows down queries because it is thePRIMARY KEY
, thereby eschewing the "natural" "composite" PK of(post_id, meta_key)
.meta_key
fromVARCHAR(255)
, notVARCHAR(191)
. (We can discuss the reasons, and workarounds, in a separate question, if 191 is not sufficient.)INDEX(meta_key)
is optional, but needed if you want to "find posts that have a particular key".If you would like to present your
CREATE TABLE
, I can provide anALTER
to convert it to this.If you need the ability to have multiple meta keys with the same key name for one post, then use this solution. It is nearly as good as the above suggestion.
Source doc
Possible ALTER
Caveats:
meta_id
because some WP user pointed out that it is referenced by other tables.SELECTs
involving postmeta.The SQL: