MySQL database transaction and storage engine related content learning (detailed)

One, MySQL things

1.1 The concept of transaction

●A transaction is a mechanism, an operation sequence, which contains a group of database operation commands, and submits or cancels operation requests to the system together with all the commands as a whole, that is, this group of database commands are either executed or not executed.
●A transaction is an inseparable logical unit of work. When concurrent operations are performed on the database system, the transaction is the smallest unit of control.
●Transactions are suitable for scenarios where multiple users are simultaneously operating database systems, such as banks, insurance companies, and securities trading systems.
●The transaction ensures the consistency of data through the integrity of the transaction.
To put it bluntly, the so-called transaction, it is a sequence of operations, these operations are either executed or not executed, it is an indivisible unit of work.

1.2 ACID characteristics of transactions

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

1.2.1 Atomicity

Atomicity: Refers to the transaction as an indivisible unit of work, and the operations in the transaction either all happen or never happen.
A transaction is a complete operation, and the elements of the transaction are inseparable.
All elements in the transaction must be committed or rolled back as a whole.
If any element in the transaction fails, the entire transaction will fail.

1.2.1.1 Case

When A transfers 100 yuan to B, it only executes the deduction statement and submits it. At this time, if the power is suddenly cut off, the A account has already been deducted, but the B account has not received the increase, which will cause disputes in life. . In this case, the atomicity of the transaction is required to ensure that the transaction is either executed or not executed.

1.2.2 Consistency

Consistency: It means that the integrity constraints of the database have not been destroyed before the start of the transaction and after the end of the transaction. .
When the transaction is completed, the data must be in a consistent state.
Before the transaction begins, the data stored in the database is in a consistent state.
In the ongoing transaction, the data may be in an inconsistent state.
When the transaction is successfully completed, the data must be returned to a known consistent state again.

1.2.2.1 Case

For bank transfer transactions, regardless of whether the transaction succeeds or fails, it should be ensured that the total amount of deposits in the table A and B after the transaction is the same as before the transaction is executed.

1.2.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 the transaction must be independent, and 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 starts, or after another transaction using the same data ends.

1.2.3.1 Interaction between transactions

事务之间的相互影响分为四种,分别为:

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

1.2.3.2 Mysql and transaction isolation level

(1) read uncommitted:读取尚未提交的数据:不解决脏读
(2) read committed:读取已经提交的数据:可以解决脏读
(3) repeatable read:重读读取:可以解决脏读和不可重复读---mysql默认的
(4) serializable:串行化:可以解决脏读不可重复读和虚读---相当于锁表
mysql默认的事务处理级别是repeatable read,而Oracle和SQL Server是read committed 。

Insert picture description here

(1) Query the global transaction isolation level

show global variables like '%isolation%';

SELECT @@global.tx_isolation;

Insert picture description here

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

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

Insert picture description here

(3) Set the global transaction isolation level

set global transaction isolation level read committed;

Insert picture description here

(4) Set the session transaction isolation level

set session transaction isolation level read committed;

Insert picture description here

1.2.4 Endurance

Persistence: After the transaction is completed, the changes made by the transaction to the database are persisted in the database and will not be rolled back.
Refers to whether the system fails or not, the result of transaction processing is permanent.
Once the transaction is committed, the effect of the transaction will be permanently retained in the database.

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

1.3 Transaction Control Statement

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

1.3.1 Case

#创建表
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);

Insert picture description here

1.3.1.1 Test commit transaction

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

Insert picture description here
Insert picture description here

1.3.1.2 Test rollback transaction

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

Insert picture description here
Insert picture description here

1.3.1.3 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;

Insert picture description here
Insert picture description here

1.4 Use set settings to control transactions

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

Insert picture description here

If auto-commit is not turned on, all operations of mysql connected to the current session will be treated as a transaction until you enter rollback or commit; the current transaction is considered to be over. Before the end of the current transaction, the 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, no matter whether it is turned on or not, begin; commit|rollback; are all independent affairs.

Second, the storage engine

2.1 Concept introduction

