【整理】MySQL之autocommit_MySQL
bitsCN.com
【整理】MySQL之autocommit
mysql 默认是开启 auto commit 的。可以通过如下命令查看 session 级别和 global 级别的设置:
01mysql> select @@session.autocommit;02+----------------------+03| @@session.autocommit |04+----------------------+05| 1 |06+----------------------+071 row in set (0.00 sec)0809mysql> select @@global.autocommit; 10+---------------------+11| @@global.autocommit |12+---------------------+13| 1 |14+---------------------+151 row in set (0.00 sec)1617mysql>
那么如果我们不想让 mysql 执行自动提交时,应该如何禁用 autocommit 呢?可以通过 Cmd-Line、Option file、System Var 上都可用的 init_connect 来设置。
A string to be executed by the server for each client that connects. The string consists of one or more SQL statements. To specify multiple statements, separate them by semicolon characters.
上面这段话的意思是,每个 client 连接上来时都会由 server 执行一次由 init_connect 指定的 sql 字串。(是否可以认为是基于 session 的?)
利用这个变量,可以通过如下方式禁用 autocommit:
方法一:
1
mysql>SET GLOBAL init_connect='SET autocommit=0';
方法二:
在 MySQL 的配置文件中设置
1
[mysqld]
2
init_connect='SET autocommit=0'
方法三:
启动 mysql 时带上命令行参数 –init_connect='SET autocommit=0'
值得说明的一点是,这个参数的设置对拥有 super 权限的用户是无效的,具体原因说明如下:
Note that the content of init_connect is not executed for users that have the SUPER privilege. This is done so that an erroneous value for init_connect does not prevent all clients from connecting. For example, the value might contain a statement that has a syntax error, thus causing client connections to fail. Not executing init_connect for users that have the SUPER privilege enables them to open a connection and fix the init_connect value.
默认开启的 autocommit 肯定会对 mysql 的性能有一定影响,但既然默认开启必定是有原因的,所以如果你不知道自己到底会遇到什么问题的情况下还是不要改这个设置为妙。举个例子来说明开启 autocommit 会产生的性能影响,如果你插入了 1000 条数据,mysql 会 commit 1000 次,如果我们把 autocommit 关闭掉,通过程序来控制,只要一次commit 就可以了。
========= 我是分割线 =========
a. 初始状态+设置 session 级别的 autocommit 为 0
01mysql>02mysql> show binlog events;03+------------------+-----+-------------+-----------+-------------+---------------------------------------+04| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |05+------------------+-----+-------------+-----------+-------------+---------------------------------------+06| mysql-bin.000001 | 4 | Format_desc | 1 | 120 | Server ver: 5.6.10-log, Binlog ver: 4 |07+------------------+-----+-------------+-----------+-------------+---------------------------------------+081 row in set (0.00 sec)0910mysql>11mysql> show tables;12Empty set (0.00 sec)1314mysql>15mysql> select @@global.autocommit;16+---------------------+17| @@global.autocommit |18+---------------------+19| 1 |20+---------------------+211 row in set (0.00 sec)2223mysql> select @@session.autocommit; 24+----------------------+25| @@session.autocommit |26+----------------------+27| 1 |28+----------------------+291 row in set (0.00 sec)3031mysql>32mysql> set autocommit=0;33Query OK, 0 rows affected (0.00 sec)3435mysql>36mysql> select @@global.autocommit;37+---------------------+38| @@global.autocommit |39+---------------------+40| 1 |41+---------------------+421 row in set (0.00 sec)4344mysql> select @@session.autocommit; 45+----------------------+46| @@session.autocommit |47+----------------------+48| 0 |49+----------------------+501 row in set (0.00 sec)5152mysql>b. 创建一个测试表 01mysql> create table t_autocommit(02 -> id int not null auto_increment,03 -> amount int not null default '0',04 -> primary key(id)05 -> )engine=innodb;06Query OK, 0 rows affected (0.01 sec)0708mysql>09mysql> show tables;10+----------------+11| Tables_in_test |12+----------------+13| t_autocommit |14+----------------+151 row in set (0.00 sec)1617mysql>18mysql> describe t_autocommit;19+--------+---------+------+-----+---------+----------------+20| Field | Type | Null | Key | Default | Extra |21+--------+---------+------+-----+---------+----------------+22| id | int(11) | NO | PRI | NULL | auto_increment |23| amount | int(11) | NO | | 0 | |24+--------+---------+------+-----+---------+----------------+252 rows in set (0.00 sec)2627mysql>28mysql> select * from t_autocommit;29Empty set (0.00 sec)3031mysql>c. 插入数据 01mysql>02mysql> insert into t_autocommit set amount=1;03Query OK, 1 row affected (0.00 sec)0405mysql>06mysql> select * from t_autocommit; 07+----+--------+08| id | amount |09+----+--------+10| 1 | 1 |11+----+--------+121 row in set (0.00 sec)1314mysql>15mysql> update t_autocommit set amount=amount+10;16Query OK, 1 row affected (0.00 sec)17Rows matched: 1 Changed: 1 Warnings: 01819mysql>20mysql> select * from t_autocommit; 21+----+--------+22| id | amount |23+----+--------+24| 1 | 11 |25+----+--------+261 row in set (0.00 sec)2728mysql>29mysql> show binlog events;30+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+31| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |32+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+33| mysql-bin.000001 | 4 | Format_desc | 1 | 120 | Server ver: 5.6.10-log, Binlog ver: 4 |34| mysql-bin.000001 | 120 | Query | 1 | 316 | use `test`; create table t_autocommit(35id int not null auto_increment,36amount int not null default '0',37primary key(id)38)engine=innodb |39+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+402 rows in set (0.00 sec)4142mysql> 发现 binlog 中仅记录了 create table 动作,insert 和 update 由于 autocommit 为 0 的缘故没有被记录到 binlog 中。d.断开 mysql ,再重新连接。 01mysql>02mysql> quit03Bye04[root@Betty ~]#05[root@Betty ~]# mysql -u root -p06Enter password:07Welcome to the MySQL monitor. Commands end with ; or /g.08Your MySQL connection id is 309Server version: 5.6.10-log Source distribution1011Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.1213Oracle is a registered trademark of Oracle Corporation and/or its14affiliates. Other names may be trademarks of their respective15owners.1617Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.1819mysql>20mysql> use test;21Reading table information for completion of table and column names22You can turn off this feature to get a quicker startup with -A2324Database changed25mysql>26mysql> show tables;27+----------------+28| Tables_in_test |29+----------------+30| t_autocommit |31+----------------+321 row in set (0.00 sec)3334mysql>35mysql> select * from t_autocommit;36Empty set (0.00 sec)3738mysql> 发现什么数据都没有。为什么呢?因为 SQL 语句并没有被自己(当前 session)提交给 server 端去处理,只是在当前连接中做了相应处理。 重复上面的实验,但是保持 autocommit 的默认值(1)。 001mysql>002mysql> show binlog events;003+------------------+-----+-------------+-----------+-------------+---------------------------------------+004| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |005+------------------+-----+-------------+-----------+-------------+---------------------------------------+006| mysql-bin.000001 | 4 | Format_desc | 1 | 120 | Server ver: 5.6.10-log, Binlog ver: 4 |007+------------------+-----+-------------+-----------+-------------+---------------------------------------+0081 row in set (0.00 sec)009010mysql>011mysql> show tables;012Empty set (0.01 sec)013014mysql>015mysql> select @@global.autocommit; 016+---------------------+017| @@global.autocommit |018+---------------------+019| 1 |020+---------------------+0211 row in set (0.00 sec)022023mysql>024mysql> select @@session.autocommit; 025+----------------------+026| @@session.autocommit |027+----------------------+028| 1 |029+----------------------+0301 row in set (0.00 sec)031032mysql>033mysql> create table t_autocommit(034 -> id int not null auto_increment,035 -> amount int not null default '0',036 -> primary key(id)037 -> )engine=innodb;038Query OK, 0 rows affected (0.01 sec)039040mysql>041mysql> show tables;042+----------------+043| Tables_in_test |044+----------------+045| t_autocommit |046+----------------+0471 row in set (0.00 sec)048049mysql>050mysql> describe t_autocommit;051+--------+---------+------+-----+---------+----------------+052| Field | Type | Null | Key | Default | Extra |053+--------+---------+------+-----+---------+----------------+054| id | int(11) | NO | PRI | NULL | auto_increment |055| amount | int(11) | NO | | 0 | |056+--------+---------+------+-----+---------+----------------+0572 rows in set (0.00 sec)058059mysql>060mysql> insert into t_autocommit set amount=1;061Query OK, 1 row affected (0.00 sec)062063mysql>064mysql> select * from t_autocommit;065+----+--------+066| id | amount |067+----+--------+068| 1 | 1 |069+----+--------+0701 row in set (0.00 sec)071072mysql>073mysql> update t_autocommit set amount=amount+10;074Query OK, 1 row affected (0.00 sec)075Rows matched: 1 Changed: 1 Warnings: 0076077mysql>078mysql> select * from t_autocommit; 079+----+--------+080| id | amount |081+----+--------+082| 1 | 11 |083+----+--------+0841 row in set (0.00 sec)085086mysql>087mysql> show binlog events;088+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+089| Log_name | Pos | Event_type | Server_id | End_log_pos | Info |090+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+091| mysql-bin.000001 | 4 | Format_desc | 1 | 120 | Server ver: 5.6.10-log, Binlog ver: 4 |092| mysql-bin.000001 | 120 | Query | 1 | 316 | use `test`; create table t_autocommit(093id int not null auto_increment,094amount int not null default '0',095primary key(id)096)engine=innodb |097| mysql-bin.000001 | 316 | Query | 1 | 395 | BEGIN |098| mysql-bin.000001 | 395 | Intvar | 1 | 427 | INSERT_ID=1 |099| mysql-bin.000001 | 427 | Query | 1 | 538 | use `test`; insert into t_autocommit set amount=1 |100| mysql-bin.000001 | 538 | Xid | 1 | 569 | COMMIT /* xid=62 */ |101| mysql-bin.000001 | 569 | Query | 1 | 648 | BEGIN |102| mysql-bin.000001 | 648 | Query | 1 | 762 | use `test`; update t_autocommit set amount=amount+10 |103| mysql-bin.000001 | 762 | Xid | 1 | 793 | COMMIT /* xid=64 */ |104+------------------+-----+-------------+-----------+-------------+----------------------------------------------------------------------------------------------------------------------------------------+1059 rows in set (0.00 sec)106107mysql>108mysql> quit109Bye110[root@Betty ~]#111[root@Betty ~]# mysql -u root -p112Enter password:113Welcome to the MySQL monitor. Commands end with ; or /g.114Your MySQL connection id is 4115Server version: 5.6.10-log Source distribution116117Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.118119Oracle is a registered trademark of Oracle Corporation and/or its120affiliates. Other names may be trademarks of their respective121owners.122123Type 'help;' or '/h' for help. Type '/c' to clear the current input statement.124125mysql>126mysql> use test;127Reading table information for completion of table and column names128You can turn off this feature to get a quicker startup with -A129130Database changed131mysql>132mysql> show tables;133+----------------+134| Tables_in_test |135+----------------+136| t_autocommit |137+----------------+1381 row in set (0.00 sec)139140mysql>141mysql> select * from t_autocommit;142+----+--------+143| id | amount |144+----+--------+145| 1 | 11 |146+----+--------+1471 row in set (0.00 sec)148149mysql>150mysql>
这回该有的都有了。
========= 我是分割线 =========
网友说法:
不要设定 autocommit 这个开关,让它保持 autocommit=1 这个默认状态。 平常有查询/更新都是需要得到最新的数据,根本不需要启动一个事务,除非有特定状况才需要开启事务,再手工用 start transaction ... commit /rollback 。
这种全局设置,在生产环境中没有多大意义。一般都是在应用程序框架(如连接池的库)中设置 autocommit 是否为 ON/OFF, 说白了,就是得到数据库连接以后,显示的调用一次 set autocommit on/off (or =1/0)
bitsCN.com

熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

記事本++7.3.1
好用且免費的程式碼編輯器

SublimeText3漢化版
中文版,非常好用

禪工作室 13.0.1
強大的PHP整合開發環境

Dreamweaver CS6
視覺化網頁開發工具

SublimeText3 Mac版
神級程式碼編輯軟體(SublimeText3)

MySQL是一個開源的關係型數據庫管理系統。 1)創建數據庫和表:使用CREATEDATABASE和CREATETABLE命令。 2)基本操作:INSERT、UPDATE、DELETE和SELECT。 3)高級操作:JOIN、子查詢和事務處理。 4)調試技巧:檢查語法、數據類型和權限。 5)優化建議:使用索引、避免SELECT*和使用事務。

MySQL在Web應用中的主要作用是存儲和管理數據。 1.MySQL高效處理用戶信息、產品目錄和交易記錄等數據。 2.通過SQL查詢,開發者能從數據庫提取信息生成動態內容。 3.MySQL基於客戶端-服務器模型工作,確保查詢速度可接受。

InnoDB使用redologs和undologs確保數據一致性和可靠性。 1.redologs記錄數據頁修改,確保崩潰恢復和事務持久性。 2.undologs記錄數據原始值,支持事務回滾和MVCC。

MySQL是一種開源的關係型數據庫管理系統,主要用於快速、可靠地存儲和檢索數據。其工作原理包括客戶端請求、查詢解析、執行查詢和返回結果。使用示例包括創建表、插入和查詢數據,以及高級功能如JOIN操作。常見錯誤涉及SQL語法、數據類型和權限問題,優化建議包括使用索引、優化查詢和分錶分區。

