我想知道为什么我的网站的搜索结果很慢。我用过最好的Debug Bar 插件,并发现每个搜索结果都会导致5个DB查询,其中3个是SELECT meta_key
(完整的查询列表见下文)。
在下面的搜索循环代码中,我看到它调用get_post_meta
两次,如果我把它们去掉SELECT meta_key
每个帖子的查询消失,性能也相应提高。
从…起Otto\'s excellent answer, 我知道post meta应该被缓存,除非update_post_meta_cache=false
, 我什么地方都没看到。
为什么是get_post_meta
执行查询我怎么才能知道呢安装了调试栏的站点:http://test.tipulnagish.co.il/?s= (real site)
-WordPress 4.3.1、PHP 5.4.43、MySQL 5.5.42、,PremiumPress Directory Theme
对每个搜索结果的查询:
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
主题中的结果循环代码
search.php
是:
<?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; ?>