Mysql插入百万数据测试
CREATE TABLE `t_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `c_user_id` varchar(36) NOT NULL DEFAULT '', `c_name` varchar(22) NOT NULL DEFAULT '', `c_province_id` int(11) NOT NULL, `c_city_id` int(11) NOT NULL, `create_time` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE `t_user_memory` ( `id` int(11) NOT NULL AUTO_INCREMENT, `c_user_id` varchar(36) NOT NULL DEFAULT '', `c_name` varchar(22) NOT NULL DEFAULT '', `c_province_id` int(11) NOT NULL, `c_city_id` int(11) NOT NULL, `create_time` datetime NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MEMORY DEFAULT CHARSET=utf8mb4; drop FUNCTION randStr; delimiter $$ CREATE DEFINER=`root`@`%` FUNCTION `randStr`(n INT) RETURNS varchar(255) CHARSET utf8mb4 DETERMINISTIC BEGIN DECLARE chars_str varchar(100) DEFAULT 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; DECLARE return_str varchar(255) DEFAULT ''; DECLARE i INT DEFAULT 0; WHILE i<n DO SET return_str = concat(return_str, substring(chars_str, FLOOR(1 + RAND() * 62), 1)); SET i=i+1; END WHILE; RETURN return_str; END $$ drop FUNCTION randDataTime; delimiter $$ CREATE DEFINER = `root`@`%` FUNCTION `randDataTime` (sd DATETIME, ed DATETIME) RETURNS datetime DETERMINISTIC BEGIN DECLARE sub INT DEFAULT 0 ; DECLARE ret DATETIME ; SET sub = ABS( UNIX_TIMESTAMP(ed) - UNIX_TIMESTAMP(sd) ) ; SET ret = DATE_ADD( sd, INTERVAL FLOOR(1 + RAND() *(sub - 1)) SECOND ) ; RETURN ret ; END $$ SET GLOBAL max_heap_table_size = 1024 * 1024 * 1024 * 1024 * 1; select @@max_heap_table_size; drop PROCEDURE add_t_user_memory; delimiter $$ CREATE DEFINER=`root`@`%` PROCEDURE add_t_user_memory (IN n INT) BEGIN DECLARE i INT DEFAULT 1 ; WHILE i < n DO INSERT INTO t_user_memory ( c_user_id, c_name, c_province_id, c_city_id, create_time ) VALUES ( uuid(), randStr (20), FLOOR(RAND() * 1000), RAND() * 100, NOW() ); SET i = i + 1 ; END WHILE ; END $$ CALL add_t_user_memory (1000000); INSERT INTO t_user SELECT * FROM t_user_memory; UPDATE t_user SET create_time=date_add(create_time, interval FLOOR(1 + (RAND() * 4)) year); UPDATE t_user SET create_time=randDataTime(NOW(),create_time);
可以参考链接https://www.cnblogs.com/hill1126/p/11334523.html