最近想到创建一个大量数据的测试环境,于是找了一下怎么插入100W条数据,我用的是20个字段
对比一下,首先是用 mysql 的存储过程弄的:代码如下:
mysql>delimiter $
mysql>SET AUTOCOMMIT = 0$$
mysql> create procedure test()
begin
declare i decimal (10) default 0 ;
dd:loop
INSERT INTO `million` (`categ_id`, `categ_fid`, `SortPath`, `address`, `p_identifier`, `pro_specification`, `name`, `add_date`, `picture_url`, `thumb_url`, `is_display_front`, `create_html_time`, `hit`, `buy_sum`, `athor`, `templete _style`, `is_hot`, `is_new`, `is_best`) VALUES
(268, 2, '0,262,268,', 0, '2342', '423423', '123123', '2012-01-09 09:55:43', 'upload/product/20111205153432_53211.jpg', 'upload/product/thumb_20111205153432_53211.jpg', 1, 0, 0, 0, 'admin', '0', 0, 0, 0);
commit;
set i = i+1;
if i= 1000000 then leave dd;
end if;
end loop dd ;
end;$
mysql>delimiter ;
mysql> call test;
结果
mysql> call test; Query OK, 0 rows affected (58 min 30.83 sec)
非常耗时。
于是我又找了一个方法
先用PHP代码生成数据,再导入:
代码如下:
$t=mktime();
set_time_limit(1000);
$myFile="e:/insert.sql";
$fhandler=fopen($myFile,'wb');
if($fhandler){
$sql="268\t2\t'0,262,268,'\t0\t '2342'\t'423423'\t'123123'\t'23423423'\t'2012-01-09 09:55:43'\t'upload/product/20111205153432_53211.jpg'\t'upload/product/thumb_20111205153432_53211.jpg'\tNULL\tNULL\t38\t'件'\t''\t123\t123\t0";
$i=0;
while($i{
$i++;
fwrite($fhandler,$sql."\r\n");
}
echo"写入成功,耗时:",mktime()-$t;
}
然后再导入
代码如下:
LOAD DATA local INFILE 'e:/insert.sql' INTO TABLE tenmillion(`categ_id`, `categ_fid`, `SortPath`, `address`, `p_identifier`, `pro_specification`, `name`, `description`, `add_date`, `picture_url`, `thumb_url`, `shop_url`, `shop_thumb_url`, `brand_id`, `unit`, `square_meters_unit`, `market_price`, `true_price`, `square_meters_price`);
注意字段不再以逗号分割,以\t分割,条记录以\r\n分割。结果我插入10次数据,100W平均只要1分钟搞定。
第二种方式mysql中间省略了很多中间步骤,导致插入速度远胜于第一种,具体的没有研究。
快速生成mysql上百万条测试数据
由于测试需要,原表中只有1万条数据,现在随机复制插入记录,快速达到100万条。
itemid是主键。
运行几次下面代码。随机取1000条插入,
insert into downitems (chid,catid,softid,....)
SELECT chid,catid,softid... FROM `downitems` WHERE itemid >= (SELECT floor(RAND() * (SELECT MAX(itemid) FROM `downitems`))) ORDER BY itemid LIMIT 1000;
然后可以修改1000的数字了。改为5000或者1万。很快可以达到100万的数据量了。

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Notepad++7.3.1
Easy-to-use and free code editor
