我觉得这种方法比较适合,那些没有事先考虑,而已经出现了得,数据查询慢的情况。这个时候如果要把已有的大数据量表分开比较痛苦,最痛苦的事就是改代码,因为程序里面的sql语句已经写好了,现在一张表要分成几十张表,甚至上百张表,这样sql语句是不是要重写呢?举个例子,我很喜欢举子
mysql>show engines;的时候你会发现mrg_myisam其实就是merge。
第一步:建立user1及users
CREATE TABLE user1 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name CHAR(20), pass CHAR(32), sex tinyint(1)) ENGINE=Myisam;
CREATE TABLE users (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name CHAR(20), pass CHAR(32), sex tinyint(1)) ENGINE=MERGE UNION=(user1) INSERT_METHOD=LAST;
INSERT INTO users (name,pass,sex) VALUES ('user11','123456',1),('user12','123456',1),('user13','123456',0);
第二步:建立user2及修改users
CREATE TABLE user2 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name CHAR(20), pass CHAR(32), sex tinyint(1)) ENGINE=Myisam;
ALTER TABLE users UNION=(user1,user2) INSERT_METHOD=LAST;
INSERT INTO users (name,pass,sex) VALUES ('user21','123456',1),('user22','123456',1),('user23','123456',0);
第三步:建立user3及修改users
CREATE TABLE user3 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name CHAR(20), pass CHAR(32), sex tinyint(1)) ENGINE=Myisam;
ALTER TABLE users UNION=(user1,user2,user3) INSERT_METHOD=LAST;
INSERT INTO users (name,pass,sex) VALUES ('user31','123456',1),('user32','123456',1),('user33','123456',0);