<?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如何进行实际分区]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Thu, 09 Sep 2010 06:52:43 +0000</pubDate> 
<guid>http://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	如何进行实际分区&nbsp;&nbsp;<br/>看看分区的实际效果吧。我们建立几个同样的MyISAM引擎的表，包含日期敏感的数据，但只对其中一个分区。分区的表（表名为part_tab）我们采用Range范围分区模式，通过年份进行分区：<br/><div class="code">REATE TABLE part_tab(<br/>c1 int default NULL ,<br/>c2 varchar( 30 ) default NULL ,<br/>c3 date default NULL <br/>) ENGINE = MYISAM PARTITION BY RANGE (<br/>year( c3 ) <br/>)(<br/>PARTITION p0 VALUES LESS THAN( 1995 ) , PARTITION p1<br/>VALUES LESS THAN( 1996 ) , PARTITION p2<br/>VALUES LESS THAN( 1997 ) , PARTITION p3<br/>VALUES LESS THAN( 1998 ) , PARTITION p4<br/>VALUES LESS THAN( 1999 ) , PARTITION p5<br/>VALUES LESS THAN( 2000 ) , PARTITION p6<br/>VALUES LESS THAN( 2001 ) , PARTITION p7<br/>VALUES LESS THAN( 2002 ) , PARTITION p8<br/>VALUES LESS THAN( 2003 ) , PARTITION p9<br/>VALUES LESS THAN( 2004 ) , PARTITION p10<br/>VALUES LESS THAN( 2010 ) , PARTITION p11<br/>VALUES LESS THAN MAXVALUE<br/>);</div><br/><br/><br/>注意到了这里的最后一行吗？这里把不属于前面年度划分的年份范围都包含了，这样才能保证数据不会出错，大家以后要记住啊，不然数据库无缘无故出错你就爽了。那下面我们建立没有分区的表（表名为no_part_tab）：&nbsp;&nbsp;<br/><br/><div class="code">CREATE TABLE no_part_tab(<br/>c1 int( 11 ) default NULL ,<br/>c2 varchar( 30 ) default NULL ,<br/>c3 date default NULL <br/>) ENGINE = MYISAM ;</div><br/><br/><br/>下面咱写一个存储过程（感谢Peter Gulutzan给的代码，如果大家需要Peter Gulutzan的存储过程教程的中文翻译也可以跟我要，chenpengyi◎gmail.com），它能向咱刚才建立的已分区的表中平均的向每个分区插入共8百万条不同的数据。填满后，咱就给没分区的克隆表中插入相同的数据：<br/><br/><br/>mysql&gt; delimiter //&nbsp;&nbsp;<br/>mysql&gt; CREATE PROCEDURE load_part_tab()&nbsp;&nbsp;<br/>&nbsp;&nbsp;-&gt; begin&nbsp;&nbsp;<br/>&nbsp;&nbsp;-&gt; declare v int default 0;&nbsp;&nbsp;<br/>&nbsp;&nbsp;-&gt; while v &lt; 8000000&nbsp;&nbsp;<br/>&nbsp;&nbsp;-&gt; do&nbsp;&nbsp;<br/>&nbsp;&nbsp;-&gt; insert into part_tab&nbsp;&nbsp;<br/>&nbsp;&nbsp;-&gt; values (v,&#039;testing partitions&#039;,adddate(&#039;1995-01-01&#039;,(rand(v)*36520) mod 3652));&nbsp;&nbsp;<br/>&nbsp;&nbsp;-&gt; set v = v + 1;&nbsp;&nbsp;<br/>&nbsp;&nbsp;-&gt; end while;&nbsp;&nbsp;<br/>&nbsp;&nbsp;-&gt; end&nbsp;&nbsp;<br/>&nbsp;&nbsp;-&gt; //&nbsp;&nbsp;<br/>Query OK, 0 rows affected (0.00 sec)&nbsp;&nbsp;<br/>mysql&gt; delimiter ;&nbsp;&nbsp;<br/>mysql&gt; call load_part_tab();&nbsp;&nbsp;<br/>Query OK, 1 row affected (8 min 17.75 sec)&nbsp;&nbsp;<br/>mysql&gt; insert into no_part_tab select * from part_tab;&nbsp;&nbsp;<br/>Query OK, 8000000 rows affected (51.59 sec)&nbsp;&nbsp;<br/>Records: 8000000 Duplicates: 0 Warnings: 0&nbsp;&nbsp;<br/><br/>表都准备好了。咱开始对这两表中的数据进行简单的范围查询吧。先分区了的，后没分区的，跟着有执行过程解析（MySQL Explain命令解析器），可以看到MySQL做了什么：&nbsp;&nbsp;<br/>mysql&gt; select count(*) from no_part_tab where&nbsp;&nbsp;<br/>&nbsp;&nbsp;-&gt; c3 &gt; date &#039;1995-01-01&#039; and c3 &lt; date &#039;1995-12-31&#039;;&nbsp;&nbsp;<br/>+----------+&nbsp;&nbsp;<br/>&#124; count(*) &#124;&nbsp;&nbsp;<br/>+----------+&nbsp;&nbsp;<br/>&#124; 795181 &#124;&nbsp;&nbsp;<br/>+----------+&nbsp;&nbsp;<br/>1 row in set (38.30 sec)&nbsp;&nbsp;<br/> <br/>mysql&gt; select count(*) from part_tab where&nbsp;&nbsp;<br/>&nbsp;&nbsp;-&gt; c3 &gt; date &#039;1995-01-01&#039; and c3 &lt; date &#039;1995-12-31&#039;;&nbsp;&nbsp;<br/>+----------+&nbsp;&nbsp;<br/>&#124; count(*) &#124;&nbsp;&nbsp;<br/>+----------+&nbsp;&nbsp;<br/>&#124; 795181 &#124;&nbsp;&nbsp;<br/>+----------+&nbsp;&nbsp;<br/>1 row in set (3.88 sec)&nbsp;&nbsp;<br/> <br/>mysql&gt; explain select count(*) from no_part_tab where&nbsp;&nbsp;<br/>&nbsp;&nbsp;-&gt; c3 &gt; date &#039;1995-01-01&#039; and c3 &lt; date &#039;1995-12-31&#039;&#92;G&nbsp;&nbsp;<br/>*************************** 1. row ***************************&nbsp;&nbsp;<br/>&nbsp;&nbsp;id: 1&nbsp;&nbsp;<br/> select_type: SIMPLE&nbsp;&nbsp;<br/>&nbsp;&nbsp;table: no_part_tab&nbsp;&nbsp;<br/>&nbsp;&nbsp;type: ALL&nbsp;&nbsp;<br/>possible_keys: NULL&nbsp;&nbsp;<br/>&nbsp;&nbsp;key: NULL&nbsp;&nbsp;<br/>&nbsp;&nbsp;key_len: NULL&nbsp;&nbsp;<br/>&nbsp;&nbsp;ref: NULL&nbsp;&nbsp;<br/>&nbsp;&nbsp;rows: 8000000&nbsp;&nbsp;<br/>&nbsp;&nbsp;Extra: Using where&nbsp;&nbsp;<br/>1 row in set (0.00 sec)&nbsp;&nbsp;<br/> <br/>mysql&gt; explain partitions select count(*) from part_tab where&nbsp;&nbsp;<br/>&nbsp;&nbsp;-&gt; c3 &gt; date &#039;1995-01-01&#039; and c3 &lt; date &#039;1995-12-31&#039;&#92;G&nbsp;&nbsp;<br/>*************************** 1. row ***************************&nbsp;&nbsp;<br/>&nbsp;&nbsp;id: 1&nbsp;&nbsp;<br/> select_type: SIMPLE&nbsp;&nbsp;<br/>&nbsp;&nbsp;table: part_tab&nbsp;&nbsp;<br/>&nbsp;&nbsp;partitions: p1&nbsp;&nbsp;<br/>&nbsp;&nbsp;type: ALL&nbsp;&nbsp;<br/>possible_keys: NULL&nbsp;&nbsp;<br/>&nbsp;&nbsp;key: NULL&nbsp;&nbsp;<br/>&nbsp;&nbsp;key_len: NULL&nbsp;&nbsp;<br/>&nbsp;&nbsp;ref: NULL&nbsp;&nbsp;<br/>&nbsp;&nbsp;rows: 798458&nbsp;&nbsp;<br/>&nbsp;&nbsp;Extra: Using where&nbsp;&nbsp;<br/>1 row in set (0.00 sec)&nbsp;&nbsp;<br/>从上面结果可以容易看出，设计恰当表分区能比非分区的减少90％的响应时间。而命令解析Explain程序也告诉我们在对已分区的表的查询过程中仅对第一个分区进行了扫描，其他都跳过了。&nbsp;&nbsp;<br/>
]]>
</description>
</item><item>
<link>http://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] Mysql如何进行实际分区]]></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>