首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何显示Wordpress进行的所有数据库查询?

如何显示Wordpress进行的所有数据库查询?
EN

Stack Overflow用户
提问于 2010-03-19 04:17:02
回答 3查看 27.8K关注 0票数 17

使用一个类似于described here的方法,当我加载一个页面时,我可以看到在Wordpress中进行的查询总数。

现在,我想要显示页面加载时正在进行的所有数据库查询。这将允许我看到谁是我最大的资源占有者,而不必经历消除我所有的插件和主题脚本的过程。

显示Wordpress所做的所有数据库查询的最佳方式是什么?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2010-03-19 08:39:10

如果将define('SAVEQUERIES', true)添加到配置文件中,则可以通过在主题中添加以下内容来列出对当前页面所做的所有查询。

代码语言:javascript
复制
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

票数 50
EN

Stack Overflow用户

发布于 2010-03-19 08:59:56

或者,您也可以连接到posts_request。可以将coe放在functions.php中,如下所示

代码语言:javascript
复制
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

代码语言:javascript
复制
print_r($GLOBALS['debugku']);
票数 6
EN

Stack Overflow用户

发布于 2020-05-21 05:25:04

我喜欢在页面的底部添加查询/运行时间,这里是代码:

代码语言:javascript
复制
/**
 * 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>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2473079

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档