当前位置:   article > 正文

mysql分区管理

mysql分区管理
  1. --在mysql5.6之后查看分区采用
  2. show plugins;
  3. --不论创建何种类型的分区,如果表中存在主键或唯一索引时,分区列必须是唯一索引的一个组成部分
  4. mysql> create table t1(id int not null,id2 int not null,unique key(id)) partition by hash(id2) partitions 4;
  5. ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table is partitioning function
  6. --innodb与分区表不兼容
  7. Partitioned InnoDB tables cannot have foreign key references, nor can they have columns referenced by foreign keys.
  8. InnoDB tables which have or which are referenced by foreign keys cannot be partitioned
  9. mysql> alter table t2 add foreign key(id) references t1(id);
  10. ERROR 1215 (HY000): Cannot add foreign key constraint
  11. --查看分区表
  12. select * from information_schema.partitions p where p.partition_name is not null
  13. and p.table_name='t1';
  14. --查看某一分区执行计划
  15. mysql> explain partitions select * from t1 where id2=1;
  16. +----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+
  17. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra |
  18. +----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+
  19. | 1 | SIMPLE | t1 | p1 | ALL | NULL | NULL | NULL | NULL | 2 | Using where |
  20. +----+-------------+-------+------------+------+---------------+------+---------+------+------+-------------+
  21. --查看某一分区内数据,如果有多个分区要查时,请使用分号隔开
  22. mysql> select * from t1 partition(p1);
  23. +----+-----+
  24. | id | id2 |
  25. +----+-----+
  26. | 4 | 5 |
  27. | 8 | 9 |
  28. +----+-----+
  29. --mysql不支持在日期类型上直接创建分区,必需借助于函数
  30. CREATE TABLE employees (
  31. id INT NOT NULL,
  32. fname VARCHAR(30),
  33. lname VARCHAR(30),
  34. hired DATE NOT NULL DEFAULT '1970-01-01',
  35. separated DATE NOT NULL DEFAULT '9999-12-31',
  36. job_code INT,
  37. store_id INT
  38. )
  39. PARTITION BY RANGE ( YEAR(separated) ) (
  40. PARTITION p0 VALUES LESS THAN (1991),
  41. PARTITION p1 VALUES LESS THAN (1996),
  42. PARTITION p2 VALUES LESS THAN (2001),
  43. PARTITION p3 VALUES LESS THAN MAXVALUE
  44. );
  45. --按月进行分区,还是利用的函数
  46. CREATE TABLE t (
  47. id INT NOT NULL,
  48. fired_date DATE NOT NULL DEFAULT '1970-01-01'
  49. )
  50. PARTITION BY RANGE ( extract(YEAR_MONTH from fired_date) ) (
  51. PARTITION p0 VALUES LESS THAN (201601),
  52. PARTITION p1 VALUES LESS THAN (201602),
  53. PARTITION p2 VALUES LESS THAN (201603),
  54. PARTITION p3 VALUES LESS THAN MAXVALUE
  55. );
  56. --通过使用RANGE COLUMNS而不再需要使用函数
  57. The use of partitioning columns employing date or time types other than DATE or DATETIME is not supported with RANGE COLUMNS
  58. CREATE TABLE members (
  59. firstname VARCHAR(25) NOT NULL,
  60. lastname VARCHAR(25) NOT NULL,
  61. username VARCHAR(16) NOT NULL,
  62. email VARCHAR(35),
  63. joined DATE
  64. )
  65. PARTITION BY RANGE COLUMNS(joined) (
  66. PARTITION p0 VALUES LESS THAN ('1960-01-01'),
  67. PARTITION p1 VALUES LESS THAN ('1970-01-01'),
  68. PARTITION p2 VALUES LESS THAN ('1980-01-01'),
  69. PARTITION p3 VALUES LESS THAN ('1990-01-01'),
  70. PARTITION p4 VALUES LESS THAN MAXVALUE
  71. );
  72. --对于range partition的null,mysql默认把它当作最小的值看待,如果列值为空,其会把它插入到第一个分区中
  73. MariaDB [test]> insert into members(firstname,lastname,username) values ('rudy','gao','rudy.gao');
  74. Query OK, 1 row affected (0.01 sec)
  75. MariaDB [test]> select * from members;
  76. +-----------+----------+----------+-------+--------+
  77. | firstname | lastname | username | email | joined |
  78. +-----------+----------+----------+-------+--------+
  79. | rudy | gao | rudy.gao | NULL | NULL |
  80. +-----------+----------+----------+-------+--------+
  81. 1 row in set (0.00 sec)
  82. --在第一个分区中查询
  83. MariaDB [test]> select * from members partition(p0);
  84. +-----------+----------+----------+-------+--------+
  85. | firstname | lastname | username | email | joined |
  86. +-----------+----------+----------+-------+--------+
  87. | rudy | gao | rudy.gao | NULL | NULL |
  88. +-----------+----------+----------+-------+--------+
  89. --对于list partition的null,如果没有指定一个list存储null时,其是不允许插入null值的
  90. --A table that is partitioned by LIST admits NULL values if and only if one of its partitions is defined using that value-list that contains NULL.
  91. --The converse of this is that a table partitioned by LIST which does not explicitly use NULL in a value list rejects rows resulting in a NULL value for the partitioning expression
  92. mysql> INSERT INTO ts1 VALUES (NULL, 'mothra');
  93. ERROR 1504 (HY000): Table has no partition for value NULL
  94. --对于hash或者key分区,null值被当做0处理
  95. --NULL is handled somewhat differently for tables partitioned by HASH or KEY.
  96. --In these cases, any partition expression that yields a NULL value is treated as though its return value were zero
  97. --注意对于RANGE COLUMNS其不能是表达式,但其可以接受多个列
  98. ? RANGE COLUMNS does not accept expressions, only names of columns.
  99. ? RANGE COLUMNS accepts a list of one or more columns
  100. --如果RANGE COLUMNS有多列时,其必须满足所有列都符合相应条件时,才放入相对应的分区,否则会默认分区
  101. CREATE TABLE rc1 (a INT,b INT)
  102. PARTITION BY RANGE COLUMNS(a, b) (
  103. PARTITION p0 VALUES LESS THAN (5, 12),
  104. PARTITION p3 VALUES LESS THAN (MAXVALUE, MAXVALUE)
  105. );
  106. mysql> INSERT INTO rc1 VALUES (5,10), (5,11), (5,12);
  107. --查看各个分区表中行数
  108. mysql> SELECT PARTITION_NAME,TABLE_ROWS FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'rc1';
  109. +----------------+------------+
  110. | PARTITION_NAME | TABLE_ROWS |
  111. +----------------+------------+
  112. | p0 | 2 |
  113. | p3 | 1 |
  114. +----------------+------------+
  115. --其类似于如下的sql比较
  116. mysql> SELECT (5,10) < (5,12), (5,11) < (5,12), (5,12) < (5,12);
  117. --创建以key为分区的表,需要一个主键,如果没有,则mysql使用默认的虚拟主键
  118. --KEY takes only a list of zero or more column names. Any columns used as the partitioning key must comprise part or all of the table's primary key,
  119. --if the table has one. Where no column name is specified as the partitioning key, the table's primary key is used, if there is one
  120. CREATE TABLE k1 (
  121. id INT NOT NULL PRIMARY KEY,
  122. name VARCHAR(20)
  123. )
  124. PARTITION BY KEY()
  125. PARTITIONS 2;
  126. --对于已经创建的分区,最好不要改变它们的sql_mode
  127. it is strongly recommended that you never change the server SQL mode after creating partitioned tables
  128. Sometimes a change in the server SQL mode can make partitioned tables unusable
  129. Differing SQL modes on master and slave can lead to partitioning expressions being evaluated differently

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/623832
推荐阅读
相关标签
  

闽ICP备14008679号