-- 无参创建 CREATEPROCEDURE demo1() BEGIN DECLARE i INTDEFAULT0; START TRANSACTION; WHILE i<1000000 DO INSERTINTO test1(send_time,event_type,event_name,status,model_code,user_id,card_id,file_name) VALUES(from_unixtime(1541302365+FLOOR(rand()*154130236),"%Y-%m-%d %H:%i:%s"),randStr(20),randStr(20),randNum(1),randNum(2),UUID(),UUID(),UUID()); SET i=i+1; END WHILE; COMMIT; END
随机生成n个随机数字
1 2 3 4 5 6 7 8 9 10 11 12 13
DELIMITER $$ CREATEFUNCTION randNum(n int) RETURNSVARCHAR(255) BEGIN DECLARE chars_str varchar(20) DEFAULT'0123456789'; DECLARE return_str varchar(255) DEFAULT''; DECLARE i INTDEFAULT0; WHILE i < n DO SET return_str = concat(return_str,substring(chars_str , FLOOR(1+ RAND()*10 ),1)); SET i = i +1; END WHILE; RETURN return_str; END $$ DELIMITER;
随机生成手机号函数
1 2 3 4 5 6 7 8 9 10 11 12 13
DELIMITER $$ CREATEFUNCTION generatePhone() RETURNSvarchar(20) BEGIN DECLARE head char(3); DECLARE phone varchar(20); DECLARE bodys varchar(100) default "130 131 132 133 134 135 136 137 138 139 186 187 189 151 157"; DECLARE starts int; SET starts =1+floor(rand()*15)*4; SET head =trim(substring(bodys,starts,3)); SET phone =trim(concat(head,randNum(8))); RETURN phone; END $$ DELIMITER ;
随机生成用户名函数
1 2 3 4 5 6 7 8 9 10 11 12 13 14
DELIMITER $$ CREATEFUNCTION `randStr`(n INT) RETURNSvarchar(255) CHARSET utf8mb4 DETERMINISTIC BEGIN DECLARE chars_str varchar(100) DEFAULT'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'; DECLARE return_str varchar(255) DEFAULT'' ; DECLARE i INTDEFAULT0; 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$$ DELIMITER;
实验过程
未加索引 100W数据大小: 158.69MB
1 2 3 4 5 6 7 8 9
-- cos: 0.574S ,total: 40939 SELECT*from test2 t where t.send_time BETWEEN'2020-01-17'AND'2020-03-30' -- cos: 0.474S ,total: 9974 SELECT*from test2 t where t.model_code=33; -- cos: 0.460 , total:1611 SELECT*from test2 t WHERE t.event_type =303; -- cos: 0.695 , total:100040 SELECT*from test2 t WHERE t.`status` =5;
添加索引 100W数据大小: 158.69MB + 81.67MB
1 2 3 4 5 6 7 8 9
-- cos: 0.427S ,total: 353230 时间字段加索引主要是看范围内数据量的大小,目前看区别不大 SELECT*from test2 t where t.send_time BETWEEN'2020-01-17'AND'2020-03-30' -- cos: 0.457S ,total: 9974 数据量查询得到的数据只占总数据量的5% 索引失效 SELECT*from test2 t where t.model_code=33; -- cos: 0.432 , total:1611 SELECT*from test2 t WHERE t.event_type =303; -- cos: 0.642 , total:100040 SELECT*from test2 t WHERE t.`status` =5;