一般來說,當我們的數據庫的數據超過了100w記錄的時候就應該考慮分表或者分區了,這次我來詳細說說分表的一些方法。目前我所知道的方法都是MYISAM的,INNODB如何做分表並且保留事務和外鍵,我還不是很了解。
首先,我們需要想好到底分多少個表,前提當然是滿足應用。這裡我使用了一個比較簡單的分表方法,就是根據自增id的尾數來分,也就是說分0-9一共10個表,其取值也很好做,就是對10進行取模。另外,還可以根據某一字段的md5值取其中幾位進行分表,這樣的話,可以分的表就很多了。
好了,先來創建表吧,代碼如下:
CREATE TABLE `test`.`article_0` (
`id` BIGINT( 20 ) NOT NULL ,
`subject` VARCHAR( 200 ) NOT NULL ,
`content` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci
CREATE TABLE `test`.`article_1` (
`id` BIGINT( 20 ) NOT NULL ,
`subject` VARCHAR( 200 ) NOT NULL ,
`content` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci
CREATE TABLE `test`.`article_2` (
`id` BIGINT( 20 ) NOT NULL ,
`subject` VARCHAR( 200 ) NOT NULL ,
`content` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci
CREATE TABLE `test`.`article_3` (
`id` BIGINT( 20 ) NOT NULL ,
`subject` VARCHAR( 200 ) NOT NULL ,
`content` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci
CREATE TABLE `test`.`article_4` (
`id` BIGINT( 20 ) NOT NULL ,
`subject` VARCHAR( 200 ) NOT NULL ,
`content` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci
CREATE TABLE `test`.`article_5` (
`id` BIGINT( 20 ) NOT NULL ,
`subject` VARCHAR( 200 ) NOT NULL ,
`content` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci
CREATE TABLE `test`.`article_6` (
`id` BIGINT( 20 ) NOT NULL ,
`subject` VARCHAR( 200 ) NOT NULL ,
`content` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci
CREATE TABLE `test`.`article_7` (
`id` BIGINT( 20 ) NOT NULL ,
`subject` VARCHAR( 200 ) NOT NULL ,
`content` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci
CREATE TABLE `test`.`article_8` (
`id` BIGINT( 20 ) NOT NULL ,
`subject` VARCHAR( 200 ) NOT NULL ,
`content` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM CHARACTER SET utf8 COLLATE utf8_general_ci
CREATE TABLE `test`.`article_9` (
`id` BIGINT( 20 ) NOT NULL ,