计数和显示数据库查询


9

我正在寻找一种解决方案,如何计算和显示WordPress网站中的所有查询。有人知道,如果有一个好的插件?

否则,这将是在控制台上检查查询的解决方案,因为我在控制台上进行了大量工作。

Answers:


10

您可以将此代码块粘贴到当前活动的WordPress主题functions.php文件中:

function wpse_footer_db_queries(){
    echo '<!-- '.get_num_queries().' queries in '.timer_stop(0).' seconds. -->'.PHP_EOL;
}
add_action('wp_footer', 'wpse_footer_db_queries');

上面的代码块将在主题的页脚中(</body>和之前)呈现HTML注释</html>,其中包含数据库查询的数量以及如何检索日志。


那对我有帮助。是否还有一种解决方案来显示查询及其内容?像这样:SELECT * FROM wp_posts 谢谢
pkberlin

1
@derpiet See @toscho答案
Michael Ecklund 2012年

9

添加...

define( 'SAVEQUERIES', TRUE );

......你wp-config.php,和检查$wpdb->queriesshutdown。那是最新的钩子,也是唯一没有引发查询的钩子。另外,它也可以使用wp-admin/

示例代码作为插件:

<?php
/**
 * Plugin Name: T5 Inspect Queries
 * Description: Adds a list of all queries at the end of each file.
 *
 * Add the following to your wp-config.php:

define( 'WP_DEBUG',         TRUE );
define( 'SAVEQUERIES',      TRUE );

 */

add_action( 'shutdown', 't5_inspect_queries' );

/**
 * Print a list of all database queries.
 *
 * @wp-hook shutdown
 * @return  void
 */
function t5_inspect_queries()
{
    global $wpdb;

    $list = '';

    if ( ! empty( $wpdb->queries ) )
    {
        $queries = array ();
        foreach ( $wpdb->queries as $query )
        {
            $queries[] = sprintf(
                '<li><pre>%1$s</pre>Time: %2$s sec<pre>%3$s</pre></li>',
                nl2br( esc_html( $query[0] ) ),
                number_format( sprintf('%0.1f', $query[1] * 1000), 1, '.', ',' ),
                esc_html( implode( "\n", explode(', ', $query[2] ) ) )
            );
        }

        $list = '<ol>' . implode( '', $queries ) . '</ol>';
    }

    printf(
        '<style>pre{white-space:pre-wrap !important}</style>
        <div class="%1$s"><p><b>%2$s Queries</b></p>%3$s</div>',
        __FUNCTION__,
        $wpdb->num_queries,
        $list
    );
}

更新资料

在考虑了一段时间之后,我编写了另一个更适合我需要的插件-如果您更喜欢控制台,也可能是您的。

<?php
/**
 * Plugin Name: T5 Log Queries
 * Description: Writes all queries to '/query-log.sql'.
 * Plugin URI:  http://wordpress.stackexchange.com/a/70853/73
 * Version:     2012.11.04
 * Author:      Thomas Scholz
 * Author URI:  http://toscho.de
 * Licence:     MIT
 */

add_filter( 'query', 't5_log_queries' );

/**
 * Write the SQL to a file.
 *
 * @wp-hook query
 * @param   string $query
 * @return  string Unchanged query
 */
function t5_log_queries( $query )
{
    static $first = TRUE;
    // Change the path here.
    $log_path = apply_filters(
        't5_log_queries_path',
        ABSPATH . 'query-log.sql'
    );
    $header = '';

    if ( $first )
    {
        $time    = date( 'Y-m-d H:i:s' );
        $request = $_SERVER['REQUEST_URI'];
        $header  = "\n\n# -- Request URI: $request, Time: $time ------------\n";
        $first   = FALSE;
    }

    file_put_contents( $log_path, "$header\n$query", FILE_APPEND | LOCK_EX );

    return $query;
}

使用以下文件跟踪文件tail如果已安装Git,则在Windows上可用):

$ tail -f query-log.sql -n 50
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.