GET_POST_META导致数据库查询

时间:2015-09-27 作者:Jonathan

我想知道为什么我的网站的搜索结果很慢。我用过最好的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; ?>

1 个回复
最合适的回答,由SO网友:Jonathan 整理而成

最终,我意识到查询后的输出行是堆栈跟踪!所以我查看了主题的代码,实际上在white_label_themes->CUSTOMFIELDLIST 函数是$wpdb->get_results(...) 调用查询。所以它不是get_post_meta() 毕竟,这部分似乎工作得很好。我会看看我能对这些查询做些什么-看起来我可以简单地删除它们。。。

我仍然想知道wp_userswp_usermeta - 这与get_post_meta(), 所以我开始separate question.

相关推荐

Nginx+WordPress+HHVM:为什么Batcache不能工作?瓦尼什会提供更多帮助吗?

我听说了很多关于HHVM, 所以我用Nginx(带Pagespeed模块)和HHVM设置了WordPress博客的副本(在另一个域上)。现在,好处是显而易见的:在相同的配置上,加载时间要快两到三倍。我正在努力加快速度,我还安装了Memcached and Batcache. 我已经安装了memcached 包,已复制对象缓存。php(Pastebin) 在WordPress博客的根文件夹上,然后我安装了Batcache插件并复制了高级缓存。php(Pastebin) 将文件保存到wp content文件夹