查询速度慢-搜索媒体集

时间:2016-12-30 作者:Marat Petrov

安装wordpress 4.7后,搜索媒体库的速度变得非常缓慢。下面是一个跌入慢速查询的日志。日志:

# Time: 2016-12-30T16:28:07.386551Z
# User@Host: info_234[info_234] @ localhost []  Id: 6390731
# Query_time: 4.187182  Lock_time: 1.953823 Rows_sent: 40  Rows_examined: 644073
SET timestamp=1483115287;
SELECT SQL_CALC_FOUND_ROWS  wp_posts.ID FROM wp_posts  LEFT JOIN wp_postmeta AS sq1 ON ( wp_posts.ID = sq1.post_id AND sq1.meta_key = \'_wp_attached_file\' ) WHERE 1=1  AND (((wp_posts.post_title LIKE \'%электр%\') OR (wp_posts.post_excerpt LIKE \'%электр%\') OR (wp_posts.post_content LIKE \'%электр%\') OR ( sq1.meta_value LIKE \'%электр%\' )))  AND (wp_posts.post_mime_type LIKE \'image/%\')  AND wp_posts.post_type = \'attachment\' AND ((wp_posts.post_status = \'inherit\' OR wp_posts.post_status = \'private\')) GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 40;
告诉我怎么修?

1 个回复
SO网友:prosti

时间:2016-12-30T16:28:07.386551ZUser@Host:info\\u 234[info\\u 234]@localhost[]Id:6390731查询时间:4.187182锁定时间:1.953823行发送:40行检查:644073

设置时间戳=1483115287;

选择SQL\\u CALC\\u FOUND\\u ROWS wp\\u posts。来自wp\\U posts的ID将wp\\U Posteta作为sq1加入(wp\\U posts.ID=sq1.post\\u ID和sq1.meta\\u key=“\\U wp\\u attached\\u file”),其中1=1和((wp\\U posts.post\\u title,如\'\'%1101;Дкррр%\')或(wp\\U posts.posts\\u摘录,如\'\'%1101;Дкккрррр%\')或(wp\\U posts.post,如\'\'%1.metaрррр%\')值)和(wp\\u posts.post\\u mime\\u类型,如“image/%”)和wp\\u posts。post\\U类型=“附件”和((wp\\u posts.post\\u status=“继承”或wp\\u posts.post\\u status=“私有”)分组。wp\\U发布的ID订单。post\\U date DESC LIMIT 0,40;

我可以看出SQL查询执行了644073篇文章,这是一个大问题。它解释了你一半的问题。可能在WordPress 4.7中添加了基于meta_value, 也许你的结构中有很多元价值观。将其与WordPress 4.6中的查询进行比较会很有趣。

您可以修改ajax_query_attachments_args 钩子使您的查询运行更快。

File: wp-admin/includes/ajax-actions.php
2409:   /**
2410:    * Filters the arguments passed to WP_Query during an Ajax
2411:    * call for querying attachments.
2412:    *
2413:    * @since 3.7.0
2414:    *
2415:    * @see WP_Query::parse_query()
2416:    *
2417:    * @param array $query An array of query variables.
2418:    */
2419:   $query = apply_filters( \'ajax_query_attachments_args\', $query );
2420:   $query = new WP_Query( $query );
我觉得这是一个单独的问题,如果你有任何问题,请告诉我。