Title: Optimize Query
Last modified: November 29, 2023

---

# Optimize Query

 *  Resolved [rahulattrey](https://wordpress.org/support/users/rahulattrey/)
 * (@rahulattrey)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/optimize-query/)
 *     ```wp-block-code
       Hi Team,
   
       We couldn't determine the source of the query below. Could you provide suggestions on how we can identify and optimize the mentioned query?
   
       Query#1: Time: 2023-11-28T18:27:14.108304+05:30 User@Host: itgd_VS_write[itgd_VS_write] @ ip-10-6-100-149.ap-south-1.compute.internal [10.6.100.149] Id: 4548821 Query_time: 1.070314 Lock_time: 0.000168 Rows_sent: 50 Rows_examined: 514055
   
       SET timestamp=1701176233;
       SELECT SQL_CALC_FOUND_ROWS wp_2_posts.ID
       FROM wp_2_posts
       WHERE 1=1 AND (
       (
       wp_2_posts.ID NOT IN (
       SELECT object_id
       FROM wp_2_term_relationships
       WHERE term_taxonomy_id IN (16,3,7,5)
       )
       )
       ) AND (wp_2_posts.post_mime_type = 'image/webp' OR wp_2_posts.post_mime_type = 'image/png' OR wp_2_posts.post_mime_type = 'image/jpeg' OR wp_2_posts.post_mime_type = 'image/gif' OR wp_2_posts.post_mime_type = 'video/mp4' OR wp_2_posts.post_mime_type = 'video/webm') AND wp_2_posts.post_type = 'attachment' AND ((wp_2_posts.post_status = 'inherit'))
       GROUP BY wp_2_posts.ID
       ORDER BY wp_2_posts.post_date DESC
       LIMIT 0, 50;
   
       Query #2: Time: 2023-11-28T18:27:15.289480+05:30 User@Host: itgd_VS_write[itgd_VS_write] @ ip-10-6-100-149.ap-south-1.compute.internal [10.6.100.149] Id: 4548821 Query_time: 1.050797 Lock_time: 0.000264 Rows_sent: 10 Rows_examined: 514055
   
       SET timestamp=1701176234;
       SELECT SQL_CALC_FOUND_ROWS wp_2_posts.ID
       FROM wp_2_posts
       WHERE 1=1 AND (
       (
       wp_2_posts.ID NOT IN (
       SELECT object_id
       FROM wp_2_term_relationships
       WHERE term_taxonomy_id IN (16,3,7,5)
       )
       )
       ) AND (wp_2_posts.post_mime_type = 'image/webp' OR wp_2_posts.post_mime_type = 'image/png' OR wp_2_posts.post_mime_type = 'image/jpeg' OR wp_2_posts.post_mime_type = 'image/gif' OR wp_2_posts.post_mime_type = 'video/mp4' OR wp_2_posts.post_mime_type = 'video/webm') AND wp_2_posts.post_type = 'attachment' AND ((wp_2_posts.post_status = 'inherit'))
       GROUP BY wp_2_posts.ID
       ORDER BY wp_2_posts.post_date DESC
       LIMIT 0, 10;
   
       Query#3: Time: 2023-11-28T18:30:05.543965+05:30 User@Host: itgd_VS_write[itgd_VS_write] @ ip-10-6-101-212.ap-south-1.compute.internal [10.6.101.212] Id: 4553148 Query_time: 1.188332 Lock_time: 0.000156 Rows_sent: 25 Rows_examined: 514055
   
       SET timestamp=1701176404;
       SELECT SQL_CALC_FOUND_ROWS wp_2_posts.ID
       FROM wp_2_posts
       WHERE 1=1 AND (
       (
       wp_2_posts.ID NOT IN (
       SELECT object_id
       FROM wp_2_term_relationships
       WHERE term_taxonomy_id IN (16,3,7,5)
       )
       )
       ) AND (wp_2_posts.post_mime_type = 'image/webp' OR wp_2_posts.post_mime_type = 'image/png' OR wp_2_posts.post_mime_type = 'image/jpeg' OR wp_2_posts.post_mime_type = 'image/gif' OR wp_2_posts.post_mime_type = 'video/mp4' OR wp_2_posts.post_mime_type = 'video/webm') AND wp_2_posts.post_type = 'attachment' AND ((wp_2_posts.post_status = 'inherit'))
       GROUP BY wp_2_posts.ID
       ORDER BY wp_2_posts.post_date DESC
       LIMIT 0, 25;
       ```
   
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Foptimize-query%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  Plugin Author [Pascal Birchler](https://wordpress.org/support/users/swissspidy/)
 * (@swissspidy)
 * [2 years, 6 months ago](https://wordpress.org/support/topic/optimize-query/#post-17238734)
 * This looks like the default query initiated by `WP_REST_Attachments_Controller`
   to return media elements for showing in the story editor’s media library. Query
   time doesn’t seem particularly slow. With a large posts table and many media 
   items, such queries are definitely possible.
 * To improve query times I’d suggest updating to the latest WordPress version and
   considering using an external object cache (like Redis or Memcached) so that 
   found post objects can be cached by WordPress.
 * If you have further, general questions about WP_Query optimizations, I’d recommend
   asking in the general support forums.

Viewing 1 replies (of 1 total)

The topic ‘Optimize Query’ is closed to new replies.

 * ![](https://ps.w.org/web-stories/assets/icon.svg?rev=2386543)
 * [Web Stories](https://wordpress.org/plugins/web-stories/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/web-stories/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/web-stories/)
 * [Active Topics](https://wordpress.org/support/plugin/web-stories/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/web-stories/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/web-stories/reviews/)

## Tags

 * [dashboard](https://wordpress.org/support/topic-tag/dashboard/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * 1 reply
 * 2 participants
 * Last reply from: [Pascal Birchler](https://wordpress.org/support/users/swissspidy/)
 * Last activity: [2 years, 6 months ago](https://wordpress.org/support/topic/optimize-query/#post-17238734)
 * Status: resolved