postmeta的引用如此之慢

postmeta的引用如此之慢

本文介绍了为什么对wp_postmeta的引用如此之慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在WordPress中获取属性(使用MySQL)似乎比必要的慢。

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

(这是一个自我回答的问题,所以继续我的回答。)

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

推荐答案

wp_postmeta 的标准架构提供了糟糕的索引。这会导致性能问题。

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;

注意:


  • 当前 AUTO_INCREMENT 列是浪费空间,并且因为它是 PRIMARY KEY 而减慢了查询速度,从而避免了的自然复合PK(post_id,meta_key)

  • InnoDB进一步提升了该PK的性能聚集。 (我希望您还没有使用MyISAM!)

  • 如果您使用的是MySQL 5.6(或MariaDB 10.0或10.1),请更改 meta_key 来自 VARCHAR(255),而非 VARCHAR(191)。 (如果191还不够,我们可以在单独的问题中讨论原因和解决方法。)

  • INDEX(meta_key)是可选的,但如果你想查找具有特定密钥的帖子,则需要这样做。

  • 警告:这些更改将加快许多对postmeta的使用,但是不是全部。我不认为它会减慢任何用例。 (如果遇到这些问题,请提供此类查询。这可能是一个缓存问题,而不是真正的退化。)

  • 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.)

如果您愿意要提供你的 CREATE TABLE ,我可以提供 ALTER 将其转换为此。

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

这篇关于为什么对wp_postmeta的引用如此之慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 08:05