隨著MySQL的應(yīng)用日益廣泛,支持事務(wù)特性的InnoDB已經(jīng)成為MySQL的默認(rèn)存儲引擎。和很多關(guān)系數(shù)據(jù)庫不同的是,在InnoDB存儲引擎中,事務(wù)默認(rèn)是自動提交的,也就是說每條DML語句都會觸發(fā)commit操作。這一自動提交(autocommit)特性在很多場景下對于性能還是有一定影響的。
最近我們嘗試將一個Oracle數(shù)據(jù)庫的數(shù)據(jù)遷移到MySQL數(shù)據(jù)庫,發(fā)現(xiàn)導(dǎo)入時間很長。舉個例子來說,其中有一個表包含四千多條數(shù)據(jù),插入時間竟然超過了100秒。每插入一條數(shù)據(jù),數(shù)據(jù)庫就會自動提交一次,也就是說單這一個表MySQL會commit超過4000次,如果我們關(guān)閉自動提交功能,通過程序來控制,只要一次commit就可以了。
那么,如何關(guān)閉MySQL的autocommit特性呢?
通常有兩種方法:
-
一種是通過set命令修改會話級別或者數(shù)據(jù)庫級別的參數(shù),但是數(shù)據(jù)庫重啟后參數(shù)會恢復(fù)默認(rèn)值;
-
第二種方法是修改mysql的配置文件my.ini,一勞永逸。
1.測試環(huán)境 Windows Server 2008 r2+MySQL Community Server (GPL) 5.7.16
我是在Windows Server 2008 r2環(huán)境下進(jìn)行測試。
-
mysql> status
-
--------------
-
mysql Ver 14.14 Distrib 5.7.16, for Win64(x86_64)
-
-
Connection id: 2
-
Current database:
-
Current user: root@localhost
-
SSL: Not in use
-
Using delimiter: ;
-
Server version: 5.7.16 MySQL Community Server(GPL)
-
Protocol version: 10
-
Connection: localhost via TCP/IP
-
Server characterset: latin1
-
Db characterset: latin1
-
Client characterset: utf8
-
Conn. characterset: utf8
-
TCP port: 3306
-
Uptime: 9 min 5 sec
-
-
Threads: 1 Questions: 7 Slow queries: 0 Opens: 106 Flush tables: 1 Open tables: 99 Queries per second avg: 0.012
-
--------------
-
-
mysql>
2.通過set來關(guān)閉autocommit,重啟后恢復(fù)默認(rèn)值
首先,我們通過set命令來修改autocommit參數(shù)。
-
mysql>
-
mysql> show global variableslike '%commit%';
-
+-----------------------------------------+-------+
-
| Variable_name | Value |
-
+-----------------------------------------+-------+
-
| autocommit| ON |
-
| binlog_group_commit_sync_delay | 0 |
-
| binlog_group_commit_sync_no_delay_count | 0 |
-
| binlog_order_commits | ON |
-
| innodb_api_bk_commit_interval | 5 |
-
| innodb_commit_concurrency | 0 |
-
| innodb_flush_log_at_trx_commit | 1 |
-
| slave_preserve_commit_order | OFF |
-
+-----------------------------------------+-------+
-
8 rows in set, 1 warning (0.00 sec)
-
-
mysql> set autocommit=0;
-
Query OK, 0 rows affected (0.00 sec)
-
mysql>
-
mysql> set global autocommit=0;
-
Query OK, 0 rows affected (0.00 sec)
-
-
mysql> show global variables like '%commit%';
-
+-----------------------------------------+-------+
-
| Variable_name | Value |
-
+-----------------------------------------+-------+
-
| autocommit| OFF |
-
| binlog_group_commit_sync_delay | 0 |
-
| binlog_group_commit_sync_no_delay_count | 0 |
-
| binlog_order_commits | ON |
-
| innodb_api_bk_commit_interval | 5 |
-
| innodb_commit_concurrency | 0 |
-
| innodb_flush_log_at_trx_commit | 1 |
-
| slave_preserve_commit_order | OFF |
-
+-----------------------------------------+-------+
-
8 rows in set, 1 warning (0.02 sec)
-
-
mysql>
接下來,我們重啟數(shù)據(jù)庫,發(fā)現(xiàn)autocommit參數(shù)已經(jīng)恢復(fù)默認(rèn)值。
-
E:\mysql-5.7.16-winx64\bin>net stop mysql
-
MySQL 服務(wù)正在停止.
-
MySQL 服務(wù)已成功停止。
-
-
-
E:\mysql-5.7.16-winx64\bin>netstart mysql
-
MySQL 服務(wù)正在啟動 .
-
MySQL 服務(wù)已經(jīng)啟動成功。
-
-
-
E:\mysql-5.7.16-winx64\bin>mysql-u root-proot
-
mysql: [Warning] Using a password on the command line interface can be insecure.
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
Your MySQL connection id is 2
-
Server version: 5.7.16 MySQL Community Server (GPL)
-
-
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
-
-
Oracle is a registered trademark of Oracle Corporation and/or its
-
affiliates. Other names may be trademarks of their respective
-
owners.
-
-
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
-
mysql>
-
mysql> show global variableslike '%commit%';
-
+-----------------------------------------+-------+
-
| Variable_name | Value |
-
+-----------------------------------------+-------+
-
| autocommit | ON |
-
| binlog_group_commit_sync_delay | 0 |
-
| binlog_group_commit_sync_no_delay_count | 0 |
-
| binlog_order_commits | ON |
-
| innodb_api_bk_commit_interval | 5 |
-
| innodb_commit_concurrency | 0 |
-
| innodb_flush_log_at_trx_commit | 1 |
-
| slave_preserve_commit_order | OFF |
-
+-----------------------------------------+-------+
-
8 rows in set, 1 warning (0.00 sec)
-
-
mysql>
3.修改mysql的配置文件my.ini
我們找到mysql的配置文件my.ini,在里面添加一行記錄“autocommit=0”。
-
[mysql]
-
default-character-set=utf8
-
[mysqld]
-
max_connections=200
-
default-storage-engine=INNODB
-
basedir =E:\mysql-5.7.16-winx64\bin
-
datadir =E:\mysql-5.7.16-winx64\data
-
port = 3306
-
autocommit=0
然后重新啟動數(shù)據(jù)庫,確認(rèn)autocommit參數(shù)是否為OFF。
-
E:\mysql-5.7.16-winx64\bin>net stop mysql
-
MySQL 服務(wù)正在停止.
-
MySQL 服務(wù)已成功停止。
-
-
-
E:\mysql-5.7.16-winx64\bin>net start mysql
-
MySQL 服務(wù)正在啟動 .
-
MySQL 服務(wù)已經(jīng)啟動成功。
-
-
-
E:\mysql-5.7.16-winx64\bin>mysql -u root -proot
-
mysql: [Warning] Using a password on the command line interface can be insecure.
-
Welcome to the MySQL monitor. Commands end with ; or \g.
-
Your MySQL connection id is 2
-
Server version: 5.7.16 MySQL Community Server (GPL)
-
-
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.
-
-
Oracle is a registered trademark of Oracle Corporation and/or its
-
affiliates. Other names may be trademarks of their respective
-
owners.
-
-
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
-
-
mysql> show global variables like '%commit%';
-
+-----------------------------------------+-------+
-
| Variable_name | Value |
-
+-----------------------------------------+-------+
-
| autocommit| OFF |
-
| binlog_group_commit_sync_delay | 0 |
-
| binlog_group_commit_sync_no_delay_count | 0 |
-
| binlog_order_commits | ON |
-
| innodb_api_bk_commit_interval | 5 |
-
| innodb_commit_concurrency | 0 |
-
| innodb_flush_log_at_trx_commit | 1 |
-
| slave_preserve_commit_order | OFF |
-
+-----------------------------------------+-------+
-
8 rows in set, 1 warning (0.01 sec)
-
-
mysql>
-
mysql>
我們看到autocommit參數(shù)為OFF,目標(biāo)達(dá)成。
4.數(shù)據(jù)插入速度提升十倍
以開頭提到的那張表為例,4000多條數(shù)據(jù),在關(guān)閉autocommit參數(shù)之前插入時間為101505ms;關(guān)閉autocommit參數(shù)之后插入時間為8869ms,插入速度提升了十倍還多。
但是這個時間其實還是蠻長的,繼續(xù)提升!
~~~~~~~ the end~~~~~~~~~
hoegh
2016.11.01
當(dāng)前名稱:Windows下關(guān)閉MySQL的自動提交(autocommit)功能
文章位置:http://jinyejixie.com/article42/ppehec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、自適應(yīng)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、手機(jī)網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、App開發(fā)
廣告
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源:
創(chuàng)新互聯(lián)