WordPress提供了一些功能函数可以轻松地显示数据库查询的统计信息,这些信息可以公开地显示在网页中,或者隐藏在源代码中,更或者只有你自己可以看到。
WordPress主要提供了两个统计函数:
1.网页加载时查询数据库的次数:<?php echo get_num_queries(); ?>
2.服务器端完成这些查询所花费的时间:<?php timer_stop(7); ?>
可以使用3种方式在网页中显示:
1. 公开地显示查询的统计信息:
如果你觉的你的服务器或空间的处理速度好,或者想让你的访问者看到这些查询统计信息的话,你可以将这些统计信息公开显示在页面中:(效果查看博客的页面底部)
<p><?php echo get_num_queries(); ?> queries in < ?php timer_stop(3); ?> seconds</p>
2. 将统计信息显示在源代码中,而不出现页面的内容中:
如果你不想将统计信息显示在页面上供访问者查看,但又想自己可以知道这些统计信息,那么你可以通过html的注释将结果只显示在源代码中(PS:大家也可以从源代码中查看的哦!):
<!-- <?php echo get_num_queries(); ?> queries in < ?php timer_stop(3); ?> seconds -->
3. 统计信息只有你自己登录了后可以看到:
该方法是第二种显示的优化方法,只需博客管理员登录后就可以查看查询统计信息,访问者是无法查看的:
<?php if (current_user_can('level_10')) { echo '<!-- ' . get_num_queries() . ' queries in ' . timer_stop(3) . ' seconds -->'; } ?>
这里需要说明一下的是,这个level_10,在wordpress里面,有个用户编辑的权限,最高的是level_10,最低的是订阅者level_0。wordpress官网是这么说的。
User Level 0
- menu = * My Profile * View site * Logout
- Can login
- Can see existing posts.
- Own posts have (non-functioning) edit/delete buttons
- Can edit own profile.
User Level 1
- menu = * Post / Edit * My Profile * View site * Logout
- Can post.
- Can edit/delete own posts.
- Can edit/delete comments on own posts
User Level 2
- menu = * Post / Edit * My Profile * View site * Logout
- can promote/demote/delete lesser team member in the range 0 – (your level – 1)
User Level 3
- menu = * Post / Edit * Team * Options * Categories * Template * My Profile * View site * Logout
- cannot edit the template
- can add/edit/delete categories
- cannot edit options
- cannot see team members login name!
User Level 4
- menu = * Post / Edit * Team * Options * Categories * Template * My Profile * View site * Logout
- can see team members login name!
- can edit options
- can edit template
User Level 5
- menu = * Post / Edit * Team * Options * Categories * Template * Manage Links * My Profile * View site * Logout
- Can edit timestamps
- can edit links (default value)
要想了解更多,可以访问:https://codex.wordpress.org/User_Levels
如果想查看具体查询了数据库哪些内容,可以用如下解决方法:
1、首先在 wp-config.php 里添加如下代码:
define('SAVEQUERIES', true);
然后在 footer.php 里添加如下代码:
<?php if (is_user_logged_in()){ global $wpdb; echo "<pre>"; print_r($wpdb->queries); echo "</pre>"; } ?>
分析:
1、if (is_user_logged_in())
用于判断当前访客是否已登录,也可以用 if (current_user_can('level_10'))
来判断是否为管理员登录,目的是为了不让游客查看到这些数据,此代码可省;
2、global $wpdb;
定义全局变量$wpdb
,这是Wordpress默认的数据库类;
3、<pre></pre>
将结果嵌套在HTML标签<pre>
内;
4、print_r($wpdb->queries);
输出各次数据库查询的信息。
刷新首页或日志页,可看到类似如下的输出结果:
Array( [0] => Array ( [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 [1] => 0.0003960132598877 [2] => require, wp, WP->main, WP->query_posts, WP_Query->query, WP_Query->get_posts ) [1] => Array ( [0] => SELECT option_value FROM wp_options WHERE option_name = 'nuodou_header_code' LIMIT 1 [1] => 0.0013589859008789 [2] => require, require_once, include, get_header, locate_template, load_template, require_once, get_option ) )
下面的活儿就得自己分析了,看看哪些是可以删除,哪些是可以改进的,以减少数据库查询次数。
未经允许不得转载:哈勃私语 » WordPress显示数据库查询次数和查询花费时间
本文共2700个字 创建时间:2017年11月14日15:13