优化SQL查询|检索Wordpress类别下的所有帖子

时间:2018-04-11 16:36:40

标签: mysql sql

我希望有人可以告诉我优化此查询的最佳方法,以便更快更简单。

或者如果查询已经很好了。

我尝试做的是检索给定类别下的所有帖子,其中%VAR1%是类别变量。下面的两个查询确实有效,但它们是最好的吗?他们优化了吗?我希望的是,他们不会遍历每个帖子,然后根据类别进行过滤。也许这比较慢?

我很感激您的意见。

SELECT posts_post.post_date AS post_post_date,
       CONCAT('<a href="',posts_post.guid,'">',posts_post.post_title,'</a>') AS post_title_with_link_to_post
FROM wp_posts AS posts_post
  INNER JOIN (SELECT name, object_id as id FROM wp_terms AS post_taxonomy_category_tbl_terms INNER JOIN wp_term_taxonomy AS post_taxonomy_category_tbl_termtaxonomy ON post_taxonomy_category_tbl_termtaxonomy.term_id = post_taxonomy_category_tbl_terms.term_id  AND post_taxonomy_category_tbl_termtaxonomy.taxonomy = 'category' INNER JOIN wp_term_relationships AS rel_post_taxonomy_category_tbl  ON post_taxonomy_category_tbl_termtaxonomy.term_taxonomy_id = rel_post_taxonomy_category_tbl.term_taxonomy_id) AS post_taxonomy_category_tbl
     ON post_taxonomy_category_tbl.name = "%VAR1%"
     AND post_taxonomy_category_tbl.ID = posts_post.id
WHERE 1=1 
   AND posts_post.post_status = 'publish'
   AND posts_post.post_type = 'post'

另一个是:

SELECT GROUP_CONCAT(distinct post_taxonomy_category_tbl.name) AS post_taxonomy_category,
       CONCAT('<a href="',posts_post.guid,'">',posts_post.post_title,'</a>') AS post_title_with_link_to_post,
       posts_post.post_date AS post_post_date
FROM wp_posts AS posts_post
  INNER JOIN (SELECT name, object_id as id FROM wp_terms AS post_taxonomy_category_tbl_terms INNER JOIN wp_term_taxonomy AS post_taxonomy_category_tbl_termtaxonomy ON post_taxonomy_category_tbl_termtaxonomy.term_id = post_taxonomy_category_tbl_terms.term_id  AND post_taxonomy_category_tbl_termtaxonomy.taxonomy = 'category' INNER JOIN wp_term_relationships AS rel_post_taxonomy_category_tbl  ON post_taxonomy_category_tbl_termtaxonomy.term_taxonomy_id = rel_post_taxonomy_category_tbl.term_taxonomy_id) AS post_taxonomy_category_tbl
     ON post_taxonomy_category_tbl.ID = posts_post.id 
     AND post_taxonomy_category_tbl.name = "%VAR1%"
WHERE 1=1 
   AND posts_post.post_status = 'publish'
   AND posts_post.post_type = 'post'
GROUP BY post_taxonomy_category_tbl.id

0 个答案:

没有答案