1

after installing wordpress 4.7 Search for Media Gallery became very slowly performed. here is a log falls into the slow-queries.log:

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

tell me how to fix it?

  • what exactly is "Search for Media Gallery"? Just checking if this is a plugin? – prosti Dec 30 '16 at 17:36
  • Provide details what plugins do you use? – prosti Dec 30 '16 at 17:36
  • here. it is not a plugin screenshot.net/rmdowhq – Marat Petrov Dec 31 '16 at 18:04
  • I see now this is Media Gallery in WordPress. @MaratPetrov, so what is the ~ number of posts you may have? – prosti Dec 31 '16 at 18:21
  • Do you have the query for WordPress 4.6? – prosti Dec 31 '16 at 18:31
0

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.

  • and what I need to fix it? in my file of the same code, as you pointed out – Marat Petrov Jan 1 '17 at 20:03
  • You need to find the way to alter the query. – prosti Jan 3 '17 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.