MySQL database transaction and storage engine (thieves in detail)

Table of contents

1. MySQL transaction

1. The concept of business

2. ACID characteristics of transactions

(1) Atomicity

(2) Consistency

(3) Isolation

(4) Interaction between transactions

(5) Mysql and transaction isolation level

(6) Persistence

Two: business order

1. Query the global transaction isolation level

2. Query session transaction isolation level (only valid for the current connection)

3. Set the global transaction isolation level

4. Set the session transaction isolation level

5. Transaction control statement

6. Case

(1) Create a table

 (2) Test commit transaction

(3) Test rollback transaction

(4) Test multi-point rollback

7. Use set settings to control transactions

Three: storage engine

1. Concept introduction

2. Commonly used storage engines for MySQL

Four: MyISAM

1. Introduction to the characteristics of MyISAM

2. MyISAM tables support 3 different storage formats

(1) Static (fixed length) table

(2) Dynamic table

(3) Compression table

3. Examples of production scenarios where MyISAM is applicable

Five: InnoDB

1. InnoDB Features Introduction

2. Analysis of InnoDB applicable production scenarios

3. The basis for enterprises to choose storage engines

4. Index support

 5. Transaction processing support

Six: Storage engine commands

1. View the storage engines supported by the system

2. View the storage engine used by the table

method one:

 Method Two:

3. Modify the storage engine

(1) Method 1: modify through alter table

(2) Method 2: Specify the default storage engine and restart the service by modifying the /etc/my.cnf configuration file

(3) Method 3: Specify the storage engine when creating a table through create table

4. Deadlock demonstration

Solution to avoid deadlock:

 Summarize


1. MySQL transaction

1. The concept of business

  • A transaction is a mechanism, an operation sequence, which includes a set of database operation commands, and submits or revokes operation requests to the system together with all the commands as a whole, that is, this set of database commands is either executed or not executed.
  • A transaction is an indivisible logical unit of work. When performing concurrent operations on a database system, a transaction is the smallest control unit.
  • Transactions are suitable for scenarios where multiple users operate database systems at the same time, such as banks, insurance companies, and securities trading systems.
  • Transactions ensure data consistency through the integrity of transactions.

To put it bluntly, the so-called transaction is a sequence of operations. These operations are either executed or not executed. It is an indivisible unit of work.

2. ACID characteristics of transactions

ACID refers to the four characteristics that transactions should have in a reliable database management system (DBMS): Atomicity, Consistency, Isolation, and Durability . These are a few properties that a reliable database should have.

(1) Atomicity

Atomicity: It means that a transaction is an indivisible unit of work, and the operations in the transaction either all occur or none occur.
A transaction is a complete operation, and the elements of a transaction are inseparable.
All elements in a transaction must be committed or rolled back as a whole.
If any element in the transaction fails, the entire transaction fails.

Case:
When A transfers 100 yuan to B, he only executes the deduction statement and submits it. If there is a sudden power failure, A account has already been debited, but B account has not received the additional payment. cause disputes. In this case, the atomicity of the transaction is required to ensure that the transaction is either executed or not executed.

(2) Consistency

Consistency: It means that the integrity constraints of the database are not violated before the transaction starts and after the transaction ends. .
When the transaction completes, the data must be in a consistent state.
Before a transaction starts, the data stored in the database is in a consistent state.
During an ongoing transaction, data may be in an inconsistent state.
When the transaction completes successfully, the data must come back to a known consistent state again.

case:

For bank transfer transactions, regardless of whether the transaction succeeds or fails, it should be ensured that the total deposits of A and B in the table after the transaction is completed are consistent with those before the transaction is executed.

(3) Isolation

Isolation: In a concurrent environment, when different transactions manipulate the same data at the same time, each transaction has its own complete data space.

