WordPress displays database query times and query time

 Watson Blog October 1, 2017 12:39:05 WordPress comment one hundred and ninety-two Reading mode

WordPress provides some function functions to easily display the statistical information of database queries, which can be publicly displayed in the web page, hidden in the source code, or only visible to you. The effect is as follows:

 WordPress displays database query times and query time

WordPress mainly provides two statistical functions

1. Number of times to query the database when the webpage is loaded:?<? php echo get_num_queries(); ?>

2. Time spent by the server to complete these queries:?<? php timer_stop(1); ?>

You can use 3 ways to display in the web page:

1. Publicly display the statistical information of the query

If you think your server or space has good processing speed, or want your visitors to see these query statistics, you can publicly display these statistics on the page:( This blog is to add the following code to footer.php. The effect is shown in the figure above

  1. <p? style= "text-align:center;color:#3A90F2" >Number of queries<? php? echo ? get_num_queries();?>? Times, the total time consumption is?<? php?timer_stop(1);??>? Seconds</ p>

2. Display the statistics in the source code instead of the content of the page

If you don't want to display statistics on the page for visitors to view, but want to know these statistics, you can only display the results in the source code through html comments (PS: you can also view them from the source code!):

  1. <!--? Number of queries<? php? echo ? get_num_queries();??> Times,? Time consumption?<?? php?timer_stop(1);??>? Seconds? -->

3. Statistical information can only be seen after you log in

This method is the second display optimization method. Only the blog administrator can view the query statistics after logging in. Visitors cannot view it:

  1. <? php
  2. if ? (current_user_can('level_10'))
  3. {
  4. ???? echo ?' Number of queries<! --? '? get_num_queries()?.?'? Time, time consumption:? timer_stop(1)?.?'? Seconds? -->';
  5. }
  6. ?>

Extension

If you want to View the specific contents of the database queried , the following solutions can be used:

1. First in wp-config.php Add the following code

  1. define('SAVEQUERIES',? true);

2. Add the following code in footer.php:

  1. <? php
  2. if ? (is_user_logged_in())
  3. {
  4. ???? global ? $wpdb ;
  5. ???? echo ? "<pre>" ;
  6. ???? print_r( $wpdb ->queries);
  7. ???? echo ? "</pre>" ;
  8. }
  9. ?>

 

Code interpretation:

  1. ? if (is_user_logged_in()) It is used to determine whether the current visitor has logged in or not if (current_user_can('level_10')) To determine whether it is an administrator login, in order to prevent tourists from viewing these data, this code can be saved;
  2. global $wpdb; Defining Global Variables $wpdb , which is the default database class of Wordpress;
  3. <pre></pre> ? Nest results in HTML tags? <pre> ? Internal;
  4. print_r($wpdb->queries); Output the information of each database query.

Refresh the homepage or log page, and you can see output results similar to the following:

  1. Array
  2. ??? (
  3. ??????? [0]?=>? Array
  4. ??????????? (
  5. ??????????????? [0]?=>?? SELECT?SQL_CALC_FOUND_ROWS??wp_posts.*?FROM?wp_posts??WHERE?1=1??AND?wp_posts.post_type?=?'post'?AND? (wp_posts.post_status?=?'publish'? OR?wp_posts.post_status?=?' private ')?? ORDER?BY?wp_posts.post_date?DESC?LIMIT?0,?10
  6. ??????????????? [1]?=>? zero point zero zero zero three nine six zero one three two five nine eight eight seven seven
  7. ??????????????? [2]?=>? require ,? wp,?WP->main,?WP->query_posts,?WP_Query->query,?WP_Query->get_posts
  8. ???????????)
  9. ??????? [1]?=>? Array
  10. ??????????? (
  11. ??????????????? [0]?=>? SELECT?option_value?FROM?wp_options?WHERE?option_name?=?'nuodou_header_code'?LIMIT?1
  12. ??????????????? [1]?=>? zero point zero zero one three five eight nine eight five nine zero zero eight seven eight nine
  13. ??????????????? [2]?=>? require ,? require_once ,? include ,? get_header,?locate_template,?load_template,? require_once ,? get_option
  14. ???????????)

The final work has to be analyzed by yourself to see what can be deleted and what can be improved to reduce the number of database queries.

 Watson Blog
  • This article is written by Published on October 1, 2017 12:39:05
  • This article is collected and sorted by the website of Mutual Benefit, and the email address for problem feedback is: wosnnet@foxmail.com , please keep the link of this article for reprinting: https://wosn.net/642.html

Comment