1、生成思路
利用mysql内存表插入速度快的特点,先利用函数和存储过程在内存表中生成数据,然后再从内存表插入普通表中
2、创建内存表及普通表
CREATE TABLE t_emm_process_memory
(
id
bigint(20) NOT NULL AUTO_INCREMENT,
appName
varchar(255) DEFAULT NULL,
udid
varchar(255) DEFAULT NULL,
processName
varchar(255) DEFAULT NULL,
createTime
datetime DEFAULT NULL,
runPath
varchar(255) DEFAULT NULL,
runTime
bigint(255) DEFAULT NULL,
runningTime
varchar(255) DEFAULT NULL,
PRIMARY KEY (id
),
KEY index_id
(processName
) USING HASH
) ENGINE = MEMORY AUTO_INCREMENT = 1 DEFAULT CHARSET = utf8
上述memory就是新建的内存表 ! ! (重启服务会清空表数据)
普通表是 InnoDB
CREATE TABLE t_emm_process
(
id
bigint(20) NOT NULL AUTO_INCREMENT,
appName
varchar(255) DEFAULT NULL,
udid
varchar(255) DEFAULT NULL,
processName
varchar(255) DEFAULT NULL,
createTime
datetime DEFAULT NULL,
runPath
varchar(255) DEFAULT NULL,
runTime
bigint(255) DEFAULT NULL,
PRIMARY KEY (id
) USING BTREE,
KEY index_id
(id
),
KEY index_processName
(processName
,runningTime
,createTime
) USING BTREE,
KEY index_processTime
(createTime
,runningTime
) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=3031586 DEFAULT CHARSET=utf8;
show variables like ‘log_bin_trust_function_creators’; 打开
set global log_bin_trust_function_creators=1;
执行过程中, 内存满的话需要修改my.ini
max_heap_table_size = 400000M
3、创建函数及存储过程
CREATE FUNCTION `rand_string`(n INT) RETURNS varchar(255) CHARSET latin1
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
CREATE PROCEDURE `add_vote_memory`(IN n int)
BEGIN
DECLARE i INT DEFAULT 1;
WHILE (i <= n ) DO
INSERT into t_emm_process_memory(processName,runningTime,createTime) VALUEs (FLOOR(RAND() * 1000), FLOOR(RAND() * 100000), now());
set i=i+1;
END WHILE;
END
4、调用存储过程
CALL add_vote_memory(3000000); -- 生成了300万数据
5、插入普通表中
INSERT INTO t_emm_process(`processName`, `runningTime`, createTime)
SELECT `processName`, `runningTime`, `createTime` FROM t_emm_process_memory;
只需要插入需要的字段即可!!!
6、结果
文章参考: https://blog.csdn.net/whzhaochao/article/details/49126037
本次优化主要是, 新增了组合索引(上述建表可以看到)
本文地址:https://blog.csdn.net/weixin_42804852/article/details/111051456