<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://www.jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>http://www.jackxiang.com/post//</link>
<title><![CDATA[MySQL count(*) 与 count(col) 查询效率比较]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Tue, 29 Dec 2009 05:37:35 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	优化总结：<br/><br/>1.任何情况下SELECT COUNT(*) FROM xxx 是最优选择；<br/>2.尽量减少SELECT COUNT(*) FROM xxx WHERE COL = ‘xxx’ 这种查询；<br/>3.杜绝SELECT COUNT(COL) FROM tablename WHERE COL = ‘xxx’ 的出现。(其中COL非主键)<br/><br/>环境：<br/>MySQL版本：5.0.45<br/>OS：Windows XP SP3<br/><br/>数据表一：sphinx<br/>+———-+——————+——+—–+———+—————-+<br/>&#124; Field&nbsp;&nbsp;&nbsp;&nbsp;&#124; Type&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; Null &#124; Key &#124; Default &#124; Extra&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;<br/>+———-+——————+——+—–+———+—————-+<br/>&#124; id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; int(10) unsigned &#124; NO&nbsp;&nbsp; &#124; PRI &#124; NULL&nbsp;&nbsp;&nbsp;&nbsp;&#124; auto_increment &#124;<br/>&#124; til&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; varchar(100)&nbsp;&nbsp;&nbsp;&nbsp; &#124; NO&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;<br/>&#124; content&nbsp;&nbsp;&#124; text&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; NO&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;<br/>&#124; dataline &#124; int(11)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; NO&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;<br/>+———-+——————+——+—–+———+—————-+<br/><br/>记录数：1120100<br/><br/>查询一：<br/><br/>mysql&gt; select count(*) as totalnum from sphinx;<br/>+———-+<br/>&#124; totalnum &#124;<br/>+———-+<br/>&#124;&nbsp;&nbsp;1120100 &#124;<br/>+———-+<br/>1 row in set (0.00 sec)<br/><br/>查询二：<br/><br/>mysql&gt; select count(*) as totalnum from sphinx where id&gt;1000;<br/>+———-+<br/>&#124; totalnum &#124;<br/>+———-+<br/>&#124;&nbsp;&nbsp;1119100 &#124;<br/>+———-+<br/>1 row in set (2.17 sec)<br/><br/>查询三：<br/><br/>mysql&gt; select count(*) as totalnum from sphinx where id&gt;1000;<br/>+———-+<br/>&#124; totalnum &#124;<br/>+———-+<br/>&#124;&nbsp;&nbsp;1119100 &#124;<br/>+———-+<br/>1 row in set (0.61 sec)<br/><br/>查询四：<br/><br/>mysql&gt; select count(*) as totalnum from sphinx where id&gt;1000;<br/>+———-+<br/>&#124; totalnum &#124;<br/>+———-+<br/>&#124;&nbsp;&nbsp;1119100 &#124;<br/>+———-+<br/>1 row in set (0.61 sec)<br/><br/>查询五：<br/><br/>mysql&gt; select count(id) as totalnum from sphinx;<br/>+———-+<br/>&#124; totalnum &#124;<br/>+———-+<br/>&#124;&nbsp;&nbsp;1120100 &#124;<br/>+———-+<br/>1 row in set (0.00 sec)<br/><br/>查询六：<br/><br/>mysql&gt; select count(til) as totalnum from sphinx where id&gt;1000;<br/>+———-+<br/>&#124; totalnum &#124;<br/>+———-+<br/>&#124;&nbsp;&nbsp;1119100 &#124;<br/>+———-+<br/>1 row in set (1 min 38.61 sec)<br/><br/>查询七：<br/><br/>mysql&gt; select count(id) as totalnum from sphinx where id&gt;11000;<br/>+———-+<br/>&#124; totalnum &#124;<br/>+———-+<br/>&#124;&nbsp;&nbsp;1109100 &#124;<br/>+———-+<br/>1 row in set (0.61 sec)<br/><br/>查询八：<br/><br/>mysql&gt; select count(id) as totalnum from sphinx;<br/>+———-+<br/>&#124; totalnum &#124;<br/>+———-+<br/>&#124;&nbsp;&nbsp;1120100 &#124;<br/>+———-+<br/>1 row in set (0.03 sec)<br/><br/>结论：<br/><br/>在 select count() 没有 where 条件的时候 select count(*) 和 select count(col) 所消耗的查询时间相差无几。<br/>在 select count() 有 where 条件的时候 select count(col) 所消耗的查询时间 比 select count(*) 明显多出数量级的时间。<br/>
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] MySQL count(*) 与 count(col) 查询效率比较]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>