我有一个相当大的数据库-在wp\\U Post中有113299行,在wp\\U Posteta中有2160649行。
有一个自定义查询是在添加或编辑帖子后运行的,每次运行时它都会出现在MySQL的慢速日志文件中,而且似乎耗时太多(实际上是17到78秒)。
这就是它在query_posts
:
$args = array(
\'meta_query\' => array(
array(
\'key\' => \'article_template\',
\'value\' => \'news\',
),
),
\'posts_per_page\' => \'30\',
\'category__in\' => array( 3, 4, 5 ),
\'post_status\' => \'publish\',
\'no_found_rows\' => true,
\'orderby\' => \'meta_value\',
\'meta_key\' => \'article_datetime\',
\'order\' => \'DESC\'
);
query_posts( $args );
这是MySQL慢速日志文件中的内容:
SELECT wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (3,4,5) ) AND wp_posts.post_type = \'post\' AND (wp_posts.post_status = \'publish\') AND (wp_postmeta.meta_key = \'article_datetime\'
AND (mt1.meta_key = \'article_template\' AND CAST(mt1.meta_value AS CHAR) = \'news\') ) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value DESC LIMIT 0, 30;
这是解释对此查询显示的内容:
mysql> explain SELECT wp_posts.ID FROM wp_posts INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) INNER JOIN wp_postmeta ON (wp_posts.ID = wp_postmeta.post_id)
-> INNER JOIN wp_postmeta AS mt1 ON (wp_posts.ID = mt1.post_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (3,4,5) ) AND wp_posts.post_type = \'post\' AND (wp_posts.post_status = \'publish\') AND (wp_postmeta.meta_key = \'article_datetime\' AND (mt1.meta_key = \'article_template\' AND CAST(mt1.meta_value AS CHAR) = \'news\') ) GROUP BY wp_posts.ID ORDER BY wp_postmeta.meta_value DESC LIMIT 0, 30;
+----+-------------+-----------------------+--------+--------------------------+----------+---------+-----------------------------+-------+----------------------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-----------------------+--------+--------------------------+----------+---------+-----------------------------+-------+----------------------------------------------+
| 1 | SIMPLE | wp_postmeta | ref | post_id,meta_key | meta_key | 768 | const | 98576 | Using where; Using temporary; Using filesort |
| 1 | SIMPLE | wp_posts | eq_ref | PRIMARY,type_status_date | PRIMARY | 8 | toi_web.wp_postmeta.post_id | 1 | Using where |
| 1 | SIMPLE | mt1 | ref | post_id,meta_key | post_id | 8 | toi_web.wp_postmeta.post_id | 11 | Using where |
| 1 | SIMPLE | wp_term_relationships | ref | PRIMARY,term_taxonomy_id | PRIMARY | 8 | toi_web.mt1.post_id | 2 | Using where; Using index |
+----+-------------+-----------------------+--------+--------------------------+----------+---------+-----------------------------+-------+----------------------------------------------+
因此,我想知道是否有人有一个好的提示或建议,如何优化这一点,避免缓慢的查询?也许可以把它分成不同的
query_posts
环或者先用一个简单的
wpdb->get_results
查询
我愿意接受任何建议:)
顺便说一下,我们有一个非常健壮的MySQL集群,所以服务器资源不是问题所在。