MySQL分割槽
一,什麼是資料庫分割槽
前段時間寫過一篇關於mysql分表的的文章,下面來說一下什麼是資料庫分割槽,以mysql為例。mysql資料庫中的資料是以檔案的形勢存在磁碟上的,預設放在/mysql/data下面(可以透過my.cnf中的datadir來檢視),一張表主要對應著三個檔案,一個是frm存放表結構的,一個是myd存放表資料的,一個是myi存表索引的。如果一張表的資料量太大的話,那麼myd,myi就會變的很大,查詢資料就會變的很慢,這個時候我們可以利用mysql的分割槽功能,在物理上將這一張表對應的三個檔案,分割成許多個小塊,這樣呢,我們查詢一條資料時,就不用全部查詢了,只要知道這條資料在哪一塊,然後在那一塊找就行了。如果表的資料太大,可能一個磁碟放不下,這個時候,我們可以把資料分配到不同的磁碟裡面去。
分割槽的二種方式
1,橫向分割槽
什麼是橫向分割槽呢?就是橫著來分割槽了,舉例來說明一下,假如有100W條資料,分成十份,前10W條資料放到第一個分割槽,第二個10W條資料放到第二個分割槽,依此類推。也就是把表分成了十分,根用merge來分表,有點像哦。取出一條資料的時候,這條資料包含了表結構中的所有欄位,也就是說橫向分割槽,並沒有改變表的結構。
2,縱向分割槽
什麼是縱向分割槽呢?就是豎來分割槽了,舉例來說明,在設計使用者表的時候,開始的時候沒有考慮好,而把個人的所有資訊都放到了一張表裡面去,這樣這個表裡面就會有比較大的欄位,如個人簡介,而這些簡介呢,也許不會有好多人去看,所以等到有人要看的時候,在去查詢,分表的時候,可以把這樣的大欄位,分開來。
感覺資料庫的分割槽好像是切蘋果,到底是橫著切呢,還是豎著切,根據個人喜好了,mysql提供的分割槽屬於第一種,橫向分割槽,並且細分成很多種方式。下面將舉例說明一下。
二,mysql的分割槽
我覺著吧,mysql的分割槽只有一種方式,只不過運用不同的演算法,規則將資料分配到不同的區塊中而已。
1,mysql5.1及以上支援分割槽功能
安裝安裝的時候,我們就可以檢視一下
mysql> show plugins; +--------------------------------+----------+--------------------+---------+---------+ | Name | Status | Type | Library | License | +--------------------------------+----------+--------------------+---------+---------+ | binlog | ACTIVE | STORAGE ENGINE | NULL | GPL | | mysql_native_password | ACTIVE | AUTHENTICATION | NULL | GPL | | mysql_old_password | ACTIVE | AUTHENTICATION | NULL | GPL | | MEMORY | ACTIVE | STORAGE ENGINE | NULL | GPL | | MRG_MYISAM | ACTIVE | STORAGE ENGINE | NULL | GPL | | MyISAM | ACTIVE | STORAGE ENGINE | NULL | GPL | | CSV | ACTIVE | STORAGE ENGINE | NULL | GPL | | InnoDB | ACTIVE | STORAGE ENGINE | NULL | GPL | | INNODB_RSEG | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_UNDO_LOGS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_TRX | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_LOCKS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_LOCK_WAITS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMP | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMP_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMPMEM | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CMPMEM_RESET | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_TABLES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_TABLESTATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_INDEXES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_COLUMNS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_FIELDS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_FOREIGN | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_FOREIGN_COLS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_SYS_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_TABLE_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_INDEX_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_POOL_PAGES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_POOL_PAGES_INDEX | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_POOL_PAGES_BLOB | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | XTRADB_ADMIN_COMMAND | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_CHANGED_PAGES | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_PAGE | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_PAGE_LRU | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | INNODB_BUFFER_POOL_STATS | ACTIVE | INFORMATION SCHEMA | NULL | GPL | | PERFORMANCE_SCHEMA | ACTIVE | STORAGE ENGINE | NULL | GPL | | FEDERATED | DISABLED | STORAGE ENGINE | NULL | GPL | | BLACKHOLE | ACTIVE | STORAGE ENGINE | NULL | GPL | | ARCHIVE | ACTIVE | STORAGE ENGINE | NULL | GPL | | partition | ACTIVE | STORAGE ENGINE | NULL | GPL | +--------------------------------+----------+--------------------+---------+---------+ 40 rows in set (0.00 sec)
檢視一下變數,如果支援的話,會有上面的提示的。
2,range分割槽
按照RANGE分割槽的表是透過如下一種方式進行分割槽的,每個分割槽包含那些分割槽表示式的值位於一個給定的連續區間內的行
//建立range分割槽表 mysql> CREATE TABLE IF NOT EXISTS `user` ( -> `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '使用者ID', -> `name` varchar(50) NOT NULL DEFAULT '' COMMENT '名稱', -> `sex` int(1) NOT NULL DEFAULT '0' COMMENT '0為男,1為女', -> PRIMARY KEY (`id`) -> ) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 -> PARTITION BY RANGE (id) ( -> PARTITION p0 VALUES LESS THAN (3), -> PARTITION p1 VALUES LESS THAN (6), -> PARTITION p2 VALUES LESS THAN (9), -> PARTITION p3 VALUES LESS THAN (12), -> PARTITION p4 VALUES LESS THAN MAXVALUE -> ); Query OK, 0 rows affected (0.13 sec) //插入一些資料 mysql> INSERT INTO `test`.`user` (`name` ,`sex`)VALUES ('tank', '0') -> ,('zhang',1),('ying',1),('張',1),('映',0),('test1',1),('tank2',1) -> ,('tank1',1),('test2',1),('test3',1),('test4',1),('test5',1),('tank3',1) -> ,('tank4',1),('tank5',1),('tank6',1),('tank7',1),('tank8',1),('tank9',1) -> ,('tank10',1),('tank11',1),('tank12',1),('tank13',1),('tank21',1),('tank42',1); Query OK, 25 rows affected (0.05 sec) Records: 25 Duplicates: 0 Warnings: 0 //到存放資料庫表檔案的地方看一下,my.cnf裡面有配置,datadir後面就是 [root@BlackGhost test]# ls |grep user |xargs du -sh 4.0K user#P#p0.MYD 4.0K user#P#p0.MYI 4.0K user#P#p1.MYD 4.0K user#P#p1.MYI 4.0K user#P#p2.MYD 4.0K user#P#p2.MYI 4.0K user#P#p3.MYD 4.0K user#P#p3.MYI 4.0K user#P#p4.MYD 4.0K user#P#p4.MYI 12K user.frm 4.0K user.par //取出資料 mysql> select count(id) as count from user; +-------+ | count | +-------+ | 25 | +-------+ 1 row in set (0.00 sec) //刪除第四個分割槽 mysql> alter table user drop partition p4; Query OK, 0 rows affected (0.11 sec) Records: 0 Duplicates: 0 Warnings: 0 /**存放在分割槽裡面的資料丟失了,第四個分割槽裡面有14條資料,剩下的3個分割槽 只有11條資料,但是統計出來的檔案大小都是4.0K,從這兒我們可以看出分割槽的 最小區塊是4K */ mysql> select count(id) as count from user; +-------+ | count | +-------+ | 11 | +-------+ 1 row in set (0.00 sec) //第四個區塊已刪除 [root@BlackGhost test]# ls |grep user |xargs du -sh 4.0K user#P#p0.MYD 4.0K user#P#p0.MYI 4.0K user#P#p1.MYD 4.0K user#P#p1.MYI 4.0K user#P#p2.MYD 4.0K user#P#p2.MYI 4.0K user#P#p3.MYD 4.0K user#P#p3.MYI 12K user.frm 4.0K user.par /*可以對現有表進行分割槽,並且會按規則自動的將表中的資料分配相應的分割槽 中,這樣就比較好了,可以省去很多事情,看下面的操作*/ mysql> alter table aa partition by RANGE(id) -> (PARTITION p1 VALUES less than (1), -> PARTITION p2 VALUES less than (5), -> PARTITION p3 VALUES less than MAXVALUE); Query OK, 15 rows affected (0.21 sec) //對15資料進行分割槽 Records: 15 Duplicates: 0 Warnings: 0 //總共有15條 mysql> select count(*) from aa; +----------+ | count(*) | +----------+ | 15 | +----------+ 1 row in set (0.00 sec) //刪除一個分割槽 mysql> alter table aa drop partition p2; Query OK, 0 rows affected (0.30 sec) Records: 0 Duplicates: 0 Warnings: 0 //只有11條了,說明對現有的表分割槽成功了 mysql> select count(*) from aa; +----------+ | count(*) | +----------+ | 11 | +----------+ 1 row in set (0.00 sec)
時間欄位分割槽
- 時間欄位分割槽:
-
####建表
CREATE TABLE `testdn` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`uid` bigint(20) unsigned DEFAULT '0',
`username` char(32) NOT NULL DEFAULT '' COMMENT '姓名',
`mobile` char(64) NOT NULL COMMENT '手機號碼',
`iden_card` varchar(64) DEFAULT '0' COMMENT '身份證號',
`zone_id` int(10) unsigned DEFAULT '0' COMMENT '工作城市所在地',
`gps_zone_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'GPS歸屬地',
`push_end_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '已推送未購買標記時間', -----date與datetime用to_days函式轉換
`order_score` int(4) DEFAULT '0' COMMENT '訂單評分,10分以下直接變成會員單,90分以上一直是金牌會員單',
PRIMARY KEY (`id`,`push_end_time`),
KEY `idx_usernmae` (`username`),
KEY `idx_uid` (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=13597441 DEFAULT CHARSET=utf8 COMMENT='訂單表'
PARTITION BY RANGE (TO_DAYS(push_end_time))
(PARTITION testdn_20160601 VALUES LESS THAN (736481) ENGINE = InnoDB,
PARTITION testdn_20160701 VALUES LESS THAN (736511) ENGINE = InnoDB,
PARTITION testdn_20160801 VALUES LESS THAN (736542) ENGINE = InnoDB,
PARTITION testdn_20160901 VALUES LESS THAN (736573) ENGINE = InnoDB,
PARTITION testdn_20161001 VALUES LESS THAN (736603) ENGINE = InnoDB,
PARTITION testdn_20161101 VALUES LESS THAN (736634) ENGINE = InnoDB,
PARTITION testdn_20161201 VALUES LESS THAN (736664) ENGINE = InnoDB,
PARTITION testdn_20170101 VALUES LESS THAN (736695) ENGINE = InnoDB,
PARTITION testdn_20170201 VALUES LESS THAN (736726) ENGINE = InnoDB,
PARTITION testdn_20170301 VALUES LESS THAN (736754) ENGINE = InnoDB,
PARTITION testdn_20170401 VALUES LESS THAN (736785) ENGINE = InnoDB,
PARTITION testdn_20170501 VALUES LESS THAN (736815) ENGINE = InnoDB)
#####It is also possible to partition a table by RANGE, based on the value of a TIMESTAMP column, using the UNIX_TIMESTAMP() function, as shown in this example:
CREATE TABLE `testdn2` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`uid` bigint(20) unsigned DEFAULT '0',
`username` char(32) NOT NULL DEFAULT '' COMMENT '姓名',
`mobile` char(64) NOT NULL COMMENT '手機號碼',
`iden_card` varchar(64) DEFAULT '0' COMMENT '身份證號',
`zone_id` int(10) unsigned DEFAULT '0' COMMENT '工作城市所在地',
`gps_zone_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'GPS歸屬地',
`push_end_time` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '已推送未購買標記時間',
`order_score` int(4) DEFAULT '0' COMMENT '訂單評分,10分以下直接變成會員單,90分以上一直是金牌會員單',
PRIMARY KEY (`id`,`push_end_time`),
KEY `idx_usernmae` (`username`),
KEY `idx_uid` (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=13597441 DEFAULT CHARSET=utf8 COMMENT='訂單表'
PARTITION BY RANGE (unix_timestamp(push_end_time))
(PARTITION testdn_20160601 VALUES LESS THAN (UNIX_TIMESTAMP('2016-06-01 00:00:00')) ENGINE = InnoDB,
PARTITION testdn_20160701 VALUES LESS THAN (UNIX_TIMESTAMP('2016-07-01 00:00:00')) ENGINE = InnoDB,
PARTITION testdn_20160801 VALUES LESS THAN (UNIX_TIMESTAMP('2016-08-01 00:00:00')) ENGINE = InnoDB,
PARTITION testdn_20160901 VALUES LESS THAN (UNIX_TIMESTAMP('2016-09-01 00:00:00')) ENGINE = InnoDB,
PARTITION testdn_20161001 VALUES LESS THAN (UNIX_TIMESTAMP('2016-10-01 00:00:00')) ENGINE = InnoDB,
PARTITION testdn_20161101 VALUES LESS THAN (UNIX_TIMESTAMP('2016-11-01 00:00:00')) ENGINE = InnoDB,
PARTITION testdn_20161201 VALUES LESS THAN (UNIX_TIMESTAMP('2016-12-01 00:00:00')) ENGINE = InnoDB,
PARTITION testdn_20170101 VALUES LESS THAN (UNIX_TIMESTAMP('2017-01-01 00:00:00')) ENGINE = InnoDB,
PARTITION testdn_20170201 VALUES LESS THAN (UNIX_TIMESTAMP('2017-02-01 00:00:00')) ENGINE = InnoDB,
PARTITION testdn_20170301 VALUES LESS THAN (UNIX_TIMESTAMP('2017-03-01 00:00:00')) ENGINE = InnoDB,
PARTITION testdn_20170401 VALUES LESS THAN (UNIX_TIMESTAMP('2017-04-01 00:00:00')) ENGINE = InnoDB,
PARTITION testdn_20170501 VALUES LESS THAN (UNIX_TIMESTAMP('2017-05-01 00:00:00')) ENGINE = InnoDB)
#####columns 分割槽,時間不用轉換,自動轉換 時間只支援date datetime,還支援int等,詳見innodb內幕分割槽部分
CREATE TABLE `testdn3` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`uid` bigint(20) unsigned DEFAULT '0',
`username` char(32) NOT NULL DEFAULT '' COMMENT '姓名',
`mobile` char(64) NOT NULL COMMENT '手機號碼',
`iden_card` varchar(64) DEFAULT '0' COMMENT '身份證號',
`zone_id` int(10) unsigned DEFAULT '0' COMMENT '工作城市所在地',
`gps_zone_id` int(10) unsigned NOT NULL DEFAULT '0' COMMENT 'GPS歸屬地',
`push_end_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '已推送未購買標記時間',
`order_score` int(4) DEFAULT '0' COMMENT '訂單評分,10分以下直接變成會員單,90分以上一直是金牌會員單',
PRIMARY KEY (`id`,`push_end_time`),
KEY `idx_usernmae` (`username`),
KEY `idx_uid` (`uid`)
) ENGINE=InnoDB AUTO_INCREMENT=13597441 DEFAULT CHARSET=utf8 COMMENT='訂單表'
PARTITION BY RANGE columns (push_end_time)
(PARTITION testdn_20160601 VALUES LESS THAN ('2016-06-01 00:00:00') ENGINE = InnoDB,
PARTITION testdn_20160701 VALUES LESS THAN ('2016-07-01 00:00:00') ENGINE = InnoDB,
PARTITION testdn_20160801 VALUES LESS THAN ('2016-08-01 00:00:00') ENGINE = InnoDB,
PARTITION testdn_20160901 VALUES LESS THAN ('2016-09-01 00:00:00') ENGINE = InnoDB,
PARTITION testdn_20161001 VALUES LESS THAN ('2016-10-01 00:00:00') ENGINE = InnoDB,
PARTITION testdn_20161101 VALUES LESS THAN ('2016-11-01 00:00:00') ENGINE = InnoDB,
PARTITION testdn_20161201 VALUES LESS THAN ('2016-12-01 00:00:00') ENGINE = InnoDB,
PARTITION testdn_20170101 VALUES LESS THAN ('2017-01-01 00:00:00') ENGINE = InnoDB,
PARTITION testdn_20170201 VALUES LESS THAN ('2017-02-01 00:00:00') ENGINE = InnoDB,
PARTITION testdn_20170301 VALUES LESS THAN ('2017-03-01 00:00:00') ENGINE = InnoDB,
PARTITION testdn_20170401 VALUES LESS THAN ('2017-04-01 00:00:00') ENGINE = InnoDB,
PARTITION testdn_20170501 VALUES LESS THAN ('2017-05-01 00:00:00') ENGINE = InnoDB)
3,list分割槽
LIST分割槽中每個分割槽的定義和選擇是基於某列的值從屬於一個值列表集中的一個值,而RANGE分 區是從屬於一個連續區間值的集合。
//這種方式失敗 mysql> CREATE TABLE IF NOT EXISTS `list_part` ( -> `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '使用者ID', -> `province_id` int(2) NOT NULL DEFAULT 0 COMMENT '省', -> `name` varchar(50) NOT NULL DEFAULT '' COMMENT '名稱', -> `sex` int(1) NOT NULL DEFAULT '0' COMMENT '0為男,1為女', -> PRIMARY KEY (`id`) -> ) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 -> PARTITION BY LIST (province_id) ( -> PARTITION p0 VALUES IN (1,2,3,4,5,6,7,8), -> PARTITION p1 VALUES IN (9,10,11,12,16,21), -> PARTITION p2 VALUES IN (13,14,15,19), -> PARTITION p3 VALUES IN (17,18,20,22,23,24) -> ); ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function //這種方式成功 mysql> CREATE TABLE IF NOT EXISTS `list_part` ( -> `id` int(11) NOT NULL COMMENT '使用者ID', -> `province_id` int(2) NOT NULL DEFAULT 0 COMMENT '省', -> `name` varchar(50) NOT NULL DEFAULT '' COMMENT '名稱', -> `sex` int(1) NOT NULL DEFAULT '0' COMMENT '0為男,1為女' -> ) ENGINE=INNODB DEFAULT CHARSET=utf8 -> PARTITION BY LIST (province_id) ( -> PARTITION p0 VALUES IN (1,2,3,4,5,6,7,8), -> PARTITION p1 VALUES IN (9,10,11,12,16,21), -> PARTITION p2 VALUES IN (13,14,15,19), -> PARTITION p3 VALUES IN (17,18,20,22,23,24) -> ); Query OK, 0 rows affected (0.33 sec)
上面的這個建立list分割槽時,如果有主銉的話,分割槽時主鍵必須在其中,不然就會報錯。如果我不用主鍵,分割槽就建立成功了,一般情況下,一個張表肯定會有一個主鍵,這算是一個分割槽的侷限性吧。
如果對資料進行測試,請參考range分割槽的測試來操作
4,hash分割槽
HASH分割槽主要用來確保資料在預先確定數目的分割槽中平均分佈,你所要做的只是基於將要被雜湊的列值指定一個列值或表示式,以 及指定被分割槽的表將要被分割成的分割槽數量。
mysql> CREATE TABLE IF NOT EXISTS `hash_part` ( -> `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '評論ID', -> `comment` varchar(1000) NOT NULL DEFAULT '' COMMENT '評論', -> `ip` varchar(25) NOT NULL DEFAULT '' COMMENT '來源IP', -> PRIMARY KEY (`id`) -> ) ENGINE=INNODB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 -> PARTITION BY HASH(id) -> PARTITIONS 3; Query OK, 0 rows affected (0.06 sec)
測試請參考range分割槽的操作
5,key分割槽
按照KEY進行分割槽類似於按照HASH分割槽,除了HASH分割槽使用的用 戶定義的表示式,而KEY分割槽的 雜湊函式是由MySQL 伺服器提供。
mysql> CREATE TABLE IF NOT EXISTS `key_part` ( -> `news_id` int(11) NOT NULL COMMENT '新聞ID', -> `content` varchar(1000) NOT NULL DEFAULT '' COMMENT '新聞內容', -> `u_id` varchar(25) NOT NULL DEFAULT '' COMMENT '來源IP', -> `create_time` DATE NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '時間' -> ) ENGINE=INNODB DEFAULT CHARSET=utf8 -> PARTITION BY LINEAR HASH(YEAR(create_time)) -> PARTITIONS 3; Query OK, 0 rows affected (0.07 sec)
測試請參考range分割槽的操作
6,子分割槽
子分割槽是分割槽表中每個分割槽的再次分割,子分割槽既可以使用HASH希分割槽,也可以使用KEY分割槽。這 也被稱為複合分割槽(composite partitioning)。
1,如果一個分割槽中建立了子分割槽,其他分割槽也要有子分割槽
2,如果建立了了分割槽,每個分割槽中的子分割槽數必有相同
3,同一分割槽內的子分割槽,名字不相同,不同分割槽內的子分割槽名子可以相同(5.1.50不適用)
mysql> CREATE TABLE IF NOT EXISTS `sub_part` ( -> `news_id` int(11) NOT NULL COMMENT '新聞ID', -> `content` varchar(1000) NOT NULL DEFAULT '' COMMENT '新聞內容', -> `u_id` int(11) NOT NULL DEFAULT 0s COMMENT '來源IP', -> `create_time` DATE NOT NULL DEFAULT '0000-00-00 00:00:00' COMMENT '時間' -> ) ENGINE=INNODB DEFAULT CHARSET=utf8 -> PARTITION BY RANGE(YEAR(create_time)) -> SUBPARTITION BY HASH(TO_DAYS(create_time))( -> PARTITION p0 VALUES LESS THAN (1990)(SUBPARTITION s0,SUBPARTITION s1,SUBPARTITION s2), -> PARTITION p1 VALUES LESS THAN (2000)(SUBPARTITION s3,SUBPARTITION s4,SUBPARTITION good), -> PARTITION p2 VALUES LESS THAN MAXVALUE(SUBPARTITION tank0,SUBPARTITION tank1,SUBPARTITION tank3) -> ); Query OK, 0 rows affected (0.07 sec)
官方網站說不同分割槽內的子分割槽可以有相同的名字,但是mysql5.1.50卻不行會提示以下錯誤
ERROR 1517 (HY000): Duplicate partition name s1
三,分割槽管理
1,刪除分割槽
mysql> alter table user drop partition p4;
2,新增分割槽
//range新增新分割槽 mysql> alter table user add partition(partition p4 values less than MAXVALUE); Query OK, 0 rows affected (0.06 sec) Records: 0 Duplicates: 0 Warnings: 0 //list新增新分割槽 mysql> alter table list_part add partition(partition p4 values in (25,26,28)); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 //hash重新分割槽 mysql> alter table hash_part add partition partitions 4; Query OK, 0 rows affected (0.12 sec) Records: 0 Duplicates: 0 Warnings: 0 //key重新分割槽 mysql> alter table key_part add partition partitions 4; Query OK, 1 row affected (0.06 sec) //有資料也會被重新分配 Records: 1 Duplicates: 0 Warnings: 0 //子分割槽新增新分割槽,雖然我沒有指定子分割槽,但是系統會給子分割槽命名的 mysql> alter table sub1_part add partition(partition p3 values less than MAXVALUE); Query OK, 0 rows affected (0.02 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> show create table sub1_part\G; *************************** 1. row *************************** Table: sub1_part Create Table: CREATE TABLE `sub1_part` ( `news_id` int(11) NOT NULL COMMENT '新聞ID', `content` varchar(1000) NOT NULL DEFAULT '' COMMENT '新聞內容', `u_id` varchar(25) NOT NULL DEFAULT '' COMMENT '來源IP', `create_time` date NOT NULL DEFAULT '0000-00-00' COMMENT '時間' ) ENGINE=InnoDB DEFAULT CHARSET=utf8 !50100 PARTITION BY RANGE (YEAR(create_time)) SUBPARTITION BY HASH (TO_DAYS(create_time)) (PARTITION p0 VALUES LESS THAN (1990) (SUBPARTITION s0 ENGINE = InnoDB, SUBPARTITION s1 ENGINE = InnoDB, SUBPARTITION s2 ENGINE = InnoDB), PARTITION p1 VALUES LESS THAN (2000) (SUBPARTITION s3 ENGINE = InnoDB, SUBPARTITION s4 ENGINE = InnoDB, SUBPARTITION good ENGINE = InnoDB), PARTITION p2 VALUES LESS THAN (3000) (SUBPARTITION tank0 ENGINE = InnoDB, SUBPARTITION tank1 ENGINE = InnoDB, SUBPARTITION tank3 ENGINE = InnoDB), PARTITION p3 VALUES LESS THAN MAXVALUE (SUBPARTITION p3sp0 ENGINE = InnoDB, //子分割槽的名子是自動生成的 SUBPARTITION p3sp1 ENGINE = InnoDB, SUBPARTITION p3sp2 ENGINE = InnoDB)) 1 row in set (0.00 sec)
3,重新分割槽
//range重新分割槽 mysql> ALTER TABLE user REORGANIZE PARTITION p0,p1,p2,p3,p4 INTO (PARTITION p0 VALUES LESS THAN MAXVALUE); Query OK, 11 rows affected (0.08 sec) Records: 11 Duplicates: 0 Warnings: 0 //list重新分割槽 mysql> ALTER TABLE list_part REORGANIZE PARTITION p0,p1,p2,p3,p4 INTO (PARTITION p0 VALUES in (1,2,3,4,5)); Query OK, 0 rows affected (0.28 sec) Records: 0 Duplicates: 0 Warnings: 0 //hash和key分割槽不能用REORGANIZE,官方網站說的很清楚 mysql> ALTER TABLE key_part REORGANIZE PARTITION COALESCE PARTITION 9; ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'PARTITION 9' at line 1
四,分割槽優點
1,分割槽可以分在多個磁碟,儲存更大一點
2,根據查詢條件,也就是where後面的條件,查詢只查詢相應的分割槽不用全部查詢了
3,進行大資料搜尋時可以進行並行處理。
4,跨多個磁碟來分散資料查詢,來獲得更大的查詢吞吐量
來自:http://blog.51yip.com/mysql/1013.html
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29096438/viewspace-1409170/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- mysql 分割槽MySql
- 理解MySQL分割槽MySql
- 搞懂MySQL分割槽MySql
- 【MYSQL】 分割槽表MySql
- MySql建立分割槽MySql
- MySQL 子分割槽MySql
- MySQL KEY分割槽MySql
- MySQL HASH分割槽MySql
- MySQL COLUMNS分割槽MySql
- MySQL LIST分割槽MySql
- MySQL RANGE分割槽MySql
- MySQL 分割槽表MySql
- MySQL分割槽表MySql
- mysql分割槽nullMySqlNull
- mysql 分割槽示例MySql
- MySql資料分割槽操作之新增分割槽操作MySql
- MySQL 分割槽表 partition線上修改分割槽欄位MySql
- MySQL的分割槽(一)MySql
- MySQL的分割槽(二)MySql
- MySQL 分割槽表探索MySql
- mysql 分表 分割槽MySql
- MySQL分割槽學習MySql
- MySQL 分割槽建索引MySql索引
- MySQL分割槽介紹MySql
- mysql 分割槽表用法MySql
- MySQL表分割槽管理MySql
- MySQL分割槽表的分割槽原理和優缺點MySql
- 將mysql非分割槽錶轉換為分割槽表MySql
- MySQL分割槽如何遷移MySql
- mysql 進行表分割槽MySql
- Mysql表分割槽實現MySql
- Mysql 的分割槽型別MySql型別
- mysql分割槽表筆記MySql筆記
- MySQL入門--分割槽表MySql
- MySQL分割槽(Partition)詳解MySql
- mysql —— 分表分割槽(1)MySql
- MySQL的List分割槽表MySql
- Mysql表分割槽實操MySql