All concurrent transactions that modify data are isolated from each other, indicating that a transaction must be independent, it should not depend on or affect other transactions in any way.
A transaction that modifies data can access the data before another transaction using the same data begins, or after another transaction using the same data ends

(4) Interaction between transactions

(1)脏读:一个事务读取了另一个事务未提交的数据,而这个数据是有可能回滚的。
(2)不可重复读:一个事务内两个相同的查询却返回了不同数据。这是由于查询时系统中其他事务修改的提交而引起的。
(3)幻读:一个事务对一个表中的数据进行了修改,这种修改涉及到表中的全部数据行。同时,另一个事务也修改这个表中的数据,这种修改是向表中插入一行新数据。那么,操作前一个事务的用户会发现表中还有没有修改的数据行,就好象发生了幻觉一样。
(4)丢失更新:两个事务同时读取同一条记录,A先修改记录,B也修改记录(B不知道A修改过),B提交数据后B的修改结果覆盖了A的修改结果。

(5) Mysql and transaction isolation level

MySQL transaction supports the following four isolations to control the modification made by the transaction and notify the modification to other concurrent transactions:

(1)未提交读(Read Uncommitted):
允许脏读,即允许一个事务可以看到其他事务未提交的修改。

(2)提交读(Read Committed):
允许一个事务只能看到其他事务已经提交的修改,未提交的修改是不可见的。

(3)可重复读(Repeated Read): ---mysql默认的隔离级别
确保如果在一个事务中执行两次相同的SELECT语句,都能得到相同的结果,不管其他事务是否提交这些修改。

(4)串行读(Serializable): ---相当于锁表
完全串行化的读,将一个事务与其他事务完全地隔离。每次读都需要获得表级共享锁,读写相互都会阻塞。

The default transaction processing level of mysql is repeatable read, while Oracle and SQL Server are read committed

(6) Persistence

Persistence: After the transaction is completed, the changes made by the transaction to the database are permanently saved in the database and will not be rolled back.
It means that regardless of whether the system fails or not, the results of transaction processing are permanent.
Once a transaction is committed, the effects of the transaction are permanently retained in the database.

Summary : In transaction management, atomicity is the foundation, isolation is the means, consistency is the purpose, and persistence is the result.

Two: business order

1. Query the global transaction isolation level

show global variables like '%isolation%';

SELECT @@global.tx_isolation;

2. Query session transaction isolation level (only valid for the current connection)

show session variables like '%isolation%';
SELECT @@session.tx_isolation;
SELECT @@tx_isolation;

3. Set the global transaction isolation level

set global transaction isolation level read committed;

4. Set the session transaction isolation level

set session transaction isolation level read committed;

5. Transaction control statement

BEGIN或START TRANSACTION:显式地开启一个事务。
COMMIT或COMMITWORK:提交事务,并使已对数据库进行的所有修改变为永久性的。
ROLLBACK或ROLLBACK WORK:回滚会结束用户的事务,并撤销正在进行的所有未提交的修改。
SAVEPOINT S1:使用 SAVEPOINT 允许在事务中创建一个回滚点,一个事务中可以有多SAVEPOINT;“S1”代表回滚点名称。
ROLLBACK TO [SAVEPOINT] S1:把事务回滚到标记点。

6. Case

(1) Create a table

#创建表
use xyw;
create table account (
id int(10) primary key not null,
name varchar(40),
money double
);

insert into account values(1,'A',1000);
insert into account values(2,'B',1000);

 (2) Test commit transaction

begin;
update account set money= money - 100 where name='A';
commit;
quit
mysql -u root -p
use xyw;
select * from account; 

 

(3) Test rollback transaction

begin;
update account set money= money + 100 where name='A';
rollback;
quit
mysql -u root -p
use xyw;
select * from account;

(4) Test multi-point rollback

begin; .
update account set money= money + 100 where name='A';
SAVEPOINT S1;
update account set money= money + 100 where name='B';
SAVEPOINT S2;
insert into account values(3,'C',1200); 
select * from account;
ROLLBACK TO S1;
select * from account;

 

