我该如何优化这个wordpress查询?

时间:2011-01-02 23:30:29

标签: mysql wordpress query-optimization

我正试图找出下面查询的最佳索引组合。现在它使用临时;使用filesort并杀死我的vps。查询需要6-10秒。

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_taxonomy ON (wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id)
  INNER JOIN wp_terms ON (wp_term_taxonomy.term_id = wp_terms.term_id) 
WHERE
  1=1
  AND wp_term_taxonomy.taxonomy = 'post_tag' 
  AND wp_terms.slug IN ('pie') 
  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, 20

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

这与性能有何比较?

SELECT
  p.ID
FROM 
  wp_posts AS p
WHERE
  1=1
  AND p.post_type = 'post'
  AND p.post_status = 'publish'
  AND EXISTS (
    SELECT 1 
      FROM wp_term_relationships AS r 
           INNER JOIN wp_term_taxonomy AS t ON 
             r.term_taxonomy_id = t.term_taxonomy_id AND t.taxonomy = 'post_tag'
           INNER JOIN wp_terms AS m ON 
             m.term_id = t.term_id AND m.slug IN ('pie') 
     WHERE r.object_id = wp_posts.ID 
  )
ORDER BY 
  p.post_date DESC 
LIMIT 0, 20

索引应位于wp_terms.slugwp_term_taxonomy.taxonomy上的所有主键和外键上,以及wp_posts.post_status, post_type, post_date上的复合键。