1

I want to know why my site's search results are slow. I've used the excellent Debug Bar plugin, and found out that each search result causes 5 DB queries, of which 3 are SELECT meta_key (see below for complete query list).

Looking in the search loop code (below), I see it calls get_post_meta twice, and if I remove those then two of the SELECT meta_key queries per post disappear, and performance improve accordingly.

From Otto's excellent answer, I learned that post meta's are supposed to be cached, unless update_post_meta_cache=false, which I didn't see anywhere.

  • Why is get_post_meta doing queries?
  • How can I find out?

Site with Debug Bar installed: http://test.tipulnagish.co.il/?s= (real site)
- WordPress 4.3.1, PHP 5.4.43, MySQL 5.5.42, PremiumPress Directory Theme


Queries for each search results:

SELECT * FROM wp_users WHERE ID = '42'
require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/BT/search.php'), the_post, WP_Query->the_post, WP_Query->setup_postdata, get_userdata, get_user_by, WP_User::get_data_by #134 (0.5ms)
SELECT user_id, meta_key, meta_value FROM wp_usermeta WHERE user_id IN (42) ORDER BY umeta_id ASC
require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/BT/search.php'), the_post, WP_Query->the_post, WP_Query->setup_postdata, get_userdata, get_user_by, WP_User->init, WP_User->for_blog, WP_User->_init_caps, get_user_meta, get_metadata, update_meta_cache #135 (0.7ms)
SELECT meta_key FROM wp_postmeta WHERE meta_key !='_wp_page_template' AND meta_key !='_edit_last' AND meta_key !='_edit_lock' AND meta_key !='_encloseme' AND meta_key !='_pingme' GROUP BY meta_key
require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/BT/search.php'), white_label_themes->ITEM_CONTENT, white_label_themes->CUSTOMFIELDLIST #136 (35.9ms)
SELECT meta_key FROM wp_postmeta WHERE meta_key !='_wp_page_template' AND meta_key !='_edit_last' AND meta_key !='_edit_lock' AND meta_key !='_encloseme' AND meta_key !='_pingme' GROUP BY meta_key
require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/BT/search.php'), white_label_themes->ITEM_CONTENT, white_label_themes->CUSTOMFIELDLIST #137 (36.0ms)
SELECT meta_key FROM wp_postmeta WHERE meta_key !='_wp_page_template' AND meta_key !='_edit_last' AND meta_key !='_edit_lock' AND meta_key !='_encloseme' AND meta_key !='_pingme' GROUP BY meta_key
require('wp-blog-header.php'), require_once('wp-includes/template-loader.php'), include('/themes/BT/search.php'), get_template_part, locate_template, load_template, require('/themes/BT/content-listing.php'), white_label_themes->ITEM_CONTENT, white_label_themes->CUSTOMFIELDLIST

The results loop code in the theme's search.php is:

<?php hook_items_before(); ?>
<?php wp_reset_query(); ?>
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
    <?php if($canShowExtras):
        $maplong = get_post_meta($post->ID,'map-log',true);
        if($maplong !=""):
            if(!isset($map_coords)){ $map_coords = ""; }
            $map_coords .= "[".get_post_meta($post->ID,'map-lat',true).", ".$maplong. /*lots of formatting here...*/;
        endif;
    endif; ?>
    <?php get_template_part( 'content', hook_content_templatename($post->post_type) ); ?> 
    <?php wp_reset_postdata(); ?>
<?php endwhile; ?>
1
  • Does this happen with a bundled theme. Do you have any cache plugins installed that might be deleting these caches. The db is only queried when a specific value is not in cache. This definitely something in your theme or in plugin that is causing this to happen Sep 28, 2015 at 7:36

1 Answer 1

0

Eventually, I realized that the output lines after the queries are stack traces! So I looked in the theme's code, and indeed inside the white_label_themes->CUSTOMFIELDLIST function is a $wpdb->get_results(...) call to the queries. So it wasn't get_post_meta() after all - that part appears to work fine. I'll see what I can do regarding those queries - looks like I can simply remove them...

I still wonder about the 2 other queries from wp_users and wp_usermeta - this has nothing to do with get_post_meta(), so I started a separate question.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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