Mysql分區表的治理與保護。本站提示廣大學習愛好者:(Mysql分區表的治理與保護)文章只能為提供參考,不一定能成為您想要的結果。以下是Mysql分區表的治理與保護正文
轉變一個表的分區計劃只需應用alter table 加 partition_options 子句便可以了。和創立分區表時的create table語句很像。
創立表
CREATE TABLE trb3 (id INT, name VARCHAR(50), purchased DATE) PARTITION BY RANGE( YEAR(purchased) ) ( PARTITION p0 VALUES LESS THAN (1990), PARTITION p1 VALUES LESS THAN (1995), PARTITION p2 VALUES LESS THAN (2000), PARTITION p3 VALUES LESS THAN (2005) );
創立拔出數據存儲進程
delimiter $$ drop procedure if exists pr_trb3$$ create procedure pr_trb3(in begindate date,in enddate date,in tabname varchar(40)) begin while begindate<enddate 1="" begindate="date_add(begindate,interval" delimiter="" do="" drop="" end="" execute="" from="" insert="" pre="" prepare="" s="concat_ws('" set="" stmt=""><p>挪用存儲進程拔出數據</p><pre class="brush:sql;">call pr_trb3('1985-01-01','2004-12-31','trb3');</pre> <p>檢查數據散布</p> <pre class="brush:sql;">select partition_name part, partition_expression expr, partition_description descr, table_rows from information_schema.partitions where table_schema = schema() and table_name='trb3'; +------+------------------+-------+------------+ | part | expr | descr | table_rows | +------+------------------+-------+------------+ | p0 | YEAR(purchased) | 1990 | 1826 | | p1 | YEAR(purchased) | 1995 | 1826 | | p2 | YEAR(purchased) | 2000 | 1826 | | p3 | YEAR(purchased) | 2005 | 1826 | +------+------------------+-------+------------+ 4 rows in set (0.00 sec)</pre> <p>轉變分區計劃</p> <pre class="brush:sql;">mysql> ALTER TABLE trb3 PARTITION BY KEY(id) PARTITIONS 4; Query OK, 7304 rows affected (0.07 sec) Records: 7304 Duplicates: 0 Warnings: 0</pre> <p>檢查數據</p> <pre class="brush:sql;">select partition_name part, partition_expression expr, partition_description descr, table_rows from information_schema.partitions where table_schema = schema() and table_name='trb3'; +------+------+-------+------------+ | part | expr | descr | table_rows | +------+------+-------+------------+ | p0 | `id` | NULL | 7472 | | p1 | `id` | NULL | 0 | | p2 | `id` | NULL | 0 | | p3 | `id` | NULL | 0 | +------+------+-------+------------+ 4 rows in set (0.00 sec) mysql> select 1826*4; +--------+ | 1826*4 | +--------+ | 7304 | +--------+ 1 row in set (0.00 sec)</pre> <p>count(*)行數分歧,解釋數據沒出成績,然則information_schema.partitions查出來的纰謬<del>,這就不曉得為何了</del></p> <blockquote> <p>For partitioned InnoDB tables, the row count given in the TABLE_ROWS column of the INFORMATION_SCHEMA.PARTITIONS table is only an estimated value used in SQL optimization, and is not always exact.</p> </blockquote> <pre class="brush:sql;">mysql> select count(*) from trb3; +----------+ | count(*) | +----------+ | 7304 | +----------+ 然則count(*)照樣7304,甚麼鬼</pre> <p>再次轉變分區計劃</p> <pre class="brush:sql;">ALTER TABLE trb3 PARTITION BY RANGE( YEAR(purchased) ) ( PARTITION p0 VALUES LESS THAN (1990), PARTITION p1 VALUES LESS THAN (1995), PARTITION p2 VALUES LESS THAN (2000), PARTITION p3 VALUES LESS THAN (2005) ); mysql> select -> partition_name part, -> partition_expression expr, -> partition_description descr, -> table_rows -> from information_schema.partitions where -> table_schema = schema() -> and table_name='trb3'; +------+------------------+-------+------------+ | part | expr | descr | table_rows | +------+------------------+-------+------------+ | p0 | YEAR(purchased) | 1990 | 1826 | | p1 | YEAR(purchased) | 1995 | 1826 | | p2 | YEAR(purchased) | 2000 | 0 | | p3 | YEAR(purchased) | 2005 | 0 | +------+------------------+-------+------------+ 4 rows in set (0.00 sec)</pre> <p><del>丟數據了。。</del><br> 更正,現實沒丟,這個information_shcema.partitions表有延遲,過一會再查就行了</p> <pre class="brush:sql;">mysql> select -> partition_name part, -> partition_expression expr, -> partition_description descr, -> table_rows -> from information_schema.partitions where -> table_schema = schema() -> and table_name='trb3'; +------+------------------+-------+------------+ | part | expr | descr | table_rows | +------+------------------+-------+------------+ | p0 | YEAR(purchased) | 1990 | 1826 | | p1 | YEAR(purchased) | 1995 | 1826 | | p2 | YEAR(purchased) | 2000 | 1826 | | p3 | YEAR(purchased) | 2005 | 1826 | +------+------------------+-------+------------+ 4 rows in set (0.00 sec)</pre> <p>官方文檔說:<br> This has the same effect on the structure of the table as dropping the table and re-creating it using CREATE TABLE trb3 PARTITION BY KEY(id) PARTITIONS 2;<br> 就是說ALTER TABLE trb3 PARTITION BY與 drop table然後從新create table trb3 partition by key(id) partitions 2一樣呢。</p> <h3 id="改存儲引擎和通俗表沒啥差別">改存儲引擎,和通俗表沒啥差別</h3> <pre class="brush:sql;">mysql> drop table trb3; Query OK, 0 rows affected (0.01 sec) mysql> CREATE TABLE trb3 (id INT, name VARCHAR(50), purchased DATE) -> PARTITION BY RANGE( YEAR(purchased) ) ( -> PARTITION p0 VALUES LESS THAN (1990), -> PARTITION p1 VALUES LESS THAN (1995), -> PARTITION p2 VALUES LESS THAN (2000), -> PARTITION p3 VALUES LESS THAN (2005) -> ); Query OK, 0 rows affected (0.03 sec) mysql> call pr_trb3('1985-01-01','2004-12-31','trb3'); Query OK, 0 rows affected (1.69 sec) mysql> select -> partition_name part, -> partition_expression expr, -> partition_description descr, -> table_rows -> from information_schema.partitions where -> table_schema = schema() -> and table_name='trb3'; +------+------------------+-------+------------+ | part | expr | descr | table_rows | +------+------------------+-------+------------+ | p0 | YEAR(purchased) | 1990 | 1826 | | p1 | YEAR(purchased) | 1995 | 1826 | | p2 | YEAR(purchased) | 2000 | 1826 | | p3 | YEAR(purchased) | 2005 | 1826 | +------+------------------+-------+------------+ 4 rows in set (0.01 sec) mysql> alter table trb3 engine=myisam; Query OK, 7304 rows affected (0.02 sec) Records: 7304 Duplicates: 0 Warnings: 0 mysql> select -> partition_name part, -> partition_expression expr, -> partition_description descr, -> table_rows -> from information_schema.partitions where -> table_schema = schema() -> and table_name='trb3'; +------+------------------+-------+------------+ | part | expr | descr | table_rows | +------+------------------+-------+------------+ | p0 | YEAR(purchased) | 1990 | 1826 | | p1 | YEAR(purchased) | 1995 | 1826 | | p2 | YEAR(purchased) | 2000 | 1826 | | p3 | YEAR(purchased) | 2005 | 1826 | +------+------------------+-------+------------+ 4 rows in set (0.01 sec) mysql> show create table trb3\G *************************** 1. row *************************** Table: trb3 Create Table: CREATE TABLE `trb3` ( `id` int(11) DEFAULT NULL, `name` varchar(50) DEFAULT NULL, `purchased` date DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 /*!50100 PARTITION BY RANGE ( YEAR(purchased)) (PARTITION p0 VALUES LESS THAN (1990) ENGINE = MyISAM, PARTITION p1 VALUES LESS THAN (1995) ENGINE = MyISAM, PARTITION p2 VALUES LESS THAN (2000) ENGINE = MyISAM, PARTITION p3 VALUES LESS THAN (2005) ENGINE = MyISAM) */ 1 row in set (0.00 sec)</pre> <h3 id="將表由分區表改成非分區表">將表由分區表改成非分區表</h3> <pre class="brush:sql;">mysql> alter table trb3 remove partitioning; Query OK, 7304 rows affected (0.01 sec) Records: 7304 Duplicates: 0 Warnings: 0 mysql> select -> partition_name part, -> partition_expression expr, -> partition_description descr, -> table_rows -> from information_schema.partitions where -> table_schema = schema() -> and table_name='trb3'; +------+------+-------+------------+ | part | expr | descr | table_rows | +------+------+-------+------------+ | NULL | NULL | NULL | 7304 | +------+------+-------+------------+ 1 row in set (0.00 sec) mysql> show create table trb3\G *************************** 1. row *************************** Table: trb3 Create Table: CREATE TABLE `trb3` ( `id` int(11) DEFAULT NULL, `name` varchar(50) DEFAULT NULL, `purchased` date DEFAULT NULL ) ENGINE=MyISAM DEFAULT CHARSET=utf8 1 row in set (0.00 sec)</pre> <h3 id="range-list分區治理">Range List分區治理</h3> <pre class="brush:sql;">mysql> drop table trb3; Query OK, 0 rows affected (0.01 sec) mysql> CREATE TABLE trb3 (id INT, name VARCHAR(50), purchased DATE) -> PARTITION BY RANGE( YEAR(purchased) ) ( -> PARTITION p0 VALUES LESS THAN (1990), -> PARTITION p1 VALUES LESS THAN (1995), -> PARTITION p2 VALUES LESS THAN (2000), -> PARTITION p3 VALUES LESS THAN (2005) -> ); Query OK, 0 rows affected (0.03 sec) mysql> call pr_trb3('1985-01-01','2004-12-31','trb3'); Query OK, 0 rows affected (1.75 sec) mysql> select -> partition_name part, -> partition_expression expr, -> partition_description descr, -> table_rows -> from information_schema.partitions where -> table_schema = schema() -> and table_name='trb3'; +------+------------------+-------+------------+ | part | expr | descr | table_rows | +------+------------------+-------+------------+ | p0 | YEAR(purchased) | 1990 | 1826 | | p1 | YEAR(purchased) | 1995 | 1826 | | p2 | YEAR(purchased) | 2000 | 1826 | | p3 | YEAR(purchased) | 2005 | 1826 | +------+------------------+-------+------------+ 4 rows in set (0.00 sec)</pre> <h4 id="增長分區">增長分區</h4> <pre class="brush:sql;">mysql> alter table trb3 add partition (partition p5 values less than(2010)); Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0</pre> <h4 id="歸並分區">歸並分區</h4> <pre class="brush:sql;">mysql> alter table trb3 reorganize partition p3,p5 into(partition p5 values less than(2010)); Query OK, 1826 rows affected (0.03 sec) Records: 1826 Duplicates: 0 Warnings: 0 mysql> select -> partition_name part, -> partition_expression expr, -> partition_description descr, -> table_rows -> from information_schema.partitions where -> table_schema = schema() -> and table_name='trb3'; +------+------------------+-------+------------+ | part | expr | descr | table_rows | +------+------------------+-------+------------+ | p0 | YEAR(purchased) | 1990 | 1826 | | p1 | YEAR(purchased) | 1995 | 1826 | | p2 | YEAR(purchased) | 2000 | 1826 | | p5 | YEAR(purchased) | 2010 | 1826 | +------+------------------+-------+------------+ 4 rows in set (0.00 sec)</pre> <h4 id="決裂分區">決裂分區</h4> <pre class="brush:sql;">mysql> ALTER TABLE trb3 REORGANIZE PARTITION p5 INTO ( -> PARTITION p3 VALUES LESS THAN (2005), -> PARTITION p4 VALUES LESS THAN (2010) -> ); Query OK, 1826 rows affected (0.04 sec) Records: 1826 Duplicates: 0 Warnings: 0 select partition_name part, partition_expression expr, partition_description descr, table_rows from information_schema.partitions where table_schema = schema() and table_name='trb3'; +------+------------------+-------+------------+ | part | expr | descr | table_rows | +------+------------------+-------+------------+ | p0 | YEAR(purchased) | 1990 | 1826 | | p1 | YEAR(purchased) | 1995 | 1826 | | p2 | YEAR(purchased) | 2000 | 1826 | | p3 | YEAR(purchased) | 2005 | 1826 | | p4 | YEAR(purchased) | 2010 | 0 | +------+------------------+-------+------------+ 5 rows in set (0.00 sec)</pre> <h3 id="hash-key分區">HASH KEY分區</h3> <pre class="brush:sql;">CREATE TABLE trb3 (id INT, name VARCHAR(50), purchased DATE) PARTITION BY hash( YEAR(purchased) ) partitions 12; mysql>call pr_trb3('1985-01-01','2004-12-31','trb3'); select partition_name part, partition_expression expr, partition_description descr, table_rows from information_schema.partitions where table_schema = schema() and table_name='trb3'; +------+------------------+-------+------------+ | part | expr | descr | table_rows | +------+------------------+-------+------------+ | p0 | YEAR(purchased) | NULL | 731 | | p1 | YEAR(purchased) | NULL | 365 | | p2 | YEAR(purchased) | NULL | 365 | | p3 | YEAR(purchased) | NULL | 365 | | p4 | YEAR(purchased) | NULL | 366 | | p5 | YEAR(purchased) | NULL | 730 | | p6 | YEAR(purchased) | NULL | 730 | | p7 | YEAR(purchased) | NULL | 730 | | p8 | YEAR(purchased) | NULL | 732 | | p9 | YEAR(purchased) | NULL | 730 | | p10 | YEAR(purchased) | NULL | 730 | | p11 | YEAR(purchased) | NULL | 730 | +------+------------------+-------+------------+ 12 rows in set (0.00 sec)</pre> <h4 id="縮建分區從12個到8個">縮建分區從12個到8個</h4> <pre class="brush:sql;">mysql> ALTER TABLE trb3 COALESCE PARTITION 4; Query OK, 7304 rows affected (0.13 sec) Records: 7304 Duplicates: 0 Warnings: 0 select partition_name part, partition_expression expr, partition_description descr, table_rows from information_schema.partitions where table_schema = schema() and table_name='trb3'; +------+------------------+-------+------------+ | part | expr | descr | table_rows | +------+------------------+-------+------------+ | p0 | YEAR(purchased) | NULL | 732 | | p1 | YEAR(purchased) | NULL | 1095 | | p2 | YEAR(purchased) | NULL | 1095 | | p3 | YEAR(purchased) | NULL | 1095 | | p4 | YEAR(purchased) | NULL | 1097 | | p5 | YEAR(purchased) | NULL | 730 | | p6 | YEAR(purchased) | NULL | 730 | | p7 | YEAR(purchased) | NULL | 730 | +------+------------------+-------+------------+ 8 rows in set (0.00 sec) mysql> select count(*) from trb3; +----------+ | count(*) | +----------+ | 7304 | +----------+ 1 row in set (0.00 sec)</pre> <p>沒丟數據</p> <p>壓縮前2004年在P0</p> <pre class="brush:sql;">mysql> select mod(2004,12); +--------------+ | mod(2004,12) | +--------------+ | 0 | +--------------+</pre> <p>壓縮後2004年在P4</p> <pre class="brush:sql;">mysql> select mod(2004,8); +-------------+ | mod(2004,8) | +-------------+ | 4 | +-------------+</pre> <h3 id="exchanging-partitions-and-subpartitions-with-tables">Exchanging Partitions and Subpartitions with Tables</h3> <h3 id="分區子分區交流">分區(子分區)交流</h3> <pre class="brush:sql;"> ALTER TABLE pt EXCHANGE PARTITION p WITH TABLE nt</pre> <p>pt是一個分區表,p是pt的分區或子分區,而nt是一個非分區表</p> <h4 id="限制前提">限制前提:</h4> <p>1.表nt不是分區表<br> 2.表nt不是暫時表<br> 3.表pt和nt構造在其他方面是雷同的<br> 4.表n沒有外鍵束縛,也沒有其他表援用它的列為外鍵<br> 5.表nt的一切行都包括在表p的分區規模內(好比p range分區最年夜values less than 10,那末表nt不克不及有年夜於等於10的值)</p> <h4 id="權限">權限:</h4> <p>除 ALTER, INSERT, and CREATE 權限外,你還要有DROP權限能力履行ALTER TABLE … EXCHANGE PARTITION.</p> <h4 id="其他留意事項">其他留意事項:</h4> <p>1.履行ALTER TABLE … EXCHANGE PARTITION 不會挪用任安在nt表和p表上的觸發器<br> 2.在交流表中的任何AUTO_INCREMENT列會被reset<br> 3.IGNORE症結字在履行ALTER TABLE … EXCHANGE PARTITION時會掉效</p> <h4 id="完全實例語句以下">完全實例語句以下:</h4> <pre class="brush:sql;">ALTER TABLE pt EXCHANGE PARTITION p WITH TABLE nt;</pre> <p>在一次ALTER TABLE EXCHANGE PARTITION 中,只能有一個分區和一個非分區表被交流<br> 想交流多個,就履行屢次ALTER TABLE EXCHANGE PARTITION<br> 任何MySQL支撐的分區類型都可以停止交流</p> <h4 id="交流實例">交流實例</h4> <pre class="brush:sql;">CREATE TABLE e ( id INT NOT NULL, fname VARCHAR(30), lname VARCHAR(30) ) PARTITION BY RANGE (id) ( PARTITION p0 VALUES LESS THAN (50), PARTITION p1 VALUES LESS THAN (100), PARTITION p2 VALUES LESS THAN (150), PARTITION p3 VALUES LESS THAN (MAXVALUE) ); INSERT INTO e VALUES (1669, "Jim", "Smith"), (337, "Mary", "Jones"), (16, "Frank", "White"), (2005, "Linda", "Black");</pre> <p>創立一個與e構造一樣的非分區表e2</p> <pre class="brush:sql;">mysql> create table e2 like e; Query OK, 0 rows affected (0.01 sec) mysql> show create table e2\G *************************** 1. row *************************** Table: e2 Create Table: CREATE TABLE `e2` ( `id` int(11) NOT NULL, `fname` varchar(30) DEFAULT NULL, `lname` varchar(30) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 /*!50100 PARTITION BY RANGE (id) (PARTITION p0 VALUES LESS THAN (50) ENGINE = InnoDB, PARTITION p1 VALUES LESS THAN (100) ENGINE = InnoDB, PARTITION p2 VALUES LESS THAN (150) ENGINE = InnoDB, PARTITION p3 VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */ 1 row in set (0.00 sec) mysql> alter table e2 remove partitioning; Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table e2\G *************************** 1. row *************************** Table: e2 Create Table: CREATE TABLE `e2` ( `id` int(11) NOT NULL, `fname` varchar(30) DEFAULT NULL, `lname` varchar(30) DEFAULT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8 1 row in set (0.00 sec)</pre> <p>檢查數據在e表中的散布:</p> <pre class="brush:sql;">select partition_name part, partition_expression expr, partition_description descr, table_rows from information_schema.partitions where table_schema = schema() and table_name='e' +------+------+----------+------------+ | part | expr | descr | table_rows | +------+------+----------+------------+ | p0 | id | 50 | 1 | | p1 | id | 100 | 0 | | p2 | id | 150 | 0 | | p3 | id | MAXVALUE | 3 | +------+------+----------+------------+ 4 rows in set (0.00 sec)</pre> <p>將分區p0與e2表停止交流:</p> <pre class="brush:sql;">mysql> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2; Query OK, 0 rows affected (0.01 sec) select partition_name part, partition_expression expr, partition_description descr, table_rows from information_schema.partitions where table_schema = schema() and table_name='e'; +------+------+----------+------------+ | part | expr | descr | table_rows | +------+------+----------+------------+ | p0 | id | 50 | 0 | | p1 | id | 100 | 0 | | p2 | id | 150 | 0 | | p3 | id | MAXVALUE | 3 | +------+------+----------+------------+ 4 rows in set (0.01 sec) mysql> select * from e2; +----+-------+-------+ | id | fname | lname | +----+-------+-------+ | 16 | Frank | White | +----+-------+-------+ 1 row in set (0.00 sec) </pre> <p>重做試驗,此次在交流前在表e2中拔出一些數據</p> <pre class="brush:sql;">mysql> insert into e2 values(16,'FAN','BOSHI'); Query OK, 1 row affected (0.00 sec) mysql> insert into e2 values(51,'DU','YALAN'); Query OK, 1 row affected (0.00 sec) mysql> select * from e2; +----+-------+-------+ | id | fname | lname | +----+-------+-------+ | 16 | FAN | BOSHI | | 51 | DU | YALAN | +----+-------+-------+ 2 rows in set (0.00 sec) mysql> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2; ERROR 1737 (HY000): Found a row that does not match the partition</pre> <p>報錯了,由於51超越了p0的規模。<br> 如之前所說,此時應用IGNORE也杯水車薪</p> <pre class="brush:sql;">mysql> ALTER IGNORE TABLE e EXCHANGE PARTITION p0 WITH TABLE e2; ERROR 1737 (HY000): Found a row that does not match the partition</pre> <p>修正id為49,如許就屬於p0的規模了</p> <pre class="brush:sql;">mysql> update e2 set id=49 where id=51; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> ALTER TABLE e EXCHANGE PARTITION p0 WITH TABLE e2; Query OK, 0 rows affected (0.01 sec) mysql> select -> partition_name part, -> partition_expression expr, -> partition_description descr, -> table_rows -> from information_schema.partitions where -> table_schema = schema() -> and table_name='e'; +------+------+----------+------------+ | part | expr | descr | table_rows | +------+------+----------+------------+ | p0 | id | 50 | 2 | | p1 | id | 100 | 0 | | p2 | id | 150 | 0 | | p3 | id | MAXVALUE | 3 | +------+------+----------+------------+ 4 rows in set (0.00 sec) e2的數據被交流到了p0中 mysql> select * from e partition(p0); +----+-------+-------+ | id | fname | lname | +----+-------+-------+ | 16 | FAN | BOSHI | | 49 | DU | YALAN | +----+-------+-------+ 2 rows in set (0.00 sec) e的p0分區中的數據被交流到了e2中 mysql> select * from e2; +----+-------+-------+ | id | fname | lname | +----+-------+-------+ | 16 | Frank | White | +----+-------+-------+ 1 row in set (0.01 sec)</pre> <h4 id="交流subpartition">交流subpartition</h4> <pre class="brush:sql;">CREATE TABLE es ( id INT NOT NULL, fname VARCHAR(30), lname VARCHAR(30) ) PARTITION BY RANGE (id) SUBPARTITION BY KEY (lname) SUBPARTITIONS 2 ( PARTITION p0 VALUES LESS THAN (50), PARTITION p1 VALUES LESS THAN (100), PARTITION p2 VALUES LESS THAN (150), PARTITION p3 VALUES LESS THAN (MAXVALUE) ); INSERT INTO es VALUES (1669, "Jim", "Smith"), (337, "Mary", "Jones"), (16, "Frank", "White"), (2005, "Linda", "Black"); CREATE TABLE es2 LIKE es; ALTER TABLE es2 REMOVE PARTITIONING;</pre> <p>雖然我們沒有顯示的指定每一個子分區的名字,我們仍可以經由過程information_schema.partitions表獲得到子分區的名字</p> <pre class="brush:sql;">select partition_name part, subpartition_name, partition_expression expr, partition_description descr, table_rows from information_schema.partitions where table_schema = schema() and table_name='es'; +------+-------------------+------+----------+------------+ | part | subpartition_name | expr | descr | table_rows | +------+-------------------+------+----------+------------+ | p0 | p0sp0 | id | 50 | 1 | | p0 | p0sp1 | id | 50 | 0 | | p1 | p1sp0 | id | 100 | 0 | | p1 | p1sp1 | id | 100 | 0 | | p2 | p2sp0 | id | 150 | 0 | | p2 | p2sp1 | id | 150 | 0 | | p3 | p3sp0 | id | MAXVALUE | 3 | | p3 | p3sp1 | id | MAXVALUE | 0 | +------+-------------------+------+----------+------------+</pre> <p>接上去,開端將p3sp0和es停止交流</p> <pre class="brush:sql;">mysql> select * from es partition(p3sp0); +------+-------+-------+ | id | fname | lname | +------+-------+-------+ | 1669 | Jim | Smith | | 337 | Mary | Jones | | 2005 | Linda | Black | +------+-------+-------+ 3 rows in set (0.00 sec) mysql> ALTER TABLE es EXCHANGE PARTITION p3sp0 WITH TABLE es2; Query OK, 0 rows affected (0.00 sec) mysql> select * from es partition(p3sp0); Empty set (0.00 sec) mysql> select * from es2; +------+-------+-------+ | id | fname | lname | +------+-------+-------+ | 1669 | Jim | Smith | | 337 | Mary | Jones | | 2005 | Linda | Black | +------+-------+-------+ 3 rows in set (0.00 sec)</pre> <p>假如一個分區表有子分區,那末你只能以子分區為粒度停止交流,而不克不及直接交流子分區的父分區</p> <pre class="brush:sql;">mysql> ALTER TABLE es EXCHANGE PARTITION p3 WITH TABLE es2; ERROR 1704 (HY000): Subpartitioned table, use subpartition instead of partition</pre> <p>EXCHANGE PARTITION有著嚴厲的請求<br> 兩個將要交流的表的 列名,列的創立次序,列的數目,和索引都要嚴厲分歧。固然存儲引擎也要分歧</p> <pre class="brush:sql;">mysql> desc es2; +-------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------+-------------+------+-----+---------+-------+ | id | int(11) | NO | | NULL | | | fname | varchar(30) | YES | | NULL | | | lname | varchar(30) | YES | | NULL | | +-------+-------------+------+-----+---------+-------+ 3 rows in set (0.00 sec) mysql> create index id_name on es2(id,fname); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> ALTER TABLE es EXCHANGE PARTITION p3sp0 WITH TABLE es2; ERROR 1736 (HY000): Tables have different definitions</pre> <p>轉變es2的存儲引擎</p> <pre class="brush:sql;">mysql> drop index id_name on es2; Query OK, 0 rows affected (0.00 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table es2 engine=myisam; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> ALTER TABLE es EXCHANGE PARTITION p3sp0 WITH TABLE es2; ERROR 1497 (HY000): The mix of handlers in the partitions is not allowed in this version of MySQL</pre> <h3 id="分區表的保護">分區表的保護</h3> <p>CHECK TABLE, OPTIMIZE TABLE, ANALYZE TABLE, and REPAIR TABLE可以被用於保護分區表</p> <p>Rebuilding partitions.相當於將分區中的數據drop失落再拔出回來,關於防止磁盤碎片很有用<br> Example:</p> <pre class="brush:sql;">ALTER TABLE t1 REBUILD PARTITION p0, p1;</pre> <p>Optimizing partitions.假如你的表增長刪除年夜量數據,或許停止了年夜量的邊長列的更新操作( VARCHAR, BLOB, or TEXT columns)。那末optimize partition將收受接管未應用的空間,並整頓分區數據文件。<br> Example:</p> <pre class="brush:sql;">ALTER TABLE t1 OPTIMIZE PARTITION p0, p1;</pre> <p>運轉OPTIMIZE PARTITION 相當於做了 CHECK PARTITION, ANALYZE PARTITION, and REPAIR PARTITION</p> <blockquote> <p>Some MySQL storage engines, including InnoDB, do not support per-partition optimization; in these cases, ALTER TABLE … OPTIMIZE PARTITION rebuilds the entire table. In MySQL 5.6.9 and later, running this statement on such a table causes the entire table to rebuilt and analyzed, and an appropriate warning to be issued. (Bug #11751825, Bug #42822) Use ALTER TABLE … REBUILD PARTITION and ALTER TABLE … ANALYZE PARTITION instead, to avoid this issue.</p> </blockquote> <p>Analyzing partitions.讀取並保留分區的鍵散布<br> Example:</p> <pre class="brush:sql;">ALTER TABLE t1 ANALYZE PARTITION p3;</pre> <p>Repairing partitions.修補被損壞的分區<br> Example:</p> <pre class="brush:sql;">ALTER TABLE t1 REPAIR PARTITION p0,p1;</pre> <p>Checking partitions.可使用簡直與對非分區表應用CHECK TABLE 雷同的方法檢討分區。<br> Example:</p> <pre class="brush:sql;">ALTER TABLE trb3 CHECK PARTITION p1;</pre> <p>這個敕令可以告知你表trb3的分區p1中的數據或索引能否曾經被損壞。假如產生了這類情形,應用“ALTER TABLE … REPAIR PARTITION”來修補該分區。</p> <h4 id="以上每一個敕令都支撐將分區換成all">以上每一個敕令都支撐將分區換成ALL</h4> <blockquote> <p>The use of mysqlcheck and myisamchk is not supported with partitioned tables.</p> </blockquote> <p>mysqlcheck和myisamchk不支撐分區表</p> <p>你可使用 ALTER TABLE … TRUNCATE PARTITION. 來刪除一個或多個分區中的數據<br> 如:ALTER TABLE … TRUNCATE PARTITION ALL刪除一切數據</p> <p>ANALYZE, CHECK, OPTIMIZE, REBUILD, REPAIR, and TRUNCATE 操作不支撐 subpartitions.</p> </enddate>
以上所述是小編給年夜家引見的Mysql分區表的治理與保護,願望對年夜家有所贊助,假如年夜家有任何疑問請給我留言,小編會實時答復年夜家的。在此也異常感激年夜家對網站的支撐!