A-A+

mysql中insert into select语句测试

2017年10月16日 PHP技术文章 暂无评论 阅读 0 views 次

mysql中insert into select语句是把指定表数据复制到我们新表中去,这个就是在insert into 的基础加了一个select功能了,下面来看一些测试的例子.

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

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

前提条件,代码如下:

  1. MySQL   
  2. CREATE TABLE `user` (   
  3.   `id` int(10) NOT NULL AUTO_INCREMENT,   
  4.   `username` varchar(30) NOT NULL,   
  5.   `passwordchar(32) NOT NULL,   
  6.   PRIMARY KEY (`id`)   
  7. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;   
  8.    
  9. CREATE TABLE `user_his` (   
  10.   `his_id` int(10) NOT NULL AUTO_INCREMENT,   
  11.   `id` int(10) NOT NULL,   
  12.   `username` varchar(30) NOT NULL,   
  13.   `passwordchar(32) NOT NULL,   
  14.   PRIMARY KEY (`his_id`)   
  15. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;   

查看一下结构,更直观,代码如下:

  1. mysql> desc user;   
  2. +----------+-------------+------+-----+---------+----------------+   
  3. | Field    | Type        | Null | Key | Default | Extra          |   
  4. +----------+-------------+------+-----+---------+----------------+   
  5. | id       | int(10)     | NO   | PRI | NULL    | auto_increment |   
  6. | username | varchar(30) | NO   |     | NULL    |                |   
  7. password | char(32)    | NO   |     | NULL    |                |   
  8. +----------+-------------+------+-----+---------+----------------+   
  9. rows in set (0.00 sec)   
  10.    
  11. mysql> desc user_his;   
  12. +----------+-------------+------+-----+---------+----------------+   
  13. | Field    | Type        | Null | Key | Default | Extra          |   
  14. +----------+-------------+------+-----+---------+----------------+   
  15. | his_id   | int(10)     | NO   | PRI | NULL    | auto_increment |   
  16. | id       | int(10)     | NO   |     | NULL    |                |   
  17. | username | varchar(30) | NO   |     | NULL    |                |   
  18. password | char(32)    | NO   |     | NULL    |                |   
  19. +----------+-------------+------+-----+---------+----------------+   
  20. rows in set (0.01 sec)   

插入原始数据,MySQL

  1. 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

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秒,代码如下:

  1. mysql> insert into user(username,passwordselect username,password from user;   
  2. Query OK, 12 rows affected, 1 warning (0.07 sec)   
  3. Records: 12  Duplicates: 0  Warnings: 1   
  4. mysql> insert into user(username,passwordselect username,password from user;   
  5. Query OK, 24 rows affected, 1 warning (0.08 sec)   
  6. Records: 24  Duplicates: 0  Warnings: 1   
  7. mysql> insert into user(username,passwordselect username,password from user;   
  8. Query OK, 48 rows affected, 1 warning (0.11 sec)   
  9. Records: 48  Duplicates: 0  Warnings: 1   
  10. mysql> insert into user(username,passwordselect username,password from user;   
  11. Query OK, 96 rows affected, 1 warning (0.10 sec)   
  12. Records: 96  Duplicates: 0  Warnings: 1   
  13. mysql> insert into user(username,passwordselect username,password from user;   
  14. Query OK, 192 rows affected, 1 warning (0.10 sec)   
  15. Records: 192  Duplicates: 0  Warnings: 1   
  16. mysql> insert into user(username,passwordselect username,password from user;   
  17. Query OK, 384 rows affected, 1 warning (0.10 sec)   
  18. Records: 384  Duplicates: 0  Warnings: 1   
  19. mysql> insert into user(username,passwordselect username,password from user;\   
  20. Query OK, 768 rows affected, 1 warning (0.13 sec)   
  21. Records: 768  Duplicates: 0  Warnings: 1   
  22. mysql> insert into user(username,passwordselect username,password from user;\   
  23. Query OK, 1536 rows affected, 1 warning (0.15 sec)   
  24. Records: 1536  Duplicates: 0  Warnings: 1   
  25. mysql>   
  26. mysql> insert into user(username,passwordselect username,password from user;\   
  27. Query OK, 3072 rows affected, 1 warning (0.17 sec)   
  28. Records: 3072  Duplicates: 0  Warnings: 1   
  29. mysql> insert into user(username,passwordselect username,password from user;\   
  30. Query OK, 6144 rows affected, 1 warning (0.28 sec)   
  31. Records: 6144  Duplicates: 0  Warnings: 1   
  32. mysql> insert into user(username,passwordselect username,password from user;\   
  33. Query OK, 12288 rows affected, 1 warning (0.42 sec)   
  34. Records: 12288  Duplicates: 0  Warnings: 1   
  35. mysql> insert into user(username,passwordselect username,password from user;\   
  36. Query OK, 24576 rows affected, 1 warning (0.99 sec)   
  37. Records: 24576  Duplicates: 0  Warnings: 1   
  38. mysql> insert into user(username,passwordselect username,password from user;\   
  39. Query OK, 49152 rows affected, 1 warning (1.98 sec)   
  40. Records: 49152  Duplicates: 0  Warnings: 1   
  41. mysql> insert into user(username,passwordselect username,password from user;\   
  42. Query OK, 98304 rows affected, 1 warning (4.04 sec)   
  43. Records: 98304  Duplicates: 0  Warnings: 1   
  44. mysql> insert into user(username,passwordselect username,password from user;\   
  45. Query OK, 196608 rows affected, 1 warning (8.89 sec)   
  46. Records: 196608  Duplicates: 0  Warnings: 1   
  47. mysql> insert into user(username,passwordselect username,password from user;\   
  48. Query OK, 393216 rows affected, 1 warning (17.14 sec)   
  49. Records: 393216  Duplicates: 0  Warnings: 1   
  50. mysql> insert into user(username,passwordselect username,password from user;\   
  51. Query OK, 786432 rows affected, 1 warning (38.07 sec)   
  52. Records: 786432  Duplicates: 0  Warnings: 1   
  53. mysql> insert into user(username,passwordselect username,password from user;\   
  54. Query OK, 1572864 rows affected, 1 warning (1 min 11.91 sec)   
  55. Records: 1572864  Duplicates: 0  Warnings: 1   
  56. mysql> insert into user(username,passwordselect username,password from user;\//开源代码www.xiariboke.net   
  57. Query OK, 3145728 rows affected, 1 warning (2 min 29.04 sec)   
  58. Records: 3145728  Duplicates: 0  Warnings: 1  
标签:

给我留言