Definition : The data in MySQL is stored in files using a variety of different technologies. Each technology uses different storage mechanisms, indexing techniques, locking levels and ultimately provides different functions and capabilities, these different technologies and supporting functions It is called the storage engine in MySQL.
Storage engine is the storage method or storage format of MySQL storing data in the file system.

2.2 MySQL commonly used storage engines

MySQL commonly used storage engine
●MyISAM
●InnoDB
MySQL database component, responsible for performing the actual data I/O operation. In the
MySQL system, the storage engine is on the file system. The data will be transferred to the storage engine before it is saved to the data file. Store according to the storage format of each storage engine

Three, MyISAM

3.1 Introduction to the features of MyISAM

1. MyISAM does not support transactions or foreign key constraints. It only supports full-text indexing. Data files and index files are stored separately. The
access speed is fast and there is no requirement for transaction integrity.
2. MyISAM is suitable for query and insert-based applications
MyISAM在磁盘上存储成三个文件,文件名和表名都相同,但是扩展名分别为

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

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

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

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

5. The data is written or read separately, the speed is faster and the resources are relatively small.
6. The storage format supported by MyIAM

静态表
动态表
压缩表

3.2 MyISAM table supports 3 different storage formats

(1) Static (fixed length) table
Static table is the default storage format. The fields in the static table are all non-variable fields, so that each record has a fixed length.
The advantage of this storage method is that the storage is very fast, easy to cache, and easy to recover from failure; the
disadvantage is that it usually occupies more space than dynamic tables.
(2) Dynamic table The
dynamic table contains variable fields, and the records are not of fixed length.
The advantage of such storage is that it takes up less space, but frequent updates and deletions of records will generate fragmentation. It is necessary to periodically execute OPTIMIZE TABLE statement or myisamchk -r command to improve performance, and it is relatively difficult to recover when a failure occurs.
(3) Compressed table The
compressed table is created by the myisamchk tool and occupies a very small space. Because each record is compressed separately, there is only a very small access expense.

3.3 Examples of production scenarios applicable to MyISAM

1, the business transaction does not need to support
2, unilaterally read or write data more business
3, MyISAM storage engine data read and write are not suitable for more frequent scene
4, using a relatively low write concurrent access business
5 、Services with relatively few data modification
6.Services that do not require very high data service consistency
7. Server hardware resources are relatively poor

Four, InnoDB

4.1 Introduction to InnoDB Features

1. Support transactions, support 4 transaction isolation levels
MySQL starting from version 5.5.5, 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 clusters
5. Support partitions and table spaces, similar to oracle database
6. Support foreign key constraints. Before 5.5, full-text indexing is not supported. After 5.5, full-text indexing is supported.
7. When hardware resource requirements are still relatively high,
8. Row-level Locked, but the 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-growing fields, InnoDB must contain only the index of the field, but a composite index can be created with other fields in the MyISAM table.
11. When the entire table is cleared, InnoDB deletes row by row, which is very slow. MyISAM will rebuild the table

4.2 InnoDB applicable production scenario analysis

1. Business needs transaction support
2. Row-level locking has good adaptability to high concurrency, but it is necessary to ensure that the query is completed through the index
3. Scenarios where business data is updated frequently, such as: forums, Weibo, etc.
4. Business data consistency requirements are high, such as: banking business
5. Hardware devices have large memory, use InnoDB's better caching capabilities to improve memory utilization and reduce disk IO pressure

4.3 Basis for Enterprise Selection of Storage Engine

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 lock: MyISAM support
Row lock: 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 indexes at all.

5. Transaction processing support

Improve the reliability during the period of updating and inserting information in the table. The
storage engine can be selected according to whether the enterprise business needs to support transactions.

(1) View storage engines supported by the system

show engines;

Insert picture description here

(2) View the storage engine used by the table

method one:

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

Insert picture description here
Method Two:

use 库名;
show create table 表名;

Insert picture description here

(3) Modify the storage engine

方法一:通过alter table修改

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

Insert picture description here

方法二:通过修改/etc/my.cnf 配置文件,指定默认存储引擎并重启服务

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

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

Insert picture description here
Insert picture description here
Insert picture description here

方法三:通过create table 创建表时指定存储引擎

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

Insert picture description here

Guess you like

Origin blog.csdn.net/IvyXYW/article/details/113662941