5

Fetching of attributes in WordPress (using MySQL) seems to be slower than necessary.

(This is a self-answered question, so proceed to my answer.)

0

1 Answer 1

16

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:

CREATE TABLE wp_postmeta (
    post_id …,
    meta_key …,
    meta_value …,
    PRIMARY KEY(post_id, meta_key),
    INDEX(meta_key)
) ENGINE=InnoDB;

Notes:

  • The current AUTO_INCREMENT column is a waste of space, and slows down queries because it is the PRIMARY KEY, thereby eschewing the "natural" "composite" PK of (post_id, meta_key).
  • InnoDB further boosts the performance of that PK due to "clustering". (I hope you are not still using MyISAM!)
  • If you are using MySQL 5.6 (Or MariaDB 10.0 or 10.1), change meta_key from VARCHAR(255), not VARCHAR(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".
  • Caveat: These changes will speed up many uses of postmeta, but not all. I don't think it will slow down any use cases. (Please provide such queries if you encounter them. It could be a caching issue, not ar real degradation.)

If you would like to present your CREATE TABLE, I can provide an ALTER 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.

    meta_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,  -- keep after all
    ...
    PRIMARY KEY(post_id, meta_key, meta_id),  -- to allow dup meta_key for a post

Source doc

Possible ALTER

Caveats:

  • I have no way to test this.
  • This does not address the 767 error
  • This keeps meta_id because some WP user pointed out that it is referenced by other tables.
  • It assumes you might have multiple rows for a (post_id, meta_key) combo. (This seems like poor schema design?)
  • All this does is speed up typical SELECTs involving postmeta.
  • This probably applies to woocommerce, too.
  • If you use this, please dump your database and be ready to reload it in case of trouble.

The SQL:

ALTER TABLE wp_postmeta
    DROP PRIMARY KEY,
    DROP INDEX post_id,
    ADD PRIMARY KEY(post_id, meta_key, meta_id),  -- to allow dup meta_key for a post
    ADD INDEX(meta_id);    -- to keep AUTO_INCREMENT happy

Plugin WP Index Improvements -- to do that alter, plus others. (As mentioned in the Comments, below.)

27
  • 1
    I've implemented this using your ALTER query on my testing environment and it improved the wp_postmeta performance drastically. This question (and solution) seems to have very little attention in my opinion. Why is that? Are there any drawbacks? I'm kind of worried to alter that table on a production environment with 2M+ postmeta rows. By the way: Everywhere I hear how the wp_postmeta table is badly optimized. How come it STILL is in 2020? Dec 10, 2020 at 12:48
  • 2
    @RickJames and I have a new WordPress plugin, GPL and free, to handle this re-keying of postmeta and similar tables. wordpress.org/plugins/index-wp-mysql-for-speed
    – O. Jones
    Jun 14, 2021 at 16:20
  • 1
    Nothing in WordPress core exploits FULLTEXT indexes. They require MATCH directives in queries to be useful. It's possible you have a plugin that uses MATCH. If you have an InnoDB Barracuda-based DBMS (recent versions of MySQL and MariaDB) our experience shows you're better off with a different clustered index than (meta_id). Please read this. plumislandmedia.net/index-wp-mysql-for-speed/tables_and_keys/…
    – O. Jones
    Sep 22 at 16:45
  • 1
    It's a rare ops person at a WordPress-intensive hosting company that knows much about this issue. (Prove me wrong, hosting company DBAs! Join the core performance team!) The indexes in your comment are definitely suboptimal on modern MariaDB / MySql: the 191 hack is not needed.
    – O. Jones
    Sep 24 at 23:49
  • 1
    The (191) is unnecessary in 8.0 and hurts performance. Point that out to the JCloud IT staff.
    – Rick James
    Sep 26 at 15:31

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.