国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home Database Mysql Tutorial 【整理】MySQL之a(chǎn)utocommit_MySQL

【整理】MySQL之a(chǎn)utocommit_MySQL

Jun 01, 2016 pm 01:28 PM

bitsCN.com

【整理】MySQL之a(chǎn)utocommit

?

mysql 默認是開啟 auto commit 的??梢酝ㄟ^如下命令查看 session 級別和 global 級別的設(shè)置:?

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 執(zhí)行自動提交時,應(yīng)該如何禁用 autocommit 呢?可以通過 Cmd-Line、Option file、System Var 上都可用的 init_connect 來設(shè)置。

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 執(zhí)行一次由 init_connect 指定的 sql 字串。(是否可以認為是基于 session 的?)

利用這個變量,可以通過如下方式禁用 autocommit:

方法一:

1

mysql>SET GLOBAL init_connect='SET autocommit=0';

方法二:

在 MySQL 的配置文件中設(shè)置

1

[mysqld]

2

init_connect='SET autocommit=0'

方法三:

啟動 mysql 時帶上命令行參數(shù) –init_connect='SET autocommit=0'

值得說明的一點是,這個參數(shù)的設(shè)置對擁有 super 權(quán)限的用戶是無效的,具體原因說明如下:

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 的性能有一定影響,但既然默認開啟必定是有原因的,所以如果你不知道自己到底會遇到什么問題的情況下還是不要改這個設(shè)置為妙。舉個例子來說明開啟 autocommit 會產(chǎn)生的性能影響,如果你插入了 1000 條數(shù)據(jù),mysql 會 commit 1000 次,如果我們把 autocommit 關(guān)閉掉,通過程序來控制,只要一次commit 就可以了。

========= 我是分割線 =========

a. 初始狀態(tài)+設(shè)置 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. 創(chuàng)建一個測試表 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. 插入數(shù)據(jù) 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>       發(fā)現(xiàn) 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>       發(fā)現(xiàn)什么數(shù)據(jù)都沒有。為什么呢?因為 SQL 語句并沒有被自己(當前 session)提交給 server 端去處理,只是在當前連接中做了相應(yīng)處理。 重復(fù)上面的實驗,但是保持 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>

?

這回該有的都有了。?

?

========= 我是分割線 ?=========?

?

網(wǎng)友說法:?

不要設(shè)定 autocommit 這個開關(guān),讓它保持 autocommit=1 這個默認狀態(tài)。 平常有查詢/更新都是需要得到最新的數(shù)據(jù),根本不需要啟動一個事務(wù),除非有特定狀況才需要開啟事務(wù),再手工用 start transaction ... commit /rollback 。

這種全局設(shè)置,在生產(chǎn)環(huán)境中沒有多大意義。一般都是在應(yīng)用程序框架(如連接池的庫)中設(shè)置 autocommit 是否為 ON/OFF, 說白了,就是得到數(shù)據(jù)庫連接以后,顯示的調(diào)用一次 set autocommit on/off (or =1/0)

?

?

bitsCN.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276
Connecting to MySQL Database Using the Command Line Client Connecting to MySQL Database Using the Command Line Client Jul 07, 2025 am 01:50 AM

The most direct way to connect to MySQL database is to use the command line client. First enter the mysql-u username -p and enter the password correctly to enter the interactive interface; if you connect to the remote database, you need to add the -h parameter to specify the host address. Secondly, you can directly switch to a specific database or execute SQL files when logging in, such as mysql-u username-p database name or mysql-u username-p database name

Handling character sets and collations issues in MySQL Handling character sets and collations issues in MySQL Jul 08, 2025 am 02:51 AM

Character set and sorting rules issues are common when cross-platform migration or multi-person development, resulting in garbled code or inconsistent query. There are three core solutions: First, check and unify the character set of database, table, and fields to utf8mb4, view through SHOWCREATEDATABASE/TABLE, and modify it with ALTER statement; second, specify the utf8mb4 character set when the client connects, and set it in connection parameters or execute SETNAMES; third, select the sorting rules reasonably, and recommend using utf8mb4_unicode_ci to ensure the accuracy of comparison and sorting, and specify or modify it through ALTER when building the library and table.