7. Use set settings to control transactions

SET AUTOCOMMIT=0;	#禁止自动提交
SET AUTOCOMMIT=1;	#开启自动提交,Mysq1默认为1
SHOW VARIABLES LIKE 'AUTOCOMMIT';    #查看Mysql中的AUTOCOMMIT值

 If autocommit is not enabled, all operations of mysql connected to the current session will be regarded as a transaction until you enter rollback or commit; the current transaction is considered to be over. Before the end of the current transaction, a new mysql connection cannot read any operation results of the current session.
If automatic commit is turned on, mysql will treat each SQL statement as a transaction, and then commit automatically.
Of course, whether it is enabled or not, begin; commit|rollback; are independent transactions.
 

Three: storage engine

1. Concept introduction

Definition : Data in MySQL is stored in files using a variety of different technologies, each of which uses different storage mechanisms, indexing techniques, locking levels, and ultimately provides different functions and capabilities. These different technologies and supporting functions It is called a storage engine in MySQL
The storage engine is the storage method or storage format that MySQL stores data in the file system

2. Commonly used storage engines for MySQL

●MyISAM
●InnoDB
The components in the MySQL database are responsible for performing actual data I/O operations.
In the MySQL system, the storage engine is on top of the file system. Before data is saved to the data file, it will be transmitted to the storage engine. storage format for storage

Four: MyISAM

1. Introduction to the characteristics of MyISAM

1. MyISAM does not support transactions, nor does it support foreign key constraints. It only supports full-text indexes. Data files and index files are stored separately for fast access and no requirements for transaction integrity.

2. MyISAM is suitable for query and insert-based applications
MyISAM在磁盘上存储成三个文件,文件名和表名都相同,但是扩展名分别为

.frm文件存储表结构的定义
数据文件的扩展名为.MYD (MYData)
索引文件的扩展名是.MYI (MYIndex)

 3. Table-level locking form, when data is updated, the entire table is locked

4. The database blocks each other in the process of reading and writing

会在数据写入的过程阻塞用户数据的读取
也会在数据读取的过程中阻塞用户的数据写入

5. The data is written or read separately, the speed process is fast and the resources are relatively small

6. Storage format supported by MyIAM

静态表
动态表
压缩表

2. MyISAM tables support 3 different storage formats

(1) Static (fixed length) table

Static tables are the default storage format. Fields in static tables are immutable, so each record is of fixed length.
The advantage of this storage method is that the storage is very fast, easy to cache, and easy to recover when a failure occurs; the
disadvantage is that it usually takes up more space than a dynamic table.

(2) Dynamic table

Dynamic tables contain variable fields and records are not of fixed length.
The advantage of this kind of storage is that it occupies less space, but frequent updates and deletions of records will cause fragmentation. It is necessary to regularly execute the OPTIMIZE TABLE statement or the myisamchk -r command to improve performance, and it is relatively difficult to recover when a failure occurs.

(3) Compression table

Compressed tables are created by the myisamchk tool and take up very little space because each record is compressed individually, so there is very little access overhead.

3. Examples of production scenarios where MyISAM is applicable

1. The business of the company does not need the support of transactions.
2. The business that reads or writes a lot of data unilaterally.
3. The MyISAM storage engine data reads and writes frequently. It is not suitable for the scene.
4. The business that uses read and write concurrent access is relatively low5
. . Businesses with relatively few data modifications
6. Businesses that do not require very high data business consistency
7. Server hardware resources are relatively poor

Five: InnoDB

1. InnoDB Features Introduction

