【发布时间】:2011-01-29 04:58:31
【问题描述】:
使用类似于described here 的方法,我可以看到加载页面时在 Wordpress 中进行的查询总数。
现在我想显示页面加载时正在进行的所有数据库查询。这将使我能够看到我最大的资源消耗者是谁,而无需经历消除所有插件和主题脚本的过程。
显示由 Wordpress 进行的所有数据库查询的最佳方式是什么?
【问题讨论】:
使用类似于described here 的方法,我可以看到加载页面时在 Wordpress 中进行的查询总数。
现在我想显示页面加载时正在进行的所有数据库查询。这将使我能够看到我最大的资源消耗者是谁,而无需经历消除所有插件和主题脚本的过程。
显示由 Wordpress 进行的所有数据库查询的最佳方式是什么?
【问题讨论】:
如果将define('SAVEQUERIES', true) 添加到配置文件中,则可以通过将以下内容添加到主题来列出对当前页面进行的所有查询。
if (current_user_can('administrator')){
global $wpdb;
echo "<pre>";
print_r($wpdb->queries);
echo "</pre>";
}
查看文档了解更多详情:http://codex.wordpress.org/Editing_wp-config.php#Save_queries_for_analysis
【讨论】:
或者你可以连接到posts_request。你可以把coe放在functions.php里面比如
add_filter('posts_request','debug_post_request'); // debugging sql query of a post
function debug_post_request($sql_text) {
$GLOBALS['debugku'] = $sql_text; //intercept and store the sql<br/>
return $sql_text;
}
在您的主题页脚中,您可以使用 print_r 之类的
print_r($GLOBALS['debugku']);
【讨论】:
【讨论】:
我喜欢在页面底部添加查询/经过时间,这里是代码:
/**
* show all sql at footer if it defined in wp-config.php:
* define('SAVEQUERIES', true);
*/
function plg_name_show_debug_queries()
{
if (defined('SAVEQUERIES') && SAVEQUERIES) {
global $wpdb;
if (is_array($wpdb->queries)) foreach ($wpdb->queries as $key => $q) {
list($query, $elapsed, $debug) = $q;
$time = number_format(($elapsed * 1000), 3);
$count = $key + 1;
$total_time += $elapsed;
echo "
<div style=\"position: relative; z-index: 9999 ; background: black; color: white; padding:10px\">
$count - Query: $query <br> Time: $time ms
</div>";
}
echo "
<div style=\"position: relative; z-index: 9999 ; background: black; color: white; padding:10px\">
Total Queries: " . count($wpdb->queries) . "<br>Total Time: " . number_format(($total_time * 1000), 3) . " ms
</div>";
}
}
add_action('admin_footer', 'plg_name_show_debug_queries', PHP_INT_MAX);
add_action('wp_footer', 'plg_name_show_debug_queries', PHP_INT_MAX);
【讨论】: