快上网专注成都网站设计 成都网站制作 成都网站建设
成都网站建设公司服务热线:028-86922220

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

MySQL怎么批量导入数据优化

这篇文章将为大家详细讲解有关MySQL怎么批量导入数据优化,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

创新互联公司长期为上千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为张掖企业提供专业的成都做网站、网站设计,张掖网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。

--MyISAM表
mysql> show create table test\G
*************************** 1. row ***************************
       Table: test
Create Table: CREATE TABLE `test` (
  `id` int(11) NOT NULL,
  `last_name` char(30) NOT NULL,
  `first_name` char(30) NOT NULL,
  PRIMARY KEY (`id`),
  KEY `name` (`last_name`,`first_name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)

mysql> show keys from test;
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| test  |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |
| test  |          1 | name     |            1 | last_name   | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
| test  |          1 | name     |            2 | first_name  | A         |        NULL |     NULL | NULL   |      | BTREE      |         |               |
+-------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
3 rows in set (0.00 sec)

mysql> alter table test disable keys;
Query OK, 0 rows affected (0.00 sec)

mysql>  load data infile '/var/lib/mysql-files/test_out.txt' into table test charset gbk fields terminated by ',' enclosed by '"';
Query OK, 5 rows affected (0.02 sec)
Records: 5  Deleted: 0  Skipped: 0  Warnings: 0

mysql> alter table test enable keys;
Query OK, 0 rows affected (0.00 sec)

--InnoDB表
导入的数据按照主键的顺序排列;

将unique_checks参数置为0;
mysql> show variables like '%unique%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| unique_checks | ON    |
+---------------+-------+
1 row in set (0.01 sec)

mysql> set unique_checks = 0;
Query OK, 0 rows affected (0.10 sec)

mysql> show variables like '%unique%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| unique_checks | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

mysql>  load data infile '/var/lib/mysql-files/test_out.txt' into table test charset gbk fields terminated by ',' enclosed by '"';
Query OK, 5 rows affected (0.00 sec)
Records: 5  Deleted: 0  Skipped: 0  Warnings: 0

将autocommit参数设为0

mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | ON    |
+---------------+-------+
1 row in set (0.00 sec)

mysql> set autocommit = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> show variables like 'autocommit';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit    | OFF   |
+---------------+-------+
1 row in set (0.00 sec)

mysql> load data infile '/var/lib/mysql-files/test_out.txt' into table test charset gbk fields terminated by ',' enclosed by '"';
Query OK, 5 rows affected (0.00 sec)
Records: 5  Deleted: 0  Skipped: 0  Warnings: 0

关于MySQL怎么批量导入数据优化就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。


网站标题:MySQL怎么批量导入数据优化
分享网址:http://6mz.cn/article/ijpdsd.html

其他资讯