1. Support transactions, support 4 transaction isolation levels
MySQL starts from version 5.5.5, and the default storage engine is InnoDB
2. Read and write blocking is related to transaction isolation levels
3. Can cache indexes and data very efficiently
4. Tables and primary keys Store in the form of clusters
5. Support partitions and table spaces, similar to oracle databases
6. Support foreign key constraints. Full-text indexes are not supported before 5.5, but full-text indexes are supported after 5.5.
7. For occasions that require relatively high hardware resources
8. Row level lock, but a full table scan will still be a table-level lock, such as

update table set a=1 where user like "%zhang%";
9、InnoDB中不保存表的行数,如'select count(*) from table;'时InnoDB需要扫描一遍整个表来计算有多少行,
但是MyISAM只要简单的读出保存好的行数即可。需要注意的是当count(*)语句包含where条件时MyISAM也需要扫描整个表

10. For self-increasing fields, InnoDB must contain an index only for this field, but in MyISAM tables, combined indexes can be built together with other fields. 11. When clearing the
entire table, InnoDB deletes one row at a time, which is very slow. MyISAM will rebuild the table

2. Analysis of InnoDB applicable production scenarios

1. The business needs the support of transactions.
2. Row-level locking has a good adaptability to high concurrency, but it is necessary to ensure that the query is completed through the index.
3. Scenarios with frequent business data updates, such as: forums, microblogs, etc.
4. Business data consistency requirements are high, such as: banking business
5. The memory of the hardware device is large, and the better caching capability of InnoDB is used to improve memory utilization and reduce the pressure of disk IO

3. The basis for enterprises to choose storage engines

1. Need to consider what different core functions and application scenarios each storage engine provides
2. Supported fields and data types

(1) All engines support common data types
(2) But not all engines support other field types, such as binary objects

 3. Locking type: Different storage engines support different levels of locking

Table locking: MyISAM support
Row locking: InnoDB support

4. Index support

 Indexing can significantly improve performance when searching and restoring data in the database.
Different storage engines provide different indexing techniques.
Some storage engines do not support indexing at all.

 5. Transaction processing support

Improve reliability during updating and inserting information into the table
Storage engine can be selected according to whether the enterprise business needs to support transactions

Six: Storage engine commands

1. View the storage engines supported by the system

show engines;

2. View the storage engine used by the table

method one:

show table status from 库名 where name='表名'\G

 Method Two:

use 库名;
show create table 表名;

3. Modify the storage engine

(1) Method 1: modify through alter table

use 库名;
alter table 表名 engine=MyISAM;

(2) Method 2: Specify the default storage engine and restart the service by modifying the /etc/my.cnf configuration file

vim /etc/my.cnf
[mysqld]
default-storage-engine=INNODB

systemctl restart mysql.service
注意:此方法只对修改了配置文件并重启mysql服务后新创建的表有效,已经存在的表不会有变更。

 

 

(3) Method 3: Specify the storage engine when creating a table through create table

use 库名;
create table 表名 (字段1 数据类型,...) engine=MyISAM;

4. Deadlock demonstration

 If the row lock is used improperly, it will lead to deadlock (deadlock is generally caused by transactions waiting for each other to release resources, and finally forming a loop)

Service A Transaction B
begin; begin;
delete from student where id=2;#Before the end of the transaction, the row with id=2 will be locked
select * from t1 where id=1 for update; #Add an exclusive lock to simulate concurrency and lock the row with id=1
elete from t1 where id=1; #deadlock generation
update t1 set name='abc' where id=2; #Deadlock occurs. Because the row with id=5 in session 1 is still in the process of being deleted, the row has been locked
rollback; #Rollback, end transaction. The row with id=5 is unlocked
update t1 set name='abc' where id=2; #Update data successfully

Solution to avoid deadlock:

1. Use more reasonable business logic to access tables and rows in a fixed order.

2. Dismantle big affairs into small ones. Large transactions are more prone to deadlocks. If the business permits, large transactions should be split into small ones.

3. In the same transaction, try to lock all the resources needed at one time to reduce the probability of deadlock.

