<?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 联合索引和unique索引]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Tue, 29 Jul 2008 10:25:49 +0000</pubDate> 
<guid>http://www.jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	注意：Index（Name，Age）表示在Name，Age两列上建立联合索引<br/>ALTER TABLE tablename ADD INDEX(Tid,Uid);<br/>由于索引对数据库的查询性能有着至关重要的影响，下面是我的一些总结和体会：<br/><br/>一个查询一次只能使用一个索引：select name from user where name=&#039;plantegg&#039; and age&gt;35 , 如果Index（name); Index(age)的话，MySQL查询优化器会自动选择一个索引来使用；<br/>MySQL选择哪个索引，可以这样来看：mysql&gt; show index from photo;<br/>+-------+------------+------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+<br/>&#124; Table &#124; Non_unique &#124; Key_name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; Seq_in_index &#124; Column_name&nbsp;&nbsp; &#124; Collation &#124; Cardinality &#124; Sub_part &#124; Packed &#124; Null &#124; Index_type &#124; Comment &#124;<br/>+-------+------------+------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+<br/>&#124; photo &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0 &#124; PRIMARY&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124; photo_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;237871 &#124;&nbsp;&nbsp;&nbsp;&nbsp; NULL &#124; NULL&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; BTREE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; <br/>&#124; photo &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124; index_random&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124; random&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;237871 &#124;&nbsp;&nbsp;&nbsp;&nbsp; NULL &#124; NULL&nbsp;&nbsp; &#124; YES&nbsp;&nbsp;&#124; BTREE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; <br/>&#124; photo &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124; FK_photo_profile_id&nbsp;&nbsp;&nbsp;&nbsp;&#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124; profile_id&nbsp;&nbsp;&nbsp;&nbsp;&#124; A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;237871 &#124;&nbsp;&nbsp;&nbsp;&nbsp; NULL &#124; NULL&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; BTREE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; <br/>&#124; photo &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124; FK_photo_temp_photo_id &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124; temp_photo_id &#124; A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;237871 &#124;&nbsp;&nbsp;&nbsp;&nbsp; NULL &#124; NULL&nbsp;&nbsp; &#124; YES&nbsp;&nbsp;&#124; BTREE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; <br/>&#124; photo &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124; FK_photo_album_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1 &#124; album_id&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124; A&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;237871 &#124;&nbsp;&nbsp;&nbsp;&nbsp; NULL &#124; NULL&nbsp;&nbsp; &#124; YES&nbsp;&nbsp;&#124; BTREE&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#124;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#124; <br/>+-------+------------+------------------------+--------------+---------------+-----------+-------------+----------+--------+------+------------+---------+<br/>Cardinality越大表示索引候选分得越细（默认都是BTree索引）；<br/>你也可以试试Force Index强制使用某个索引看看速度是不是MySQL是不是查询起来更快（如果真是这样的话你需要Analyze yourTable 了，MySQL重新计算你的Cardinality以帮助他正确地选择INDEX）<br/>仔细分析Explain的结果：重点留意Extra，Key，Rows，Select_type的结果！<br/>小心查询中的Group by 、order by之类的，基本上这样的查询在Explain的时候都会出现： Using where; Using temporary; Using filesort<br/>联合索引要小心使用，Index（Name，Age)时,如果where name=&#039;pp&#039; 能使用索引，where age=25时不能使用索引；where name=&#039;pp&#039; and age&gt;25 能使用索引；&nbsp;&nbsp;&nbsp;&nbsp;where name =&#039;pp&#039;&nbsp;&nbsp;order by&nbsp;&nbsp;age&nbsp;&nbsp;能使用索引；&nbsp;&nbsp;where&nbsp;&nbsp;name&gt;&#039;pp&#039;&nbsp;&nbsp;order by age&nbsp;&nbsp;不能使用索引，但是 where&nbsp;&nbsp;name&gt;&#039;pp&#039;&nbsp;&nbsp;order by name,age&nbsp;&nbsp;能使用索引,请仔细留意差异&nbsp;&nbsp;;&nbsp;&nbsp;order by name asc age desc 将不能使用索引！<br/>索引只有被加入到内存里的时候对你的查询才有帮助，如果索引太大根本无法放入内存这样的索引失去了意义！访问索引的时候还需要Random&nbsp;&nbsp;Aceess&nbsp;&nbsp;Disk这比不用索引还慢！<br/>select&nbsp;&nbsp;的时候能不用select * 就不要用，也就是需要哪些列只拿那些列（Hibernate那些对性能没有啥好处的）,比如：在Index（Name)的时候，select * from user where name like &#039;pp%&#039; 和 select name from user where name like &#039;pp%&#039; 两者性能千差万别，如果有10000条符合记录的结果的话（User表总共有10亿条记录）前一个查询可能需要2分钟（假设你的系统每秒100 IOPS的样子）后一个查询可能只需要0.01秒！因为前一个查询要从硬盘上取出散布在到处的这10000条记录，后一个查询直接从内存中的索引上拿Name就够了！后一个查询你explain的时候在Extra中会看到Using Index。<br/><br/>永远要警惕对磁盘的随机访问，顺序读写和随机访问的性能差别是N个数量级的（顺序读写的时候你的ＯＳ、Ｄｉｓｈ　Cache 这个时候大显身手）对这个问题如果感兴趣的话建议你去用C写个测试程序，随机读写的时候不断地fseek,相应地同样的功能你不要fseek而是通过顺序读写到内存中，在内存自己扔掉那些应该由磁盘去fseek的地方，应该明白我的意思吧！<br/>5.0.27后，MYSQL就支持set profling=1了，这样可以详细分析你的SQL语句每一步骤的时间消耗了<br/>如果order by 的时候有 limit + 索引配合的话，你会有意外惊喜的<br/>如果你能够看到一个查询能像MySQL查询优化器那样在大脑中分解他的话，我想对你来说没啥难题了，:-)<br/><br/>待续，下次想到了再添加吧<br/><br/>[code]alter table Tbl_User ADD UNIQUE INDEX (FQQ);[/code]
]]>
</description>
</item><item>
<link>http://www.jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] [转] mysql 联合索引和unique索引]]></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>