MySQL在數據庫和編程中的地位非常重要,它是一個開源的關係型數據庫管理系統,廣泛應用於各種應用場景。 1)MySQL提供高效的數據存儲、組織和檢索功能,支持Web、移動和企業級系統。 2)它使用客戶端-服務器架構,支持多種存儲引擎和索引優化。 3)基本用法包括創建表和插入數據,高級用法涉及多表JOIN和復雜查詢。 4)常見問題如SQL語法錯誤和性能問題可以通過EXPLAIN命令和慢查詢日誌調試。 5)性能優化方法包括合理使用索引、優化查詢和使用緩存,最佳實踐包括使用事務和PreparedStatemen

選擇MySQL的原因是其性能、可靠性、易用性和社區支持。 1.MySQL提供高效的數據存儲和檢索功能,支持多種數據類型和高級查詢操作。 2.採用客戶端-服務器架構和多種存儲引擎,支持事務和查詢優化。 3.易於使用,支持多種操作系統和編程語言。 4.擁有強大的社區支持,提供豐富的資源和解決方案。

MySQL与其他编程语言相比,主要用于存储和管理数据,而其他语言如Python、Java、C 则用于逻辑处理和应用开发。MySQL以其高性能、可扩展性和跨平台支持著称,适合数据管理需求,而其他语言在各自领域如数据分析、企业应用和系统编程中各有优势。

MySQL索引基数对查询性能有显著影响:1.高基数索引能更有效地缩小数据范围,提高查询效率;2.低基数索引可能导致全表扫描,降低查询性能;3.在联合索引中,应将高基数列放在前面以优化查询。
