改进或优化非常慢的查询

时间:2014-10-29 作者:Biranit Goren

我有一个相当大的数据库-在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集群,所以服务器资源不是问题所在。

1 个回复
最合适的回答,由SO网友:Fiaz Husyn 整理而成

将“article\\u datetime”理解为一个数值,我建议不要使用“orderby”=>“meta\\u value”\'orderby\' => \'meta_value_num\'

\'orderby\'=>\'meta\\u value\',\'meta\\u key\'=>\'article\\u datetime\',\'order\'=>\'DESC\'

,而是在php中对结果进行排序。

结束

相关推荐

Child Theme Performance

我在这里和其他地方读过很多关于创建子主题的讨论,这是基于其他现有主题创建主题的推荐方法。我想知道与创建非儿童主题相比,这种方法可能存在的性能劣势。似乎父子主题方法必须处理加载比单个主题多得多的文件,除非我在法典解释中遗漏了什么。如果是这样的话,它必须降低加载速度,可能会显著降低加载速度。如果有人能为我澄清这一点,我将不胜感激。