<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[Heck's  Blog]]></title> 
<link>https://www.heckjj.com/index.php</link> 
<description><![CDATA[一瞬间的决定，往往可以改变很多，事实上，让自己成功的往往不是知识，是精神！ 如果你总是为自己找借口，那只好让成功推迟。执行力，今天！]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[Heck's  Blog]]></copyright>
<item>
<link>https://www.heckjj.com/post//</link>
<title><![CDATA[MySQL分区建索引和唯一索引]]></title> 
<author>Heck &lt;@hecks.tk&gt;</author>
<category><![CDATA[数据库]]></category>
<pubDate>Wed, 19 Jan 2022 06:22:55 +0000</pubDate> 
<guid>https://www.heckjj.com/post//</guid> 
<description>
<![CDATA[ 
	mysql分区后每个分区成了独立的文件，虽然从逻辑上还是一张表其实已经分成了多张独立的表，从“information_schema.INNODB_SYS_TABLES”系统表可以看到每个分区都存在独立的TABLE_ID,由于Innodb数据和索引都是保存在&quot;.ibd&quot;文件当中（从INNODB_SYS_INDEXES系统表中也可以得到每个索引都是对应各自的分区(primary key和unique也不例外）），所以分区表的索引也是随着各个分区单独存储。<br/><br/>在INNODB_SYS_INDEXES系统表中type代表索引的类型;<br/><br/>0:一般的索引,<br/><br/>1:(GEN_CLUST_INDEX)不存在主键索引的表,会自动生成一个6个字节的标示值，<br/><br/>2:unique索引,<br/><br/>3:primary索引;<br/><br/>所以当我们在分区表中创建索引时其实也是在每个分区中创建索引，每个分区维护各自的索引（其实也就是local index）；对于一般的索引(非主键或者唯一)没什么问题由于索引树中只保留了索引key和主键key(如果存在主键则是主键的key否则就是系统自动生成的6个的key)不受分区的影响；但是如果表中存在主键就不一样了，虽然在每个分区文件中都存在主键索引但是主键索引需要保证全局的唯一性就是所有分区中的主键的值都必须唯一（唯一键也是一样的道理），所以在创建分区时如果表中存在主键或者唯一键那么分区列必须包含主键或者唯一键的部分或者全部列（全部列还好理解，部分列也可以个人猜测是为了各个分区和主键建立关系），由于需要保证全局性又要保证插入数据更新数据到具体的分区所以就需要将分区和主键建立关系,由于通过一般的索引进行查找其它非索引字段需要通过主键如果主键不能保证全局唯一性的话那么就需要去每个分区查找了，这样性能可想而知。<br/><br/>To enforce the uniqueness we only allow mapping of each unique/primary key value to one partition.If we removed this limitation it would mean that for every insert/update we need to check in every partition to verify that it is unique. Also PK-only lookups would need to look into every partition.<br/> <br/>索引方式：<br/>性能依次降低<br/>1.主键分区<br/><br/>主键分区即字段是主键同时也是分区字段，性能最好<br/><br/>2. 部分主键+分区索引<br/><br/>使用组合主键里面的部分字段作为分区字段，同时将分区字段建索引（见下面详细说明）<br/><br/>3.分区索引<br/><br/>没有主键，只有分区字段且分区字段建索引<br/><br/>4.分区+分区字段没有索引<br/><br/>只建了分区，但是分区字段没有建索引<br/><br/>总结<br/><br/>因为每一个表都需要有主键这样可以减少很多锁的问题，由于上面讲过主键需要解决全局唯一性并且在插入和更新时可以不需要去扫描全部分区，造成主键和分区列必须存在关系；所以最好的分区效果是使用主键作为分区字段其次是使用部分主键作为分区字段且创建分区字段的索引，其它分区方式都建议不采取。<br/><br/><br/>MYSQL的分区字段，必须包含在主键字段内<br/>在对表进行分区时，如果分区字段没有包含在主键字段内，如表A的主键为ID,分区字段为createtime ，按时间范围分区，代码如下： <br/><br/>CREATE TABLE T1 (<br/>&nbsp;&nbsp;&nbsp;&nbsp; id int(8) NOT NULL AUTO_INCREMENT,<br/>&nbsp;&nbsp;&nbsp;&nbsp; createtime datetime NOT NULL,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRIMARY KEY (id)<br/>) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8<br/>PARTITION BY RANGE(TO_DAYS (createtime))<br/>(<br/>PARTITION p0 VALUES LESS THAN (TO_DAYS(&#039;2010-04-15&#039;)),<br/>PARTITION p1 VALUES LESS THAN (TO_DAYS(&#039;2010-05-01&#039;)),<br/>PARTITION p2 VALUES LESS THAN (TO_DAYS(&#039;2010-05-15&#039;)),<br/>PARTITION p3 VALUES LESS THAN (TO_DAYS(&#039;2010-05-31&#039;)),<br/>PARTITION p4 VALUES LESS THAN (TO_DAYS(&#039;2010-06-15&#039;)),<br/>PARTITION p19 VALUES LESS ThAN&nbsp;&nbsp;MAXVALUE);<br/>复制代码<br/>错误提示：#1503<br/><br/>MySQL主键的限制，每一个分区表中的公式中的列，必须在主键/unique key 中包括，在MYSQL的官方文档里是这么说明的<br/>18.5.1. Partitioning Keys, Primary Keys, and Unique Keys<br/>This section discusses the relationship of partitioning keys with primary keys and unique keys. The rule governing this relationship can be expressed as follows: All columns used in the partitioning expression for a partitioned table must be part of every unique key that the table may have. <br/> <br/>In other words,every unique key on the table must use every columnin the table&#039;s partitioning expression. (This also includes the table&#039;s primary key, since it is by definition a unique key. This particular case is discussed later in this section.) For example, each of the following table creation statements is invalid: <br/>分区字段必须包含在主键字段内，至于为什么MYSQL会这样考虑，我觉得是这样的：为了确保主键的效率。否则同一主键区的东西一个在Ａ分区，一个在Ｂ分区，显然会比较麻烦。<br/> <br/>下面讨论解决办法，毕竟在一张表里，日期做主键的还是不常见。 <br/>方法1： <br/>顺应MYSQL的要求，就把分区字段加入到主键中，组成复合主键<br/>CREATE TABLE T1 (<br/>&nbsp;&nbsp;&nbsp;&nbsp; id int(8) NOT NULL AUTO_INCREMENT,<br/>&nbsp;&nbsp;&nbsp;&nbsp; createtime datetime NOT NULL,<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PRIMARY KEY (id,createtime)<br/>) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8<br/>PARTITION BY RANGE(TO_DAYS (createtime))<br/>(<br/>PARTITION p0 VALUES LESS THAN (TO_DAYS(&#039;2010-04-15&#039;)),<br/>PARTITION p1 VALUES LESS THAN (TO_DAYS(&#039;2010-05-01&#039;)),<br/>PARTITION p2 VALUES LESS THAN (TO_DAYS(&#039;2010-05-15&#039;)),<br/>PARTITION p3 VALUES LESS THAN (TO_DAYS(&#039;2010-05-31&#039;)),<br/>PARTITION p4 VALUES LESS THAN (TO_DAYS(&#039;2010-06-15&#039;)),<br/>PARTITION p19 VALUES LESS ThAN&nbsp;&nbsp;MAXVALUE);<br/> 测试通过，分区成功。<br/> <br/>方法2： <br/>既然MYSQL要把分区字段包含在主键内才能创建分区，那么在创建表的时候，先不指定主键字段，是否可以呢？？<br/>测试如下：<br/>CREATE TABLE T1 (<br/>&nbsp;&nbsp;&nbsp;&nbsp; id int(8) NOT NULL ,<br/>&nbsp;&nbsp;&nbsp;&nbsp; createtime datetime NOT NULL<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8<br/>PARTITION BY RANGE(TO_DAYS (createtime))<br/>(<br/>PARTITION p0 VALUES LESS THAN (TO_DAYS(&#039;2010-04-15&#039;)),<br/>PARTITION p1 VALUES LESS THAN (TO_DAYS(&#039;2010-05-01&#039;)),<br/>PARTITION p2 VALUES LESS THAN (TO_DAYS(&#039;2010-05-15&#039;)),<br/>PARTITION p3 VALUES LESS THAN (TO_DAYS(&#039;2010-05-31&#039;)),<br/>PARTITION p4 VALUES LESS THAN (TO_DAYS(&#039;2010-06-15&#039;)),<br/>PARTITION p19 VALUES LESS ThAN&nbsp;&nbsp;MAXVALUE);<br/>测试通过，分区成功。OK<br/>继续添加上主键<br/>alter table t1 add PRIMARY KEY(ID)<br/>错误1503，和前面一样的错误。<br/>alter table t1 add PRIMARY KEY(ID,createtime)<br/>创建主键成功，但还是复合主键，看来是没办法了，必须听指挥了。<br/>主键创建成功，把ID加上自增字段设置<br/>alter table t1 change id id int not null auto_increment;<br/>alter table t1 auto_increment=1;<br/> <br/>最后结论，MYSQL的分区字段，必须包含在主键字段内。 <br/> <br/>分区表中创建唯一索引：<br/>例如，按create_time进行月分区的表里，唯一索引可能是orderNo，按照上面的要求，唯一索引就成为(order_no,create_time)了。但这样不满足业务需求。<br/>解决办法：<br/>为分区表增加一个before insert触发器，在插入前查询下是否已存在即可。<br/><br/>CREATE TRIGGER `trig_insert_t_order` BEFORE INSERT ON `t_order` FOR EACH ROW BEGIN<br/>&nbsp;&nbsp;DECLARE v_count TINYINT UNSIGNED;<br/>&nbsp;&nbsp;DECLARE v_mess_str varchar(100);<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;SELECT COUNT(1) INTO @v_count<br/>&nbsp;&nbsp;FROM t_order<br/>&nbsp;&nbsp;WHERE order_no = new.order_no<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;and new.create_time&gt;=date_sub(SYSDATE(),INTERVAL 2 DAY);&nbsp;&nbsp;## 2天是否足够<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;IF (@v_count &gt; 0) THEN<br/>&nbsp;&nbsp;&nbsp;&nbsp; SELECT concat(&#039;Duplicate entry &#039;,new.order_no) INTO @v_mess_str;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SIGNAL SQLSTATE &#039;23000&#039; SET MESSAGE_TEXT = @v_mess_str, MYSQL_ERRNO = 1022;<br/>&nbsp;&nbsp;END IF;<br/>&nbsp;&nbsp;<br/>END;
]]>
</description>
</item><item>
<link>https://www.heckjj.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>https://www.heckjj.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>