mysql中insert into select语句测试
2019/10/10/17:37:48 阅读:1978 来源:谷歌SEO算法 标签:
友情链接作弊
mysql中insert into select语句是把指定表数据复制到我们新表中去,这个就是在insert into 的基础加了一个select功能了,下面来看一些测试的例子.
mysql迅速制造大批数据,复制一个表中的(部分或全部)数据到另一个表中.
用法:INSERT INTO table_name1 (field1,field2) SELECT field1,field2 FROM table_name2;
前提条件,代码如下:
- MySQL
- CREATETABLE`user`(
- `id`int(10)NOTNULLAUTO_INCREMENT,
- `username`varchar(30)NOTNULL,
- `password`char(32)NOTNULL,
- PRIMARYKEY(`id`)
- )ENGINE=InnoDBDEFAULTCHARSET=utf8;
- CREATETABLE`user_his`(
- `his_id`int(10)NOTNULLAUTO_INCREMENT,
- `id`int(10)NOTNULL,
- `username`varchar(30)NOTNULL,
- `password`char(32)NOTNULL,
- PRIMARYKEY(`his_id`)
- )ENGINE=InnoDBDEFAULTCHARSET=utf8;
查看一下结构,更直观,代码如下:
- mysql>descuser;
- +----------+-------------+------+-----+---------+----------------+
- |Field|Type|Null|Key|Default|Extra|
- +----------+-------------+------+-----+---------+----------------+
- |id|int(10)|NO|PRI|NULL|auto_increment|
- |username|varchar(30)|NO||NULL||
- |password|char(32)|NO||NULL||
- +----------+-------------+------+-----+---------+----------------+
- 3rowsinset(0.00sec)
- mysql>descuser_his;
- +----------+-------------+------+-----+---------+----------------+
- |Field|Type|Null|Key|Default|Extra|
- +----------+-------------+------+-----+---------+----------------+
- |his_id|int(10)|NO|PRI|NULL|auto_increment|
- |id|int(10)|NO||NULL||
- |username|varchar(30)|NO||NULL||
- |password|char(32)|NO||NULL||
- +----------+-------------+------+-----+---------+----------------+
- 4rowsinset(0.01sec)
插入原始数据,MySQL
- mysql>INSERTINTO`user`(`username`,`password`)VALUES('hello','123456'),('twitter','123456'),('baidu','123456'),('google','123456'),('facebook','123456'),('linux','123456'),('cisco','123456'),('huawei','123456'),('lenovo','123456'),('apple','123456'),('oracle','123456'),('sun','123456');
复制数据到历史表,MySQL
mysql> INSERT INTO `user_his`(`id`,`username`,`password`) select `id`,`username`,`password` from `user`;//phpfensi.com
附加mysql大批量复制数据,时间变化:在自己的电脑上测试(Ubuntu14.04 LTS 64位 + xampp),前3000条数据速度比较快,3000条以后执行时间成倍增加,2万5千条数据执行时间1分钟,314万数据,两分29秒,代码如下:
- mysql>insertintouser(username,password)selectusername,passwordfromuser;
- QueryOK,12rowsaffected,1warning(0.07sec)
- Records:12Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;
- QueryOK,24rowsaffected,1warning(0.08sec)
- Records:24Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;
- QueryOK,48rowsaffected,1warning(0.11sec)
- Records:48Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;
- QueryOK,96rowsaffected,1warning(0.10sec)
- Records:96Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;
- QueryOK,192rowsaffected,1warning(0.10sec)
- Records:192Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;
- QueryOK,384rowsaffected,1warning(0.10sec)
- Records:384Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;\
- QueryOK,768rowsaffected,1warning(0.13sec)
- Records:768Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;\
- QueryOK,1536rowsaffected,1warning(0.15sec)
- Records:1536Duplicates:0Warnings:1
- mysql>
- mysql>insertintouser(username,password)selectusername,passwordfromuser;\
- QueryOK,3072rowsaffected,1warning(0.17sec)
- Records:3072Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;\
- QueryOK,6144rowsaffected,1warning(0.28sec)
- Records:6144Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;\
- QueryOK,12288rowsaffected,1warning(0.42sec)
- Records:12288Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;\
- QueryOK,24576rowsaffected,1warning(0.99sec)
- Records:24576Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;\
- QueryOK,49152rowsaffected,1warning(1.98sec)
- Records:49152Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;\
- QueryOK,98304rowsaffected,1warning(4.04sec)
- Records:98304Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;\
- QueryOK,196608rowsaffected,1warning(8.89sec)
- Records:196608Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;\
- QueryOK,393216rowsaffected,1warning(17.14sec)
- Records:393216Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;\
- QueryOK,786432rowsaffected,1warning(38.07sec)
- Records:786432Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;\
- QueryOK,1572864rowsaffected,1warning(1min11.91sec)
- Records:1572864Duplicates:0Warnings:1
- mysql>insertintouser(username,password)selectusername,passwordfromuser;\//开源代码phpfensi.com
- QueryOK,3145728rowsaffected,1warning(2min29.04sec)
- Records:3145728Duplicates:0Warnings:1
热门评论