How to display all database queries made by Wordpr

2019-01-21 20:47发布

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.

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?

3条回答
▲ chillily
2楼-- · 2019-01-21 21:22

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...

how to see what queries wordpress is running

查看更多
等我变得足够好
3楼-- · 2019-01-21 21:27

or you can hook into posts_request. You can put the coe inside functions.php such as

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($GLOBALS['debugku']);
查看更多
贼婆χ
4楼-- · 2019-01-21 21:28

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.

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

查看更多
登录 后发表回答