MySQL 批量匯入資料優化
--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> 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
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26506993/viewspace-2121909/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Redis批量匯入文字資料Redis
- Python批量匯入Excel資料到MySQLPythonExcelMySql
- PHP大資料xlswriter匯入匯出(最優資料化)PHP大資料
- sqlldr批量匯入匯出資料測試SQL
- mysql資料匯入匯出MySql
- mysql資料匯出匯入MySql
- Java:匯出Excel大批量資料的優化過程JavaExcel優化
- 資料庫 MySQL 資料匯入匯出資料庫MySql
- elasticsearch bulk資料--ES批量匯入json資料ElasticsearchJSON
- mysql 資料庫匯入匯出MySql資料庫
- MySQL資料庫匯入匯出MySql資料庫
- .NET Core使用NPOI將Excel中的資料批量匯入到MySQLExcelMySql
- MySQL入門--匯出和匯入資料MySql
- MySQL資料的匯入MySql
- Elasticsearch批量匯入資料指令碼(python)Elasticsearch指令碼Python
- Oracle:從SQL檔案批量匯入資料OracleSQL
- SQL Server Bulk Insert批量資料匯入SQLServer
- 使用csv批量匯入、匯出資料的需求處理
- 【mysql】資料庫匯出和匯入MySql資料庫
- mysql匯入匯出.csv格式資料MySql
- mysqldump匯入匯出mysql資料庫MySql資料庫
- MySQL表資料匯入與匯出MySql
- Mysql 資料庫匯入與匯出MySql資料庫
- Redis資料匯入工具優化過程總結Redis優化
- MYSQL資料檔案匯入MySql
- ORACLE MYSQL互相匯入資料OracleMySql
- MSSQL資料批量插入優化詳細SQL優化
- MySQL資料匯入匯出牛刀小試MySql
- 將Excel檔案匯入資料庫(POI+Excel+MySQL+jsp頁面匯入)第一次優化Excel資料庫MySqlJS優化
- Windows 下 MySQL 資料匯入 RedisWindowsMySqlRedis
- MySQL匯入百萬資料實踐MySql
- Mysql匯入資料相當慢MySql
- MySQL資料匯入匯出亂碼問題MySql
- MySQL資料庫優化MySql資料庫優化
- 批量備份還原匯入與匯出MongoDB資料方式昝璽MongoDB
- 【MySQL】白話說MySQL(五),資料的匯出與匯入MySql
- MySQL資料匯入匯出之Load data fileMySql
- MySQL資料匯入匯出方法與工具介紹MySql