<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hiro技术站 &#187; Tips</title>
	<atom:link href="http://www.ihiro.org/tag/tips/feed" rel="self" type="application/rss+xml" />
	<link>http://www.ihiro.org</link>
	<description></description>
	<lastBuildDate>Fri, 06 Jan 2012 07:58:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>WordPress:显示你的博客数据库查询次数和查询花费时间</title>
		<link>http://www.ihiro.org/show-queries-and-times</link>
		<comments>http://www.ihiro.org/show-queries-and-times#comments</comments>
		<pubDate>Tue, 04 Aug 2009 08:53:40 +0000</pubDate>
		<dc:creator>Hiro</dc:creator>
				<category><![CDATA[Hack技巧]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Wordpress]]></category>

		<guid isPermaLink="false">http://www.ihiro.org/?p=338</guid>
		<description><![CDATA[<img src="http://www.ihiro.org/blog/wp-content/uploads/2009/08/query.jpg" alt="query" title="query" width="100" height="100" class="alignleft wp-image-339" />
<p>Wordpress提供了一些功能函数可以轻松地显示数据库查询的统计信息，这些信息可以公开地显示在网页中，或者隐藏在源代码中，更或者只有你自己可以看到。Wordpress主要提供了两个统计函数：
<br />
1.网页加载时查询数据库的次数:&#60;?php echo get_num_queries(); ?&#62;<br />
2.服务器端完成这些查询所花费的时间:&#60;?php timer_stop(7); ?&#62;</p>
<p>结果显示效果：29 queries in 0.431 seconds</p>]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.ihiro.org/blog/wp-content/uploads/2009/08/query.jpg" alt="query" title="query" width="150" height="150" class="alignright size-full wp-image-339" /></p>
<p>WordPress提供了一些功能函数可以轻松地显示数据库查询的统计信息，这些信息可以公开地显示在网页中，或者隐藏在源代码中，更或者只有你自己可以看到。Wordpress主要提供了两个统计函数：<br />
<br />
1.网页加载时查询数据库的次数:&lt;?php echo get_num_queries(); ?&gt;<br />
2.服务器端完成这些查询所花费的时间:&lt;?php timer_stop(7); ?&gt;</p>
<p>可以使用3种方式在网页中显示:</p>
<h3>1. 公开地显示查询的统计信息：</h3>
<p>如果你觉的你的服务器或空间的处理速度好，或者想让你的访问者看到这些查询统计信息的话，你可以将这些统计信息公开显示在页面中：（效果查看博客的页面底部）</p>

<div class="wp_codebox"><table><tr id="p3381"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p338code1"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>p<span style="color: #339933;">&gt;&lt;</span> ?php <span style="color: #b1b100;">echo</span> get_num_queries<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> queries in <span style="color: #339933;">&lt;</span> ?php timer_stop<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> seconds<span style="color: #339933;">&lt;/</span>p<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<h3>2. 将统计信息显示在源代码中，而不出现页面的内容中：</h3>
<p>如果你不想将统计信息显示在页面上供访问者查看，但又想自己可以知道这些统计信息，那么你可以通过html的注释将结果只显示在源代码中（PS：大家也可以从源代码中查看的哦！）：</p>

<div class="wp_codebox"><table><tr id="p3382"><td class="line_numbers"><pre>1
</pre></td><td class="code" id="p338code2"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;!--</span> <span style="color: #000000; font-weight: bold;">&lt;?php</span> <span style="color: #b1b100;">echo</span> get_num_queries<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> queries in <span style="color: #339933;">&lt;</span> ?php timer_stop<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span> seconds <span style="color: #339933;">--&gt;</span></pre></td></tr></table></div>

<h3>3. 统计信息只有你自己登录了后可以看到：</h3>
<p>该方法是第二种显示的优化方法，只需博客管理员登录后就可以查看查询统计信息，访问者是无法查看的：</p>

<div class="wp_codebox"><table><tr id="p3383"><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code" id="p338code3"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>current_user_can<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'level_10'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;!-- '</span> <span style="color: #339933;">.</span> get_num_queries<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' queries in '</span> <span style="color: #339933;">.</span> timer_stop<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' seconds --&gt;'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>

<p>查询结果显示的效果可以观测我博客的底部。</p>
<h2  class="related_post_title">你也许会喜欢阅读这些：</h2><ul class="related_post"><li>2010年06月3日 -- <a href="http://www.ihiro.org/wordpress-3-new-features" title="Wordpress:3.0版本特性，将改变你的内容管理方式">Wordpress:3.0版本特性，将改变你的内容管理方式</a> (12)</li><li>2010年03月25日 -- <a href="http://www.ihiro.org/the-enhanced-ajax-effect-instructions" title="Ajax:近期博客改版的增强Ajax加载说明">Ajax:近期博客改版的增强Ajax加载说明</a> (43)</li><li>2010年02月20日 -- <a href="http://www.ihiro.org/how-to-insert-googlebuzz-data-into-blog" title="Wordpress:如何将GoogleBuzz数据插入到博客中">Wordpress:如何将GoogleBuzz数据插入到博客中</a> (11)</li><li>2009年11月25日 -- <a href="http://www.ihiro.org/wordpress-the-article-automatically-appear-as-two" title="Wordpress:将文章自动显示为两列">Wordpress:将文章自动显示为两列</a> (13)</li><li>2009年11月8日 -- <a href="http://www.ihiro.org/my-third-theme-innernews" title="主题:我的第三个主题innerNews提供下载">主题:我的第三个主题innerNews提供下载</a> (24)</li><li>2009年09月16日 -- <a href="http://www.ihiro.org/including-wordpress-comment-reply-js" title="Wordpress:正确地引入comment-reply.js文件">Wordpress:正确地引入comment-reply.js文件</a> (23)</li><li>2009年09月13日 -- <a href="http://www.ihiro.org/wordpress-2-9-version-new-features" title="Wordpress:2.9版本&#8211;新的特性">Wordpress:2.9版本&#8211;新的特性</a> (13)</li><li>2009年09月6日 -- <a href="http://www.ihiro.org/highlight-the-administrators-reply-message" title="Wordpress:高亮管理员的留言信息">Wordpress:高亮管理员的留言信息</a> (37)</li><li>2009年08月31日 -- <a href="http://www.ihiro.org/4-ways-to-display-the-copyrights-of-blog" title="Wordpress:4个常见的显示博客版权的方法">Wordpress:4个常见的显示博客版权的方法</a> (10)</li><li>2009年08月13日 -- <a href="http://www.ihiro.org/the-reason-of-update-password-reset-loophole" title="Wordpress:最新2.84更新的原因：密码重置漏洞（图文教程解决）">Wordpress:最新2.84更新的原因：密码重置漏洞（图文教程解决）</a> (15)</li></ul>]]></content:encoded>
			<wfw:commentRss>http://www.ihiro.org/show-queries-and-times/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
<!-- WP Super Cache is installed but broken. The path to wp-cache-phase1.php in wp-content/advanced-cache.php must be fixed! -->
