`
snoopy7713
  • 浏览: 1140180 次
  • 性别: Icon_minigender_2
  • 来自: 火星郊区
博客专栏
Group-logo
OSGi
浏览量:0
社区版块
存档分类
最新评论

MYSQL批量插入数据最佳实践(MYISAM,InnoDB)

阅读更多

在使用MYSQL批量插入数据时我们一般可选的方式有:


   * 长SQL  insert into table(xx,xx) values(xx,xx),(xx,xx)

   * 或者执行多个单条 INSERT INTO table(xx,xx) VALUES (xx,xx);语句

   * 使用 LOAD DATA INFILE(导入)是SELECT...INTO OUTFILE(导出)

   * 使用 insert into ...select... 等


   但在使用这些方式之前,你可能得考虑一下执行速度问题。因为在MYSQL中的MyISAM与InnoDB引擎在读写操作执行效率有所不同。

   至于MyISAM与InnoDB的区别请看:

Advantages of InnoDB

InnoDB should be used where data integrity comes a priority because it inherently takes care of them by the help of relationship constraints and transactions.

Faster in write-intensive (inserts, updates) tables because it utilizes row-level locking and only hold up changes to the same row that’s being inserted or updated.

Disadvantages of InnoDB

Because InnoDB has to take care of the different relationships between tables, database administrator and scheme creators have to take more time in designing the data models which are more complex than those of MyISAM.

Consumes more system resources such as RAM. As a matter of fact, it is recommended by many that InnoDB engine be turned off if there’s no substantial need for it after installation of MySQL.

No full-text indexing .

 

Advantages of MyISAM

Simpler to design and create , thus better for beginners. No worries about the foreign relationships between tables.

Faster than InnoDB on the whole as a result of the simpler structure thus much less costs of server resources.

Full-text indexing .

Especially good for read-intensive (select) tables .

Disadvantages of MyISAM

No data integrity (e.g. relationship constraints) check, which then comes a responsibility and overhead of the database administrators and application developers.

Doesn’t support transactions which is essential in critical data applications such as that of banking.

Slower than InnoDB for tables that are frequently being inserted to or updated, because the entire table is locked for any insert or update.

完整原文: http://www.kavoir.com/2009/09/mysql-engines-innodb-vs-myisam-a-comparison-of-pros-and-cons.html

 

 在使用InnoDB引擎批量插入数据时有几点是要说明的:

  InnoDB批量插入最佳方式:

    * When importing data into InnoDB, make sure that MySQL does not have autocommit mode enabled because that

      requires a log flush to disk for every insert. To disable autocommit during your import operation, surround it with

      SET autocommit and COMMIT statements:

      SET autocommit=0;
      ... SQL import statements ...
      COMMIT;

      If you use the mysqldump option --opt, you get dump files that are fast to import into an InnoDB table,
      even without wrapping them with the SET autocommit and COMMIT statements.


   * If you have UNIQUE constraints on secondary keys, you can speed up table imports by temporarily turning off 
     the uniqueness checks during the import session:

      SET unique_checks=0;
      ... SQL import statements ...
      SET unique_checks=1;

      For big tables, this saves a lot of disk I/O because InnoDB can use its insert buffer to write secondary index 
      records in a batch. Be certain that the data contains no duplicate keys.


   * If you have FOREIGN KEY constraints in your tables, you can speed up table imports by turning the foreign key checks 
     off for the duration of the import session:

      SET foreign_key_checks=0;
      ... SQL import statements ...
      SET foreign_key_checks=1;

      For big tables, this can save a lot of disk I/O.

完整的InnoDB性能设置请看:http://dev.mysql.com/doc/refman/5.0/en/innodb-tuning.html

 

另外:如何优化INSERT语句速度的因素请看: http://dev.mysql.com/doc/refman/5.1/zh/optimization.html#insert-speed

 

‍以上方式我这里就不给出具体实例了。

分享到:
评论

相关推荐

    mysql大批量数据插入

    MySQL 提供了多种方法来实现大批量数据插入,包括使用批量插入语句、使用 LOAD DATA 语句、使用 MySQL 的 Bulk Insert 机制等。 在 MySQL 中,使用批量插入语句可以大大提高数据插入的速度。批量插入语句的格式如下...

    数据库引擎 MyISAM 和 InnoDB 对比

    - **MyISAM**在批量插入数据时通常更快。 - **InnoDB**支持事务处理,在处理包含多个INSERT语句的复杂事务时更为安全可靠。 9. **合并操作**: - **MyISAM**支持合并多个表为一个大表,这在数据汇总时非常有用。...

    java 下执行mysql 批量插入的几种方法及用时

    在Java中,执行MySQL批量插入数据有多种方法,每种方法在性能上都有所不同。以下是对这些方法的详细分析: 方法1:单条插入 这是最基础的插入方式,每次循环都创建一个新的SQL语句并执行。这种方法的效率最低,因为...

    提高mysql插入数据的速度.pdf

    1. 利用MySQL的批量插入功能: - 在插入大量数据时,可以将多个INSERT语句合并在一起使用,以减少I/O操作的次数。例如,可以在一个INSERT语句中一次性插入多行数据,而不是一条一条地插入。使用“INSERT INTO table...

    mysql循环添加数据(存储详细)

    本文通过具体的示例代码详细介绍了如何在MySQL中利用存储过程实现循环添加数据,并对比分析了MyISAM与InnoDB两种存储引擎在执行批量数据插入时的性能差异。这些知识对于理解MySQL的工作原理以及在实际应用中选择合适...

    提高mysql插入数据的速度归类.pdf

    2. **增大 `bulk_insert_buffer_size`**:增加此参数的值可允许 MySQL 在内存中缓存更多批量插入的数据,从而提高写入速度。 3. **禁用与启用索引**:对于非空的 MyISAM 表,在导入数据前使用 `ALTER TABLE table_...

    MySQL性能优化的21个最佳实践.zip

    13. **批量插入**:大量数据插入时,采用批量插入(multi-row INSERT)而非单条插入,以减少网络延迟和系统开销。 14. **定期分析与优化表**:使用ANALYZE TABLE和OPTIMIZE TABLE来更新统计信息和碎片整理。 15. *...

    300万条mysql测试数据

    3. **存储引擎选择**:MySQL支持多种存储引擎,如InnoDB(默认,支持事务和行级锁定)、MyISAM(速度快,不支持事务)等。测试数据能帮助我们决定在处理大规模数据时,哪种引擎更适合应用场景。 4. **性能监控**:...

    关于使用InnoDB

    2. **批量插入速度较低**:相较于MyISAM等其他存储引擎,InnoDB在执行大量数据插入操作时可能会慢一些。 #### 设置InnoDB为默认存储引擎 1. **检查当前支持的存储引擎**:首先需要确认MySQL是否支持InnoDB。可以...

    MySQL索引背后的数据结构及算法原理

    - **InnoDB的主键选择与插入优化**:选择合适的主键类型(如自增ID)和插入方式(如批量插入)可以进一步提升InnoDB表的性能。 #### 后记 通过对MySQL索引背后的数据结构和算法原理的深入理解,我们可以更好地优化...

    大数据量测试数据(MySQL)

    5. **批量插入与事务**:在导入大量数据时,使用批量INSERT语句或事务可以显著提高速度。 6. **资源监控**:观察CPU、内存和磁盘I/O使用情况,确保系统资源得到合理利用。 7. **性能日志**:启用慢查询日志,记录...

    mysql_+ssi框架

    2. SQL语言:用于与MySQL交互的语言,包括数据查询(SELECT)、数据插入(INSERT)、数据更新(UPDATE)和数据删除(DELETE)语句,以及数据库和表的创建、修改等操作。 3. 存储引擎:InnoDB和MyISAM是最常见的两个...

    mysql如何优化插入记录速度

    MySQL数据库在处理插入记录...以上是针对MySQL插入记录速度的一些常见优化策略,实践中应结合具体情况进行调整,以达到最佳性能。同时,监控系统资源使用情况,如CPU、内存和磁盘I/O,也是优化过程中不可或缺的部分。

    MySQL大佬姜承尧49完整课程笔记,进阶涨薪必看,内含MySQL配置文件

    - 加载数据优化:探讨批量插入、LOAD DATA INFILE等方法,提高数据导入效率。 3. **存储引擎** - InnoDB与MyISAM:对比分析两种主要的存储引擎,理解事务处理、行级锁定、全文搜索等功能。 - 存储引擎选择:根据...

    mysql面试题(2)

    本文将对 MySQL 面试题进行解析,涵盖 MySQL 存储引擎、批量插入、删除重复行、执行分页查询和备份单个表等问题。 一、MySQL 存储引擎 MySQL 存储引擎是负责处理表的创建、读取、更新和删除操作的组件。常见的 ...

    MySQL笔记(狂神说java)

    - 批量插入和更新可以提高效率,减少与数据库的交互次数。 - SQL查询优化,包括使用索引、避免全表扫描和子查询优化。 10. **备份与恢复**: - 备份数据库以防止数据丢失,包括全备、增量和差异备份。 - 使用...

Global site tag (gtag.js) - Google Analytics