Implementing Transactions and Understanding ACID Properties in MySQL Implementing Transactions and Understanding ACID Properties in MySQL Jul 08, 2025 am 02:50 AM

MySQL supports transaction processing, and uses the InnoDB storage engine to ensure data consistency and integrity. 1. Transactions are a set of SQL operations, either all succeed or all fail to roll back; 2. ACID attributes include atomicity, consistency, isolation and persistence; 3. The statements that manually control transactions are STARTTRANSACTION, COMMIT and ROLLBACK; 4. The four isolation levels include read not committed, read submitted, repeatable read and serialization; 5. Use transactions correctly to avoid long-term operation, turn off automatic commits, and reasonably handle locks and exceptions. Through these mechanisms, MySQL can achieve high reliability and concurrent control.

Managing Character Sets and Collations in MySQL Managing Character Sets and Collations in MySQL Jul 07, 2025 am 01:41 AM

The setting of character sets and collation rules in MySQL is crucial, affecting data storage, query efficiency and consistency. First, the character set determines the storable character range, such as utf8mb4 supports Chinese and emojis; the sorting rules control the character comparison method, such as utf8mb4_unicode_ci is case-sensitive, and utf8mb4_bin is binary comparison. Secondly, the character set can be set at multiple levels of server, database, table, and column. It is recommended to use utf8mb4 and utf8mb4_unicode_ci in a unified manner to avoid conflicts. Furthermore, the garbled code problem is often caused by inconsistent character sets of connections, storage or program terminals, and needs to be checked layer by layer and set uniformly. In addition, character sets should be specified when exporting and importing to prevent conversion errors

Using Common Table Expressions (CTEs) in MySQL 8 Using Common Table Expressions (CTEs) in MySQL 8 Jul 12, 2025 am 02:23 AM

CTEs are a feature introduced by MySQL8.0 to improve the readability and maintenance of complex queries. 1. CTE is a temporary result set, which is only valid in the current query, has a clear structure, and supports duplicate references; 2. Compared with subqueries, CTE is more readable, reusable and supports recursion; 3. Recursive CTE can process hierarchical data, such as organizational structure, which needs to include initial query and recursion parts; 4. Use suggestions include avoiding abuse, naming specifications, paying attention to performance and debugging methods.

Strategies for MySQL Query Performance Optimization Strategies for MySQL Query Performance Optimization Jul 13, 2025 am 01:45 AM

MySQL query performance optimization needs to start from the core points, including rational use of indexes, optimization of SQL statements, table structure design and partitioning strategies, and utilization of cache and monitoring tools. 1. Use indexes reasonably: Create indexes on commonly used query fields, avoid full table scanning, pay attention to the combined index order, do not add indexes in low selective fields, and avoid redundant indexes. 2. Optimize SQL queries: Avoid SELECT*, do not use functions in WHERE, reduce subquery nesting, and optimize paging query methods. 3. Table structure design and partitioning: select paradigm or anti-paradigm according to read and write scenarios, select appropriate field types, clean data regularly, and consider horizontal tables to divide tables or partition by time. 4. Utilize cache and monitoring: Use Redis cache to reduce database pressure and enable slow query

Designing a Robust MySQL Database Backup Strategy Designing a Robust MySQL Database Backup Strategy Jul 08, 2025 am 02:45 AM

To design a reliable MySQL backup solution, 1. First, clarify RTO and RPO indicators, and determine the backup frequency and method based on the acceptable downtime and data loss range of the business; 2. Adopt a hybrid backup strategy, combining logical backup (such as mysqldump), physical backup (such as PerconaXtraBackup) and binary log (binlog), to achieve rapid recovery and minimum data loss; 3. Test the recovery process regularly to ensure the effectiveness of the backup and be familiar with the recovery operations; 4. Pay attention to storage security, including off-site storage, encryption protection, version retention policy and backup task monitoring.

Optimizing complex JOIN operations in MySQL Optimizing complex JOIN operations in MySQL Jul 09, 2025 am 01:26 AM

TooptimizecomplexJOINoperationsinMySQL,followfourkeysteps:1)EnsureproperindexingonbothsidesofJOINcolumns,especiallyusingcompositeindexesformulti-columnjoinsandavoidinglargeVARCHARindexes;2)ReducedataearlybyfilteringwithWHEREclausesandlimitingselected

See all articles