进行SQL优化或查询性能测试时,咱们须要大量数据测试来模拟,这个时候引出一个问题:数据的建立
java
建立数据无非几种操做下面一一列举;
~ 1 手动输入 (可忽略)
~ 2 使用编写好的存储过程和函数执行 (下面会有介绍)
~ 3 编写代码,经过代码插入数据 (例:使用mybatis的foreach循环插入..步骤多,速度慢)
~ 4 临时数据表方式执行 (强烈推荐,速度快,简单)python
首先 咱们无论选哪一种操做 都要先准备一张表
,这个是毫无疑问的;
那么咱们就简单的建立一个表 以下;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`), KEY `idx_user_id` (`c_user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
咱们先利用函数和存储过程在内存表中生成数据,再从内存表中插入普通表sql
1 建立一个内存表
mybatis
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`), KEY `idx_user_id` (`c_user_id`) ) ENGINE=MEMORY DEFAULT CHARSET=utf8mb4;
2 建立函数和存储过程
函数
# 建立随机字符串和随机时间的函数 mysql> delimiter $$ mysql> 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$$ Query OK, 0 rows affected (0.00 sec) mysql> 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 $$ mysql> delimiter ; # 建立插入数据存储过程 mysql> 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), FLOOR(RAND() * 100), NOW()); -> SET i = i + 1; -> END WHILE; -> END -> $$ Query OK, 0 rows affected (0.01 sec)
调用存储过程
性能
mysql> CALL add_t_user_memory(1000000); //添加的数据量 ERROR 1114 (HY000): The table 't_user_memory' is full
PS:出现ERROR 1114 (HY000): The table 't_user_memory' is full
错误,表示内存已满测试
处理方式:修改 max_heap_table_size 参数的大小 默认32M或者64M就好,生产环境不要乱尝试哦
.优化
从内存表插入普通表
ui
mysql> INSERT INTO t_user SELECT * FROM t_user_memory; Query OK, 218953 rows affected (1.70 sec) Records: 218953 Duplicates: 0 Warnings: 0
建立临时数据表tmp_table
CREATE TABLE tmp_table ( id INT, PRIMARY KEY (id) );
python:python -c "for i in range(1, 1+1000000): print(i)" > base.txt
导入数据到临时表tmp_table中
load data infile '/Users/LJTjintao/temp/base.txt' replace into table tmp_table;
Query OK, 1000000 rows affected (2.55 sec)
Records: 1000000 Deleted: 0 Skipped: 0 Warnings: 0关注公众号:java宝典