slow query – Search for Media Gallery

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;

I can tell the SQL query executes for 644073 posts, and this is a biggie.
It explains half of your problem. Maybe in WordPress 4.7 added a search based on meta_value, and probably you have many meta values in your structure. It would be interesting to compare this vs. the query in WordPress 4.6.


What you may do is to modify the ajax_query_attachments_args hook to make your query run faster.

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 );

I feel this is a separate question, if you have any problems, just let me know.