• 技术文章 >数据库 >mysql教程

    MySql批量插入性能优化

    2016-06-07 14:57:27原创823

    MySql批量插入性能优化方案: 1.一条SQL语句插入多条数据。 2.在事务中进行插入处理。 3.数据有序插入。(根据索引顺序插入) 注意事项: 1.SQL语句是有长度限制,在进行数据合并在同一SQL中务必不能超过SQL长度限制,通过max_allowed_packet配置可以修改,默认

    MySql批量插入性能优化方案:
    1. 一条SQL语句插入多条数据。
    2. 在事务中进行插入处理。
    3. 数据有序插入。(根据索引顺序插入)

    注意事项:
    1. SQL语句是有长度限制,在进行数据合并在同一SQL中务必不能超过SQL长度限制,通过max_allowed_packet配置可以修改,默认是1M,测试时修改为8M。
    2. 事务需要控制大小,事务太大可能会影响执行的效率。MySQL有innodb_log_buffer_size配置项,超过这个值会把innodb的数据刷到磁盘中,这时,效率会有所下降。所以比较好的做法是,在数据达到这个这个值前进行事务提交。 <无>

    源码与演示:源码出处

    INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`) 
        VALUES ('0', 'userid_0', 'content_0', 0);
    INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`) 
        VALUES ('1', 'userid_1', 'content_1', 1);
    改成:
    INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`) 
        VALUES ('0', 'userid_0', 'content_0', 0), ('1', 'userid_1', 'content_1', 1);
    START TRANSACTION;
    INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`) 
        VALUES ('0', 'userid_0', 'content_0', 0);
    INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`) 
        VALUES ('1', 'userid_1', 'content_1', 1);
    ...
    COMMIT;
    INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`) 
        VALUES ('1', 'userid_1', 'content_1', 1);
    INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`) 
        VALUES ('0', 'userid_0', 'content_0', 0);
    INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`) 
        VALUES ('2', 'userid_2', 'content_2',2);
    
    改成:
    INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`) 
        VALUES ('0', 'userid_0', 'content_0', 0);
    INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`) 
        VALUES ('1', 'userid_1', 'content_1', 1);
    INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`) 
        VALUES ('2', 'userid_2', 'content_2',2);

    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:根据表的主键字段和数据字段从其它数据库同步相同表的数据 下一篇:自己动手写 PHP MVC 框架(40节精讲/巨细/新人进阶必看)

    相关文章推荐

    • mysql数据库的超级管理员名称是什么• mysql怎么连接数据库• count(*)为什么很慢?原因分析• 聊聊怎么用MySQL快速实现一个推荐算法• mysql事务隔离级别有哪些
    1/1

    PHP中文网