与正在执行以下查询的wordpress网站合作,但我看到此查询正在执行许多内部联接,并且该网站需要很长时间才能加载并且失败很多,并且我一直在尝试创建一个产生相同结果的查询但是还没有成功
我想知道有什么更好的方法可以做到这一点
SELECT * FROM wp_posts INNER JOIN wp_postmeta color ON wp_posts.ID = color.post_id INNER JOIN wp_postmeta transmission ON wp_posts.ID = transmission.post_id INNER JOIN wp_postmeta model ON wp_posts.ID = model.post_id INNER JOIN wp_postmeta brand ON wp_posts.ID = brand.post_id AND color.meta_key = 'color' AND color.meta_value = 'red' AND transmission.meta_key = 'transmission' AND transmission.meta_value = 'auto' AND model.meta_key = 'model' AND model.meta_value = 'model' AND brand.meta_key = 'brand' AND brand.meta_value = 'brand' AND wp_posts.post_status = 'publish' AND wp_posts.post_type = 'car' ORDER BY wp_posts.post_title
这是说明输出。
+----+-------------+-----------+--------+-----------------------------+----------+---------+------------------------+------+----------------------------------------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+--------+-----------------------------+----------+---------+------------------------+------+----------------------------------------------+ | 1 | SIMPLE | color | ref | post_id,meta_key | meta_key | 768 | const | 629 | Using where; Using temporary; Using filesort | | 1 | SIMPLE | wp_posts | eq_ref | PRIMARY,type_status_date,ID | PRIMARY | 8 | tmcdb.color.post_id | 1 | Using where | | 1 | SIMPLE | brand | ref | post_id,meta_key | post_id | 8 | tmcdb.wp_posts.ID | 4 | Using where | | 1 | SIMPLE | transmission | ref | post_id,meta_key | post_id | 8 | tmcdb.color.post_id | 4 | Using where | | 1 | SIMPLE | model | ref | post_id,meta_key | post_id | 8 | tmcdb.transmission.post_id | 4 | Using where | +----+-------------+-----------+--------+-----------------------------+----------+---------+------------------------+------+----------------------------------------------+
WordPress的架构在这里。
看来您正在尝试获取类型为的每个帖子包含一行的结果集car。似乎您想在帖子中显示每辆汽车的各种属性,并且隐藏在中postmeta。
car
postmeta
专家提示: 除非您完全知道自己要这么做的原因,否则请 不要SELECT *在软件中使用。尤其是对于包含大量JOIN操作的查询,SELECT *将返回许多无意义和冗余的列。
SELECT *
JOIN
WordPress postmeta表有一个查询设计技巧。如果要获取特定属性,请执行以下操作:
SELECT p.ID, p.post_title, color.meta_value AS color FROM wp_posts AS p LEFT JOIN wp_postmeta AS color ON p.ID = color.post_id AND 'color' = color.meta_key WHERE p.post_status = 'publish' AND /* etc etc */
在执行您要执行的操作时,了解此模式非常重要。此模式是必需的,因为它postmeta是表的一种特殊类型,称为键值或实体属性值存储。这里发生了什么?一些东西:
posts
LEFT JOIN
postmeta AS color
meta_key
'color' = color.meta_key
ON
SELECT
postmeta.meta_value
color.meta_value AS color
一旦习惯了采用这种模式,就可以通过一系列LEFT JOIN操作将其堆叠起来,以获得许多不同的属性,就像这样。
SELECT wp_posts.ID, wp_posts.post_title, wp_posts.whatever, color.meta_value AS color, transmission.meta_value AS transmission, model.meta_value AS model, brand.meta_value AS brand FROM wp_posts LEFT JOIN wp_postmeta AS color ON wp_posts.ID = color.post_id AND color.meta_key='color' LEFT JOIN wp_postmeta AS transmission ON wp_posts.ID = transmission.post_id AND transmission.meta_key='transmission' LEFT JOIN wp_postmeta AS model ON wp_posts.ID = model.post_id AND model.meta_key='model' LEFT JOIN wp_postmeta AS brand ON wp_posts.ID = brand.post_id AND brand.meta_key='brand' WHERE wp_posts.post_status = 'publish' AND wp_posts.post_type = 'car' ORDER BY wp_posts.post_title
我已经对该查询进行了一系列缩进,以使其更易于查看模式。您可能希望使用其他缩进样式。
很难知道为什么您的查询中存在性能问题。可能是因为您收到了包含所有INNER JOIN随后被过滤的所有操作的组合爆炸。但是无论如何,您显示的查询可能不返回任何行。
INNER JOIN
如果仍然遇到性能问题,请尝试postmeta在(post_id, meta_key, meta_value)列上创建复合索引。如果要创建WordPress插件,则可能是在插件安装时要做的工作。
(post_id, meta_key, meta_value)