【问题标题】:How to display all database queries made by Wordpress?如何显示由 Wordpress 进行的所有数据库查询?
【发布时间】:2011-01-29 04:58:31
【问题描述】:

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

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

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

【问题讨论】:

    标签: database wordpress


    【解决方案1】:

    如果将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

    【讨论】:

    • 谢谢,这成功了。现在我只需要理解这些查询。
    • 请注意,如果使用 Hyperdb,则需要在 db-config.php 中执行此操作: $wpdb->save_queries = defined('SAVEQUERIES') && SAVEQUERIES;
    • 你能告诉我每个查询的 [1] 值是多少吗? [1] => 0.0004429817199707
    【解决方案2】:

    或者你可以连接到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']);
    

    【讨论】:

    • 感谢您的建议。我喜欢这不需要编辑核心文件,我认为它有效,但我不是 100% 确定。解读它输出的内容有点困难。数据似乎很稀少,但我认为它可能都在一条线上,我只是看不到全部。我接受了上面 Richard M 的建议,并将其全部输出到一个格式良好的列表中。
    • 嘿,就像古老的智慧谚语:通往罗马的道路很多;D
    【解决方案3】:

    使用Query Monitor

    这是一个免费的开源插件,您可以在其中过滤各种上下文中的查询,例如:

    • 调用了哪个插件
    • 耗时最长的查询
    • 重复查询
    • 您可以按选择/更新/插入/删除进行过滤

    除此之外……

    【讨论】:

    • 这个插件的信息量惊人!我在查询下找到了我正在寻找的 SQL,调用者 = 主查询。
    【解决方案4】:

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

    /**
     * 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);
    

    【讨论】:

      猜你喜欢
      • 2018-01-09
      • 2012-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-25
      • 1970-01-01
      相关资源
      最近更新 更多