首页 数据库 mysql教程 简单记录mysql中inser into select语句测试_MySQL

简单记录mysql中inser into select语句测试_MySQL

May 31, 2016 am 08:48 AM

mysql迅速制造大批数据,复制一个表中的(部分或全部)数据到另一个表中。

用法:INSERT INTO table_name1 (field1,field2) SELECT field1,field2 FROM table_name2;

前提条件

CREATE TABLE `user` (`id` int(10) NOT NULL AUTO_INCREMENT,`username` varchar(30) NOT NULL,`password` char(32) NOT NULL,PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;CREATE TABLE `user_his` (`his_id` int(10) NOT NULL AUTO_INCREMENT,`id` int(10) NOT NULL,`username` varchar(30) NOT NULL,`password` char(32) NOT NULL,PRIMARY KEY (`his_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;
登录后复制

CREATETABLE`user`(

  `id`int(10)NOT NULLAUTO_INCREMENT,

  `username`varchar(30)NOT NULL,

  `password`char(32)NOT NULL,

  PRIMARY KEY(`id`)

)ENGINE=InnoDBDEFAULTCHARSET=utf8;

CREATETABLE`user_his`(

  `his_id`int(10)NOT NULLAUTO_INCREMENT,

  `id`int(10)NOT NULL,

  `username`varchar(30)NOT NULL,

  `password`char(32)NOT NULL,

  PRIMARY KEY(`his_id`)

)ENGINE=InnoDBDEFAULTCHARSET=utf8;

CREATETABLE`user`(
mysql> desc user;+----------+-------------+------+-----+---------+----------------+| Field| Type| Null | Key | Default | Extra|+----------+-------------+------+-----+---------+----------------+| id | int(10) | NO | PRI | NULL| auto_increment || username | varchar(30) | NO | | NULL||| password | char(32)| NO | | NULL||+----------+-------------+------+-----+---------+----------------+3 rows in set (0.00 sec)mysql> desc user_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||+----------+-------------+------+-----+---------+----------------+4 rows in set (0.01 sec)
登录后复制

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)

  `id`int(10)NOT NULLAUTO_INCREMENT,

  `username`varchar(30)NOT NULL,
mysql> INSERT INTO `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>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');

  `password`char(32)NOT NULL,

  PRIMARY KEY(`id`)
mysql> INSERT INTO `user_his`(`id`,`username`,`password`) select `id`,`username`,`password` from `user`;
登录后复制

mysql>INSERTINTO`user_his`(`id`,`username`,`password`)select`id`,`username`,`password`from`user`;

)ENGINE=InnoDBDEFAULTCHARSET=utf8;CREATETABLE`user_his`(  `his_id`int(10)NOT NULLAUTO_INCREMENT,  `id`int(10)NOT NULL,  `username`varchar(30)NOT NULL,  `password`char(32)NOT NULL,  PRIMARY KEY(`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>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>INSERTINTO`user_his`(`id`,`username`,`password`)select`id`,`username`,`password`from`user`;

附加mysql大批量复制数据,时间变化:

在自己的电脑上测试(Ubuntu14.04 LTS 64位 xampp),前3000条数据速度比较快,3000条以后执行时间成倍增加,2万5千条数据执行时间1分钟。314万数据,两分29秒

mysql> insert into user(username,password) select username,password from user;

Query OK, 12 rows affected, 1 warning (0.07 sec)

Records: 12  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;

Query OK, 24 rows affected, 1 warning (0.08 sec)

Records: 24  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;

Query OK, 48 rows affected, 1 warning (0.11 sec)

Records: 48  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;

Query OK, 96 rows affected, 1 warning (0.10 sec)

Records: 96  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;

Query OK, 192 rows affected, 1 warning (0.10 sec)

Records: 192  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;

Query OK, 384 rows affected, 1 warning (0.10 sec)

Records: 384  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;/

Query OK, 768 rows affected, 1 warning (0.13 sec)

Records: 768  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;/

Query OK, 1536 rows affected, 1 warning (0.15 sec)Records: 1536  Duplicates: 0  Warnings: 1

mysql>

mysql> insert into user(username,password) select username,password from user;/

Query OK, 3072 rows affected, 1 warning (0.17 sec)

Records: 3072  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;/

Query OK, 6144 rows affected, 1 warning (0.28 sec)

Records: 6144  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;/

Query OK, 12288 rows affected, 1 warning (0.42 sec)

Records: 12288  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;/

Query OK, 24576 rows affected, 1 warning (0.99 sec)

Records: 24576  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;/

Query OK, 49152 rows affected, 1 warning (1.98 sec)

Records: 49152  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;/

Query OK, 98304 rows affected, 1 warning (4.04 sec)

Records: 98304  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;/

Query OK, 196608 rows affected, 1 warning (8.89 sec)

Records: 196608  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;/

Query OK, 393216 rows affected, 1 warning (17.14 sec)

Records: 393216  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;/

Query OK, 786432 rows affected, 1 warning (38.07 sec)

Records: 786432  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;/

Query OK, 1572864 rows affected, 1 warning (1 min 11.91 sec)

Records: 1572864  Duplicates: 0  Warnings: 1

mysql> insert into user(username,password) select username,password from user;/

Query OK, 3145728 rows affected, 1 warning (2 min 29.04 sec)

Records: 3145728  Duplicates: 0  Warnings: 1

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

与MySQL中使用索引相比,全表扫描何时可以更快? 与MySQL中使用索引相比,全表扫描何时可以更快? Apr 09, 2025 am 12:05 AM

全表扫描在MySQL中可能比使用索引更快,具体情况包括:1)数据量较小时;2)查询返回大量数据时;3)索引列不具备高选择性时;4)复杂查询时。通过分析查询计划、优化索引、避免过度索引和定期维护表,可以在实际应用中做出最优选择。

说明InnoDB全文搜索功能。 说明InnoDB全文搜索功能。 Apr 02, 2025 pm 06:09 PM

InnoDB的全文搜索功能非常强大,能够显着提高数据库查询效率和处理大量文本数据的能力。 1)InnoDB通过倒排索引实现全文搜索,支持基本和高级搜索查询。 2)使用MATCH和AGAINST关键字进行搜索,支持布尔模式和短语搜索。 3)优化方法包括使用分词技术、定期重建索引和调整缓存大小,以提升性能和准确性。

可以在 Windows 7 上安装 mysql 吗 可以在 Windows 7 上安装 mysql 吗 Apr 08, 2025 pm 03:21 PM

是的,可以在 Windows 7 上安装 MySQL,虽然微软已停止支持 Windows 7,但 MySQL 仍兼容它。不过,安装过程中需要注意以下几点:下载适用于 Windows 的 MySQL 安装程序。选择合适的 MySQL 版本(社区版或企业版)。安装过程中选择适当的安装目录和字符集。设置 root 用户密码,并妥善保管。连接数据库进行测试。注意 Windows 7 上的兼容性问题和安全性问题,建议升级到受支持的操作系统。

mysql:简单的概念,用于轻松学习 mysql:简单的概念,用于轻松学习 Apr 10, 2025 am 09:29 AM

MySQL是一个开源的关系型数据库管理系统。1)创建数据库和表:使用CREATEDATABASE和CREATETABLE命令。2)基本操作:INSERT、UPDATE、DELETE和SELECT。3)高级操作:JOIN、子查询和事务处理。4)调试技巧:检查语法、数据类型和权限。5)优化建议:使用索引、避免SELECT*和使用事务。

InnoDB中的聚类索引和非簇索引(次级索引)之间的差异。 InnoDB中的聚类索引和非簇索引(次级索引)之间的差异。 Apr 02, 2025 pm 06:25 PM

聚集索引和非聚集索引的区别在于:1.聚集索引将数据行存储在索引结构中,适合按主键查询和范围查询。2.非聚集索引存储索引键值和数据行的指针,适用于非主键列查询。

mysql用户和数据库的关系 mysql用户和数据库的关系 Apr 08, 2025 pm 07:15 PM

MySQL 数据库中,用户和数据库的关系通过权限和表定义。用户拥有用户名和密码,用于访问数据库。权限通过 GRANT 命令授予,而表由 CREATE TABLE 命令创建。要建立用户和数据库之间的关系,需创建数据库、创建用户,然后授予权限。

说明不同类型的MySQL索引(B树,哈希,全文,空间)。 说明不同类型的MySQL索引(B树,哈希,全文,空间)。 Apr 02, 2025 pm 07:05 PM

MySQL支持四种索引类型:B-Tree、Hash、Full-text和Spatial。1.B-Tree索引适用于等值查找、范围查询和排序。2.Hash索引适用于等值查找,但不支持范围查询和排序。3.Full-text索引用于全文搜索,适合处理大量文本数据。4.Spatial索引用于地理空间数据查询,适用于GIS应用。

mysql 和 mariadb 可以共存吗 mysql 和 mariadb 可以共存吗 Apr 08, 2025 pm 02:27 PM

MySQL 和 MariaDB 可以共存,但需要谨慎配置。关键在于为每个数据库分配不同的端口号和数据目录,并调整内存分配和缓存大小等参数。连接池、应用程序配置和版本差异也需要考虑,需要仔细测试和规划以避免陷阱。在资源有限的情况下,同时运行两个数据库可能会导致性能问题。

See all articles