我有一个wordpress网站,我有一个wp_query
,其中包含以下参数:
array(
'post_status' => 'publish',
'post_type' => 'post',
'offset' => 0,
'posts_per_page' => 21,
'tax_query' =>
array(
0 => array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => 5
),
1 => array(
'taxonomy'] =>'category',
'field'] =>'id',
'terms'] =>17
),
2 => array(
'taxonomy'] =>'category'
'field'] =>'id'
'terms'] =>20
),
3 => array(
'taxonomy' => 'category'
'field' => 'id'
'terms' => 33
),
4 => array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => 55
),
5 => array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => 83
),
'relation' => 'OR'
)
)
这个问题是每次帖子发生变化时,这个查询都需要几分钟才能完成。以前有人有这个问题吗?这样就生成了这样的sql:
SELECT SQL_CALC_FOUND_ROWS wp_posts.ID
FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id)
INNER JOIN wp_term_relationships AS tt2 ON (wp_posts.ID = tt2.object_id)
INNER JOIN wp_term_relationships AS tt3 ON (wp_posts.ID = tt3.object_id)
INNER JOIN wp_term_relationships AS tt4 ON (wp_posts.ID = tt4.object_id)
INNER JOIN wp_term_relationships AS tt5 ON (wp_posts.ID = tt5.object_id)
WHERE 1=1
AND ( wp_term_relationships.term_taxonomy_id IN (5)
OR tt1.term_taxonomy_id IN (17)
OR tt2.term_taxonomy_id IN (20)
OR tt3.term_taxonomy_id IN (33)
OR tt4.term_taxonomy_id IN (55)
OR tt5.term_taxonomy_id IN (83) )
AND wp_posts.post_type = 'post'
AND ((wp_posts.post_status = 'publish'))
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 21
感谢您的帮助。
答案 0 :(得分:2)
你可以做的一件事是调整tax_query
参数;你加入了同一个表六次,每一个都有一个IN语句 - 正如你所注意到的那样,它产生了一些非常低效的SQL。
manual page for WP_Query有一些tax_query
示例,看起来您可以将所有这些不同的ID组合到一个实体中,这将大大降低SQL复杂性。
array(
'post_status' => 'publish',
'post_type' => 'post',
'offset' => 0,
'posts_per_page' => 21,
'tax_query' =>
array(
0 => array(
'taxonomy' => 'category',
'field' => 'id',
'terms' => array (5, 17, 20, 33, 55, 83),
) )
)
这应该使用任何这些ID获取术语,并且应该更有效地执行。