database 如何显示 Wordpress 进行的所有数据库查询?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2473079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to display all database queries made by Wordpress?
提问by mattz
Using a method similar to the one described here, I can see the total number of queries being made in Wordpress when I load a page.
使用与此处描述的方法类似的方法,我可以查看加载页面时在 Wordpress 中进行的查询总数。
Now I'd like to display all database queries that are being made when a page loads. This would allow me to see who my biggest resource hogs are, without having to go through the process of elimination of all my plugins and theme scripts.
现在我想显示页面加载时进行的所有数据库查询。这将使我能够看到谁是我最大的资源猪,而不必经历消除所有插件和主题脚本的过程。
What would be the best way to display all database queries made by Wordpress?
显示 Wordpress 进行的所有数据库查询的最佳方式是什么?
回答by Richard M
If you add define('SAVEQUERIES', true)
to your configuration file, you can then list all the queries made for the current page by adding the following to your theme.
如果添加define('SAVEQUERIES', true)
到配置文件中,则可以通过将以下内容添加到主题中来列出对当前页面所做的所有查询。
if (current_user_can('administrator')){
global $wpdb;
echo "<pre>";
print_r($wpdb->queries);
echo "</pre>";
}
See the documentation for more details: http://codex.wordpress.org/Editing_wp-config.php#Save_queries_for_analysis
有关更多详细信息,请参阅文档:http: //codex.wordpress.org/Editing_wp-config.php#Save_queries_for_analysis
回答by justjoe
or you can hook into posts_request. You can put the coe inside functions.php such as
或者你可以挂钩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;
}
in your theme footer, you can use print_r like
在您的主题页脚中,您可以使用 print_r 之类的
print_r($GLOBALS['debugku']);
回答by Lucas Bustamante
Use Query Monitor.
使用查询监视器。
It's a free and open-source plugin where you can filter your queries in various contexts, such as:
这是一个免费的开源插件,您可以在其中过滤各种上下文中的查询,例如:
- Which plugin called
- Queries that took the most time
- Duplicated queries
- You can filter by Select / Update / Insert / Delete
- 哪个插件调用
- 花费最多时间的查询
- 重复查询
- 您可以按选择/更新/插入/删除进行过滤
Among other things...
除其他事项外...
回答by Alejo JM
I like to add at the bottom of the page, queries/elapsed time, here the code:
我喜欢在页面底部添加查询/已用时间,代码如下:
/**
* show all sql at footer if it defined in wp-config:
* 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);