概要
由于工作用使用Mysql 和Oracle,需要准备大量的数据,便利用存储过程造百万数据。
Mysql 造数据
- 准备两张表,一个INNODB存储引擎,一个是内存表使用MEMORY存储引擎。
普通表:
DROP TABLE IF EXISTS `sys_area_InnoDB`;
CREATE TABLE `sys_area_InnoDB` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT
`code` int(6) DEFAULT NULL COMMENT
`name` varchar(40) DEFAULT NULL COMMENT
`parentId` int(6) DEFAULT NULL COMMENT
`level` int(4) DEFAULT NULL COMMENT
`type` int(4) DEFAULT NULL COMMENT
`servicestate` int(5) DEFAULT NULL COMMENT
`delete_flag` int(255) DEFAULT '0' COMMENT
`region` int(11) DEFAULT NULL COMMENT
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=2145086 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='区域表';
内存表:
DROP TABLE IF EXISTS `sys_area_memory`;
CREATE TABLE `sys_area_memory` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT
`code` int(6) DEFAULT NULL COMMENT
`name` varchar(40) DEFAULT NULL COMMENT
`parentId` int(6) DEFAULT NULL COMMENT
`level` int(4) DEFAULT NULL COMMENT
`type` int(4) DEFAULT NULL COMMENT
`servicestate` int(5) DEFAULT NULL COMMENT
`delete_flag` int(255) DEFAULT '0' COMMENT
`region` int(11) DEFAULT NULL COMMENT
PRIMARY KEY (`id`) USING BTREE
) ENGINE=MEMORY AUTO_INCREMENT=2145086 DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT='区域表';
- 由于数据写入内存中,需要调整Mysql内存值,
打开 my.ini文件 添加 两行
tmp_table_size=2048M
max_heap_table_size=2048M
重启mysql服务
3.创建存储过程
CREATE PROCEDURE pro_name(IN totalCount int)
begin
declare num int;
set num=1;
while num<totalCount do
set num=num+1;
INSERT INTO sys_area_memory (`code`, `name`, `parentId`, `level`, `type`, `servicestate`, `delete_flag`, `region`) VALUES (1, '北京市123123', 0, num, NULL, 1, 0, NULL);
end while;
END
- 执行存储过程语句 【CALL pro_name(10000000)】; 往内存表写入1千万数据,
- 将内存表数据复制到普通表数据中 sql: insert into sys_area_InnoDB SELECT * from sys_area_memory;
整个过程不到两分钟搞定。
Oracle 造数据
- 相对MySQL造数据没有这个复杂,直接调用存储过程即可;
- CALL pro_name(10000000) 整个过程也是不到两分钟完成
小结
- mysql造数据需要利用内存表完成。
- Oracle 造数据不需要中间表更加方便。