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