<?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>超群.com的博客 &#187; index_merge</title>
	<atom:link href="http://www.fuchaoqun.com/tag/index_merge/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.fuchaoqun.com</link>
	<description></description>
	<lastBuildDate>Thu, 08 Sep 2011 15:08:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>我也不知道的MySQL</title>
		<link>http://www.fuchaoqun.com/2009/03/mysql-use-multi-index/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mysql-use-multi-index</link>
		<comments>http://www.fuchaoqun.com/2009/03/mysql-use-multi-index/#comments</comments>
		<pubDate>Fri, 13 Mar 2009 06:37:11 +0000</pubDate>
		<dc:creator>超群.com</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[index_merge]]></category>
		<guid isPermaLink="false">http://chaoqun.17348.com/?p=189</guid>
		<description><![CDATA[以前看过很多文章说MySQL一次查询只能用到一个索引，在实践中也没注意，一直奉为金科玉律，以此教育自己不要乱建索引，殊不知已是明日黄花。 MySQL5.0以后引入了index_merge，在一些特定的查询中可以合并索引，详细的内容查看[中文] [英文]，接着测试一下，还是那张表，数据还是10,000条。 mysql&#62; desc tbl_name; +-------+--------------+------+-----+---------+-------+ &#124; Field &#124; Type         &#124; Null &#124; Key &#124; Default &#124; Extra &#124; +-------+--------------+------+-----+---------+-------+ &#124; uid   &#124; int(11)      &#124; NO   &#124; MUL &#124; NULL    &#124;       &#124; &#124; sid   &#124; mediumint(9) &#124; NO   &#124;     &#124; NULL    &#124;       &#124; &#124; times &#124; mediumint(9) &#124; NO   &#124;     &#124; NULL    &#124;       &#124; [...]]]></description>
			<content:encoded><![CDATA[<p>以前看过很多文章说MySQL一次查询只能用到一个索引，在实践中也没注意，一直奉为金科玉律，以此教育自己不要乱建索引，殊不知已是<a href="http://baike.baidu.com/view/46697.htm" target="_blank">明日黄花</a>。</p>
<p><strong>MySQL5.0以后引入了index_merge</strong>，在一些特定的查询中可以合并索引，详细的内容查看[<a href="http://dev.mysql.com/doc/refman/5.1/zh/optimization.html#index-merge-optimization" target="_blank">中文</a>] [<a href="http://dev.mysql.com/doc/refman/5.0/en/index-merge-optimization.html" target="_blank">英文</a>]，接着测试一下，还是那张表，数据还是10,000条。</p>
<blockquote>
<pre>mysql&gt; desc tbl_name;
+-------+--------------+------+-----+---------+-------+
| Field | Type         | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| uid   | int(11)      | NO   | MUL | NULL    |       |
| sid   | mediumint(9) | NO   |     | NULL    |       |
| times | mediumint(9) | NO   |     | NULL    |       |
+-------+--------------+------+-----+---------+-------+
3 rows in set (0.00 sec)</pre>
</blockquote>
<p>执行查询测试：</p>
<blockquote>
<pre>mysql&gt; explain select * from tbl_name where uid = 104460 and times = 38\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: tbl_name
         type: ref
possible_keys: uid
          key: uid
      key_len: 4
          ref: const
         rows: <span style="color: #ff0000;">9</span>
        Extra: Using where
1 row in set (0.00 sec)</pre>
</blockquote>
<p>用到了uid索引，扫描了9行。在times上也加上索引：</p>
<blockquote><p>mysql&gt; create index times on tbl_name(times);</p></blockquote>
<p>再执行上面的查询：</p>
<blockquote>
<pre>mysql&gt; explain select * from tbl_name where uid = 104460 and times = 38\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: tbl_name
         type: index_merge
possible_keys: uid,times
          key: uid,times
      key_len: 4,3
          ref: NULL
         rows: <span style="color: #ff0000;">1</span>
        Extra: Using intersect(uid,times); Using where
1 row in set (0.00 sec)</pre>
</blockquote>
<p>用到了索引合并交集访问算法，只扫描了一行。注意索引合并只对一些特定的查询有用（注意看文档），比如下面的：</p>
<blockquote>
<pre>mysql&gt; explain select * from tbl_name where uid = 104460 and times &gt; 38\G;
*************************** 1. row ***************************
           id: 1
  select_type: SIMPLE
        table: tbl_name
         type: ref
possible_keys: uid,times
          key: uid
      key_len: 4
          ref: const
         rows: 9
        Extra: Using where
1 row in set (0.00 sec)</pre>
</blockquote>
<p>挂了，还是只用到了uid索引，貌似还和MySQL的版本有一些关系，可以看看这篇博文<a href="http://www.alidba.net/index.php/archives/81" target="_blank">http://www.alidba.net/index.php/archives/81</a></p>
<p>致歉，我传讹了，请大家更新&#8221;大脑数据库&#8221;，感谢xiaowei同学提醒，不知道的还很多，共同学习，一起进步。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.fuchaoqun.com/2009/03/mysql-use-multi-index/feed/</wfw:commentRss>
		<slash:comments>2</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! -->