4. Lower the isolation level. If the business permits, lowering the isolation level is also a better choice. For example, adjusting the isolation level from RR to RC can avoid many deadlocks caused by gap locks.

5. Add a reasonable index to the table. If no index is used, a lock will be added to each row of the table, and the probability of deadlock will greatly increase.
 

 Summarize

Transactions
Transactions are a mechanism. It is a set of operation sequences (including one or more operation commands), and the transaction will consider all operation commands as a whole to submit or withdraw operations to the system, and all operation commands are either executed or not executed

ACID characteristics of transactions :
atomicity : the basis of transaction management. Treat all operations in a transaction as an indivisible unit of work, either all executed or none executed
consistently : the purpose of transaction management.
Guaranteeing integrity and consistent isolation of data before and after a transaction: a means of transaction management. When multiple transactions operate on the same table data concurrently, each transaction has its own independent data space, and different consistency problems can be solved through the isolation level.
Persistence : the result of transaction management. After the transaction is committed, the results of the modification of the operation command in the transaction will be persisted and will not be rolled back

When multiple transactions concurrently operate the same table data, different isolation levels may cause consistency problems: dirty
read          One transaction can see the modified data not committed by another transaction
Non-repeatable read    Two queries in one transaction You will see data inconsistency, which may be because other transactions have modified the data and submitted
phantom reads   in the middle of the two query processes    . Two queries in one transaction will see data inconsistency (maybe it was found that there was no data), this situation may be because other transactions have inserted new data in the middle of the two query processes and have committed the
loss. Updating   the data modified and submitted by one transaction may overwrite the data modified and submitted by another transaction 

Isolation level:
uncommitted read Read Uncommitted RU allows dirty reads non-repeatable reads phantom reads
committed reads   Read Committed RC does not allow dirty reads allows non-repeatable reads phantom reads
repeatable reads Repeatable Read RR does not allow dirty reads non-repeatable reads conditional permission ( Innodb does not allow) Phantom read
serial read   Serializable is not allowed (equivalent to table-level locking, which will affect the efficiency and performance of database read and write)

View isolation level
show global variables like '%isolation%';
show session variables like '%isolation%';

Set the isolation level
set global transaction isolation level read committed;
set session transaction isolation level read committed;

Transaction management operation
begin;
.... insert into update XX set delete from
savepoint XX;
rollback to XX;
commit; rollback;

Automatic transaction commit
set autocommit=0|1;
show variables like 'autocommit';


Storage engine
The storage engine is a component in the MySQL database, which is responsible for performing actual data I/O operations. It works on the file system. The data in the database will be transmitted to the storage engine first, and will be saved in the file system according to the format of the storage engine.

Common storage engine: MyISAM InnoDB

MyISAM: does not support transactions, foreign key constraints, only supports table-level locking, suitable for individual query or insert operations, weak read and write concurrency, supports full-text indexing, and consumes less resources. Data files (.MYD) and index files ( .MYI) are stored separately.
        Recommended usage scenarios: suitable for business scenarios that do not require transaction processing, separate queries or data insertion
        
InnoDB : support transactions, foreign key constraints, support row-level locking (table-level locking will still be performed during full table scanning), read and write concurrent capabilities Better, it supports full-text indexing (after version 5.5), better caching capability can reduce disk IO pressure, and data files are also index files.        
        Recommended usage scenarios: Suitable for business scenarios that require transaction support, high consistency requirements, frequent data updates, and high concurrent read and write

Storage engine management
alter table table name engine=MyISAM|InnoDB for existing tables

vim /etc/my.cnf Modify the mysql configuration file to set the default storage engine for new tables
[mysqld]
default-storage-engine=MyISAM|InnoDB     

create table table name(....) engine=MyISAM|InnoDB specifies the storage engine when creating a new table

View storage engine
show create table table name;
show table status from library name where name='table name';

show engines;

Guess you like

Origin blog.csdn.net/A1100886/article/details/131229847