<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>http://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://jackxiang.com/post//</link>
<title><![CDATA[mysql 中where 和 order by 的索引优化]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Thu, 09 Dec 2010 08:11:22 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	mysql的where和order by 在一起时，order by 后的索引不能命中。<br/><br/>有这样的问题：<br/>mysql&gt; explain SELECT * FROM product WHERE compID = 120&nbsp;&nbsp;LIMIT 0,15;<br/>+—-+————-+————+——+——————+———–+———+——-+——+——-+<br/>&#124; id &#124; select_type &#124; table &#124; type &#124; possible_keys &#124; key &#124; key_len &#124; ref &#124; rows &#124; Extra &#124;<br/>+—-+————-+————+——+——————+———–+———+——-+——+——-+<br/>&#124; 1 &#124; SIMPLE &#124; product &#124; ref &#124; comp_prod,compID &#124; comp_prod &#124; 4 &#124; const &#124; 4 &#124; &#124;<br/>+—-+————-+————+——+——————+———–+———+——-+——+——-+<br/>1 row in set (0.00 sec)<br/><br/>mysql&gt; explain SELECT * FROM product WHERE compID =120&nbsp;&nbsp;order by id desc LIMIT 0,15;<br/>+—-+————-+————+——-+——————+———+———+——+———-+————-+<br/>&#124; id &#124; select_type &#124; table &#124; type &#124; possible_keys &#124; key &#124; key_len &#124; ref &#124; rows &#124; Extra &#124;<br/>+—-+————-+————+——-+——————+———+———+——+———-+————-+<br/>&#124; 1 &#124; SIMPLE &#124; product &#124; index &#124; comp_prod,compID &#124; PRIMARY &#124; 4 &#124; NULL &#124; 14556379 &#124; Using where &#124;<br/>+—-+————-+————+——-+——————+———+———+——+———-+————-+<br/><br/>其中 id为主键<br/><br/>第二explain显示，order by 后面的索引没有使用到。<br/><br/>##### 搜索的资料###########3<br/><br/>在某些情况下，MySQL不能使用索引来解决ORDER BY，尽管它仍然使用索引来找到匹配WHERE子句的行。这些情况包括：<br/><br/>· 对不同的关键字使用ORDER BY：<br/><br/>· SELECT * FROM t1 ORDER BY key1, key2；<br/>· 对关键字的非连续元素使用ORDER BY：<br/><br/>· SELECT * FROM t1 WHERE key2=constant ORDER BY key_part2；<br/>· 混合ASC和DESC：<br/><br/>· SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 ASC；<br/>· 用于查询行的关键字与ORDER BY中所使用的不相同：<br/><br/>· SELECT * FROM t1 WHERE key2=constant ORDER BY key1；<br/>· 你正联接许多表，并且ORDER BY中的列并不是全部来自第1个用于搜索行的非常量表。(这是EXPLAIN输出中的没有const联接类型的第1个表）。<br/><br/>· 有不同的ORDER BY和GROUP BY表达式。<br/><br/>· 使用的表索引的类型不能按顺序保存行。例如，对于HEAP表的HASH索引情况即如此。<br/><br/>所以在这里order by key 必然会扫表，你能做的，只能是利用key1，key2的索引。<br/><br/>#####################<br/><br/>经过自己的测试，还可以使用 联合索引。这里 alter table add index prod_comp(id,compID) 。不过需注意索引不宜过多。<br/><br/><br/><br/>来源：http://www.sphinxsearch.org/archives/19
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] mysql 中where 和 order by 的索引优化]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>