今天實踐下mysql百萬級數據分區的影響,首先是產生百萬級別的數據量mysql
//建立帶分區的數據表 CREATE TABLE `part_person` ( `id` bigint(20) unsigned NOT NULL, `username` varchar(100) NOT NULL, `born` date NOT NULL DEFAULT '1970-01-01', `sex` tinyint(1) unsigned NOT NULL, PRIMARY KEY (`id`,`born`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8 PARTITION BY RANGE (year(born)) (PARTITION p0 VALUES LESS THAN (1980) ENGINE = MyISAM, PARTITION p1 VALUES LESS THAN (1990) ENGINE = MyISAM, PARTITION p2 VALUES LESS THAN (2000) ENGINE = MyISAM, PARTITION p3 VALUES LESS THAN (2010) ENGINE = MyISAM, PARTITION p4 VALUES LESS THAN (2020) ENGINE = MyISAM, PARTITION p5 VALUES LESS THAN MAXVALUE ENGINE = MyISAM); //建立不帶分區的數據表 CREATE TABLE `no_part_person` ( `id` bigint(20) unsigned NOT NULL, `username` varchar(100) NOT NULL, `born` date NOT NULL DEFAULT '1970-01-01', `sex` tinyint(1) unsigned NOT NULL, PRIMARY KEY (`id`,`born`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; //填充數據,建立procedure向數據表插入數據 CREATE PROCEDURE `part_generate`(IN num INT) BEGIN DECLARE char_str varchar(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; DECLARE username VARCHAR(25) DEFAULT ''; DECLARE id int UNSIGNED; DECLARE len int; set id=1; DELETE from person; WHILE id <= num DO set len = FLOOR(1 + RAND()*25); set username = ''; WHILE len > 0 DO SET username = CONCAT(username,substring(char_str,FLOOR(1 + RAND()*62),1)); SET len = len - 1; END WHILE; INSERT into part_person VALUES (id,username, ADDDATE('1970-01-01',INTERVAL RAND()*365*60 DAY), FLOOR(RAND()*2)); set id = id + 1; END WHILE; END //執行procedure插入600萬數據 call part_generate(6000000) //向未分區表插入數據 insert into no_part_person select * from part_person;
如今有了數據,對比一下有沒有分區對查詢的影響
查詢不是按照該列分區的數據時分區反而更慢一些,查詢born數據時不跨區時分區效果提高顯著,當數據跨區時提高效果沒那麼顯著,但也有提高。sql