WordPress统计当前页面数据库查询次数及消耗时间的方法
“wdmsgzz”通过精心收集,向本站投稿了2篇WordPress统计当前页面数据库查询次数及消耗时间的方法,以下是小编帮大家整理后的WordPress统计当前页面数据库查询次数及消耗时间的方法,供大家参考借鉴,希望可以帮助到您。
篇1:WordPress统计当前页面数据库查询次数及消耗时间的方法
这篇文章主要为大家介绍了WordPress统计当前页面数据库查询次数及消耗时间的方法,涉及get_num_queries与timer_stop函数的使用技巧,需要的朋友可以参考下
在WordPress中可统计当前页面数据库查询次数及消耗时间我们可直接使用两个函数get_num_queries与timer_stop函数,这里我们举例说明如下:
1.网页加载时查询数据库的次数:
代码如下:2.服务器端完成这些查询所花费的时间:
代码如下:在 footer.php 合适位置添加了如下代码,以显示wordpress查询数据库次数及查询耗时
本页共执行
代码如下:次查询操作耗时
代码如下:秒希望本文所述对大家的WordPress建站有所帮助,
篇2:输出WordPress数据库查询的具体内容,减少数据库查询次数
最近做一个wordpress的企业模板,关注了一下查询次数这个东西!
在 footer.php 里添加了如下代码,以显示wordpress查询数据库次数及查询耗时:
结果显示首页查询30次,日志页查询达45次,。。真是郁闷,为了查看具体查询了数据库哪些内容,Google了一下,得到如下解决方法,这里总结出来:
首先在 wp-config.php 里添加如下代码:
define('SAVEQUERIES', true);
然后在 footer.php 里添加如下代码:
<?php if (is_user_logged_in()){ global $wpdb; echo “
queries);”>“;} ?>
分析:
1、if (is_user_logged_in()) 用于判断当前访客是否已登录,也可以用 if (current_user_can('level_10')) 来判断是否为管理员登录,目的是为了不让游客查看到这些数据,此代码可省;
2、global $wpdb; 定义全局变量$wpdb,这是Wordpress默认的数据库类;
3、
将结果嵌套在HTML标签
内;
4、print_r($wpdb->queries); 输出各次数据库查询的信息。
刷新首页或日志页,可看到类似如下的输出结果:
SELECT” by=“BY” desc=“DESC” from=“FROM” get_header=“get_header,” get_optionnbspnbspnbspnbspnbspnbspnbspnbspnbspnbsphelliphellip=“get_option ) ……<” gt=“>” include=“include,” limit=“LIMIT” load_template=“load_template,” locate_template=“locate_template,” nbsporder=“) ORDER” option_name=“nuodou_header_code” option_value=“option_value” or=“OR” pre=“pre” require=“require,” require_once=“require_once,” select=“SELECT” sql_calc_found_rowsnbspwp_posts.=“SQL_CALC_FOUND_ROWS wp_posts.*” where=“WHERE” wp=“wp,” wp-gtmain=“WP->main,” wp-gtquery_posts=“WP->query_posts,” wp_options=“wp_options” wp_posts.post_date=“wp_posts.post_date” wp_posts.post_status=“private” wp_posts.post_type=“post” wp_postsnbspwhere=“wp_posts WHERE” wp_query-gtget_postsnbspnbspnbspnbspnbspnbspnbspnbspnbspnbsp1=“>” wp_query-gtquery=“WP_Query->query,”>
下面的活儿就得自己分析了,看看哪些是可以删除,哪些是可以改进的!
【WordPress统计当前页面数据库查询次数及消耗时间的方法】相关文章:






文档为doc格式