首页 > 代码库 > mysql 批量导入数据

mysql 批量导入数据

mysql 批量导入数据

    在我们使用数据库的过程中,避免不了备份、恢复、导入、导出数据,方法比较多,根据不同的数据量选择不同的方法,会提高我们的工作效率,

(1)从表A导入数据到表B;一般涉及到表结构相同或者表结构不同等情况,数据量少的时候效率很高,

测试表A为 send_done,创建语句为下面内容,
 

CREATE TABLE `send_done` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `phone` varchar(20) NOT NULL,
  `content` varchar(1000) NOT NULL,
  `intime` datetime NOT NULL,
  `state` int(11) NOT NULL,
  `sendtime` datetime DEFAULT NULL,
  `statcode` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `ind_state` (`state`)
) ENGINE=InnoDB AUTO_INCREMENT=1166482 DEFAULT CHARSET=utf8
CREATE TABLE `send_sf_done` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `phone` varchar(20) NOT NULL,
  `content` varchar(1000) NOT NULL,
  `intime` datetime NOT NULL,
  `state` int(11) NOT NULL,
  `sendtime` datetime DEFAULT NULL,
  `statcode` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `ind_state` (`state`)
) ENGINE=InnoDB AUTO_INCREMENT=1166482 DEFAULT CHARSET=utf8

表A的数据
MariaDB [wangjubao]> select  *  from  send_done;
+---------+-------------+--------------------------------+---------------------+-------+---------------------+---------------------+
| id      | phone       | content                        | intime              | state | sendtime            | statcode            |
+---------+-------------+--------------------------------+---------------------+-------+---------------------+---------------------+
| 1166459 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 10:33:02 |     1 | 2016-11-04 10:44:21 | 2114782562554393363 |
| 1166461 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 10:45:53 |    -3 | 2016-11-04 10:46:05 | -3                  |
| 1166463 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 10:47:42 |    -3 | 2016-11-04 10:47:54 | -3                  |
| 1166465 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 10:49:08 |    -3 | 2016-11-04 10:49:19 | -3                  |
| 1166467 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 10:55:33 |     1 | 2016-11-04 10:55:53 | 2114782569505307918 |
| 1166469 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 11:10:20 |    -3 | 2016-11-04 11:15:17 | -3                  |
| 1166471 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 11:23:16 |     1 | 2016-11-04 11:23:18 | 2114782586069773509 |
| 1166473 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 11:23:34 |     1 | 2016-11-04 11:23:35 | 2114782586234583070 |
| 1166475 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 11:23:34 |     1 | 2016-11-04 11:23:35 | 2114782586234583070 |
| 1166477 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 11:23:35 |     1 | 2016-11-04 11:23:36 | 2114782586244477897 |
| 1166479 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 11:23:35 |     1 | 2016-11-04 11:23:36 | 2114782586244477897 |
| 1166481 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 11:23:35 |     1 | 2016-11-04 11:23:36 | 2114782586244477897 |
+---------+-------------+--------------------------------+---------------------+-------+---------------------+---------------------+
1、从表A导入表B
 insert  into  send_done  select *  from send_sf_done; (字段全部一样时不用写字段)
 2、从表A导出文本加载到B
 导出数据
 select   *  from  send_done  into outfile "/tmp/winn.sql ";
 
 MariaDB [wangjubao]>  select   *  from  send_done  into outfile "/tmp/winn.sql "
    -> ;
Query OK, 12 rows affected (0.00 sec)
 检查导出文件
[root@slave1 tmp]# cat  winn.sql
1166459 15021970243     冬天到了【云信测试】    2016-11-04 10:33:02     1       2016-11-04 10:44:21     2114782562554393363
1166461 15021970243     冬天到了【云信测试】    2016-11-04 10:45:53     -3      2016-11-04 10:46:05     -3
1166463 15021970243     冬天到了【云信测试】    2016-11-04 10:47:42     -3      2016-11-04 10:47:54     -3
1166465 15021970243     冬天到了【云信测试】    2016-11-04 10:49:08     -3      2016-11-04 10:49:19     -3
1166467 15021970243     冬天到了【云信测试】    2016-11-04 10:55:33     1       2016-11-04 10:55:53     2114782569505307918
1166469 15021970243     冬天到了【云信测试】    2016-11-04 11:10:20     -3      2016-11-04 11:15:17     -3
1166471 15021970243     冬天到了【云信测试】    2016-11-04 11:23:16     1       2016-11-04 11:23:18     2114782586069773509
1166473 15021970243     冬天到了【云信测试】    2016-11-04 11:23:34     1       2016-11-04 11:23:35     2114782586234583070
1166475 15021970243     冬天到了【云信测试】    2016-11-04 11:23:34     1       2016-11-04 11:23:35     2114782586234583070
1166477 15021970243     冬天到了【云信测试】    2016-11-04 11:23:35     1       2016-11-04 11:23:36     2114782586244477897
1166479 15021970243     冬天到了【云信测试】    2016-11-04 11:23:35     1       2016-11-04 11:23:36     2114782586244477897
1166481 15021970243     冬天到了【云信测试】    2016-11-04 11:23:35     1       2016-11-04 11:23:36     2114782586244477897
 
删除之前从A表导入B表的数据,
MariaDB [wangjubao]> delete   from send_sf_done;
Query OK, 12 rows affected (0.00 sec)

MariaDB [wangjubao]> select  *  from  send_sf_done;
Empty set (0.00 sec)

加载导出的文件
load data infile ‘/tmp/winn.sql‘ into table  send_sf_done;

