<?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>Just is here--恰恰在这里 &#187; mysql</title>
	<atom:link href="http://ishere.cn/tag/mysql/feed" rel="self" type="application/rss+xml" />
	<link>http://ishere.cn</link>
	<description>Jena&#039;s blog</description>
	<lastBuildDate>Sat, 14 Jan 2012 07:51:41 +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://ishere.cn/2008/04/14/wordpress-xingnengyouhua.html</link>
		<comments>http://ishere.cn/2008/04/14/wordpress-xingnengyouhua.html#comments</comments>
		<pubDate>Mon, 14 Apr 2008 15:03:41 +0000</pubDate>
		<dc:creator>jena</dc:creator>
				<category><![CDATA[keyboard's joy]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[sql]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.aobodo.com/?p=148</guid>
		<description><![CDATA[WordPress的效率问题似乎一直是议论的热点，今天来看看其中一条SQL语句的效率。 首先将所有插件禁用，并切换为默认模板。 然后按照《WordPress执行效率问题》这篇文章的方法， 在 wp-config.php 中加入 &#60;?php define('SAVEQUERIES', true); ?&#62; 在模板的 footer.php 中加入 &#60;!-- &#60;?php var_dump($wpdb-&#62;queries); ?&#62; --&#62; 访问首页，即可从源代码中看到SQL语句。 执行时间最长的是这一条，花了0.02秒： SELECT DISTINCT * FROM wp_posts WHERE 1=1 AND post_date_gmt &#60;= '2007-01-02 07:12:59' AND ( post_status = 'publish' OR post_author = &#8230; <a href="http://ishere.cn/2008/04/14/wordpress-xingnengyouhua.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>WordPress的效率问题似乎一直是议论的热点，今天来看看其中一条SQL语句的效率。</p>
<p>首先将所有插件禁用，并切换为默认模板。 然后按照《<a href="http://www.21andy.com/blog/20060813/374.html">WordPress执行效率问题</a>》这篇文章的方法， 在 wp-config.php 中加入</p>
<pre>&lt;?php define('SAVEQUERIES', true); ?&gt;</pre>
<p>在模板的 footer.php 中加入</p>
<pre>&lt;!-- &lt;?php var_dump($wpdb-&gt;queries); ?&gt; --&gt;</pre>
<p>访问首页，即可从源代码中看到SQL语句。</p>
<p>执行时间最长的是这一条，花了0.02秒：</p>
<pre>SELECT DISTINCT * FROM wp_posts
WHERE 1=1
AND post_date_gmt &lt;= '2007-01-02 07:12:59'
AND (
post_status = 'publish'
OR
post_author = 1
AND post_status != 'draft'
AND post_status != 'static'
)
AND post_status != 'attachment'
GROUP BY  wp_posts.ID
ORDER BY post_date DESC
LIMIT 0, 10"</pre>
<p>这条语句位于 class.php 的 WP_Query-&gt;&amp;get_posts() 函数。</p>
<p>说说这个SQL语句的问题。不妨先来看看 wp_posts 表的结构(省略无关列)。</p>
<div class="ie5">
<table class="style_table" border="0" cellspacing="1">
<tbody>
<tr>
<td class="style_td">列名</td>
<td class="style_td">类型</td>
<td class="style_td">索引</td>
</tr>
<tr>
<td class="style_td">ID</td>
<td class="style_td">bigint(20)</td>
<td class="style_td">PRIMARY</td>
</tr>
<tr>
<td class="style_td">post_author</td>
<td class="style_td">bigint(20)</td>
<td class="style_td"></td>
</tr>
<tr>
<td class="style_td">post_date_gmt</td>
<td class="style_td">datetime</td>
<td class="style_td"></td>
</tr>
<tr>
<td class="style_td">post_status</td>
<td class="style_td">enum</td>
<td class="style_td">INDEX</td>
</tr>
<tr>
<td class="style_td">post_name</td>
<td class="style_td">varchar(200)</td>
<td class="style_td">INDEX</td>
</tr>
</tbody>
</table>
</div>
<p>首先是关键字 DISTINCT，DISTINCT的作用是去掉重复的数据， 相当于对选择的列进行 GROUP BY。而这条语句中选择了 * 列，包含了主键列 ID， 每行数据必然不相同，因此 DISTINCT 关键字起不到任何作用， 白白浪费一次排序。</p>
<p>其次是 post_date_gmt 和 post_author 上的比较操作。wp_posts表仅有 ID、post_status 和 post_name 列上有索引，对其他列的比较操作使得mysql不得不访问wp_posts表的所有数据， 影响效率。</p>
<p>然后是 post_status 上的不等于的比较。对索引列使用不等于比较， 会导致数据库不能使用索引（索引只能判断等于关系）。 另外显然已经有了 post_status = &#8216;publish&#8217; 的条件， 不必再判断 post_status != &#8216;attachment&#8217;。</p>
<p>最后是 GROUP BY wp_posts.ID，仍然是对一个不会重复的列进行 GROUP BY， 毫无意义。</p>
<p>如果不考虑功能损失，将这个 SQL 语句优化为下面这样，则仅需花费0.003秒。</p>
<pre>SELECT * FROM wp_posts
WHERE 1=1
AND post_status = 'publish'
ORDER BY post_date DESC
LIMIT 0, 10"</pre>
<p>使用Apache Bench做测试，优化前平均每个请求的处理时间为 503.125ms，优化后为 478.125ms。</p>
]]></content:encoded>
			<wfw:commentRss>http://ishere.cn/2008/04/14/wordpress-xingnengyouhua.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

