Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2772301
  • 博文数量: 389
  • 博客积分: 4177
  • 博客等级: 上校
  • 技术积分: 4773
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-16 23:29
文章分类

全部博文(389)

分类: Mysql/postgreSQL

2014-05-03 20:53:51

                                     MySQL的多行插入

     由于mysql的autocommit默认为打开的,而且很多生产环境都是设置为在事务提交时
需要写磁盘,所以提交产生的io开销非常大。在繁忙的oltp系统中,可能这是主要的性能
瓶劲.因此减少提交的次数非常重要,尽可能采用批量提交的方式而不是使用单次提交的.


   mysql的insert语句本可以支持一次多行insert.这种方式在其他的数据库中没有
比如oracle.


测试开始前

mysql> show status like '%commit%' ;
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| Com_commit     | 0     |
| Com_xa_commit  | 0     |
| Handler_commit | 0     |
+----------------+-------+
3 rows in set (0.00 sec)


使用多行insert。
mysql>  insert into t1(a)        
    ->            values (1),   
    ->                   (2);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0


数据库只有提交一次.
mysql> show status like '%commit%' ;
+----------------+-------+
| Variable_name  | Value |
+----------------+-------+
| Com_commit     | 0     |
| Com_xa_commit  | 0     |
| Handler_commit | 1     |
+----------------+-------+
3 rows in set (0.00 sec)


可以看到通过这种方式可以对批量insert进行优化.

SQL>  insert into t1(a)
  2             values (1),
  3                    (2);
 
insert into t1(a)
           values (1),
                  (2)
 
ORA-00933: SQL command not properly ended

对于这种直接insert多行的方式,oracle数据库不支持.

阅读(1946) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~