MariaDB [wangjubao]> load data infile ‘/tmp/winn.sql‘ into table  send_sf_done;
Query OK, 12 rows affected (0.00 sec)               
Records: 12  Deleted: 0  Skipped: 0  Warnings: 0

MariaDB [wangjubao]> select  *  from  send_sf_done;
+---------+-------------+--------------------------------+---------------------+-------+---------------------+---------------------+
| id      | phone       | content                        | intime              | state | sendtime            | statcode            |
+---------+-------------+--------------------------------+---------------------+-------+---------------------+---------------------+
| 1166459 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 10:33:02 |     1 | 2016-11-04 10:44:21 | 2114782562554393363 |
| 1166461 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 10:45:53 |    -3 | 2016-11-04 10:46:05 | -3                  |
| 1166463 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 10:47:42 |    -3 | 2016-11-04 10:47:54 | -3                  |
| 1166465 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 10:49:08 |    -3 | 2016-11-04 10:49:19 | -3                  |
| 1166467 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 10:55:33 |     1 | 2016-11-04 10:55:53 | 2114782569505307918 |
| 1166469 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 11:10:20 |    -3 | 2016-11-04 11:15:17 | -3                  |
| 1166471 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 11:23:16 |     1 | 2016-11-04 11:23:18 | 2114782586069773509 |
| 1166473 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 11:23:34 |     1 | 2016-11-04 11:23:35 | 2114782586234583070 |
| 1166475 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 11:23:34 |     1 | 2016-11-04 11:23:35 | 2114782586234583070 |
| 1166477 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 11:23:35 |     1 | 2016-11-04 11:23:36 | 2114782586244477897 |
| 1166479 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 11:23:35 |     1 | 2016-11-04 11:23:36 | 2114782586244477897 |
| 1166481 | 15021970243 | 冬天到了【云信测试】           | 2016-11-04 11:23:35 |     1 | 2016-11-04 11:23:36 | 2114782586244477897 |
+---------+-------------+--------------------------------+---------------------+-------+---------------------+---------------------+

方法3 使用select 导出数据
  mysql  -u  root  -p  -e  "select  *  from wangjubao.send_done;" >/tmp/winner2.sql
检查数据
[root@slave1 tmp]# cat  winner2.sql
id      phone   content intime  state   sendtime        statcode
1166459 15021970243     冬天到了【云信测试】    2016-11-04 10:33:02     1       2016-11-04 10:44:21     2114782562554393363
1166461 15021970243     冬天到了【云信测试】    2016-11-04 10:45:53     -3      2016-11-04 10:46:05     -3
1166463 15021970243     冬天到了【云信测试】    2016-11-04 10:47:42     -3      2016-11-04 10:47:54     -3
1166465 15021970243     冬天到了【云信测试】    2016-11-04 10:49:08     -3      2016-11-04 10:49:19     -3
1166467 15021970243     冬天到了【云信测试】    2016-11-04 10:55:33     1       2016-11-04 10:55:53     2114782569505307918
1166469 15021970243     冬天到了【云信测试】    2016-11-04 11:10:20     -3      2016-11-04 11:15:17     -3
1166471 15021970243     冬天到了【云信测试】    2016-11-04 11:23:16     1       2016-11-04 11:23:18     2114782586069773509
1166473 15021970243     冬天到了【云信测试】    2016-11-04 11:23:34     1       2016-11-04 11:23:35     2114782586234583070
1166475 15021970243     冬天到了【云信测试】    2016-11-04 11:23:34     1       2016-11-04 11:23:35     2114782586234583070
1166477 15021970243     冬天到了【云信测试】    2016-11-04 11:23:35     1       2016-11-04 11:23:36     2114782586244477897
1166479 15021970243     冬天到了【云信测试】    2016-11-04 11:23:35     1       2016-11-04 11:23:36     2114782586244477897
1166481 15021970243     冬天到了【云信测试】    2016-11-04 11:23:35     1       2016-11-04 11:23:36     2114782586244477897
处理第一行
sed   -i  "1,1d"  /tmp/winner2.sq
处理结果
1166459 15021970243     冬天到了【云信测试】    2016-11-04 10:33:02     1       2016-11-04 10:44:21     2114782562554393363
1166461 15021970243     冬天到了【云信测试】    2016-11-04 10:45:53     -3      2016-11-04 10:46:05     -3
1166463 15021970243     冬天到了【云信测试】    2016-11-04 10:47:42     -3      2016-11-04 10:47:54     -3
1166465 15021970243     冬天到了【云信测试】    2016-11-04 10:49:08     -3      2016-11-04 10:49:19     -3
1166467 15021970243     冬天到了【云信测试】    2016-11-04 10:55:33     1       2016-11-04 10:55:53     2114782569505307918
1166469 15021970243     冬天到了【云信测试】    2016-11-04 11:10:20     -3      2016-11-04 11:15:17     -3
1166471 15021970243     冬天到了【云信测试】    2016-11-04 11:23:16     1       2016-11-04 11:23:18     2114782586069773509
1166473 15021970243     冬天到了【云信测试】    2016-11-04 11:23:34     1       2016-11-04 11:23:35     2114782586234583070
1166475 15021970243     冬天到了【云信测试】    2016-11-04 11:23:34     1       2016-11-04 11:23:35     2114782586234583070
1166477 15021970243     冬天到了【云信测试】    2016-11-04 11:23:35     1       2016-11-04 11:23:36     2114782586244477897
1166479 15021970243     冬天到了【云信测试】    2016-11-04 11:23:35     1       2016-11-04 11:23:36     2114782586244477897
1166481 15021970243     冬天到了【云信测试】    2016-11-04 11:23:35     1       2016-11-04 11:23:36     2114782586244477897
加载数据
   

 

mysql 批量导入数据