MySQL index, transaction and storage engine (detailed graphic and text!) (two)

MySQL index, transaction and storage engine (detailed graphic and text!) (two)

One, MySQL transaction

1. The concept of affairs

1. A transaction is a mechanism, an operation sequence, which contains a set of database operation commands, and submits or revokes operation requests to the system together with all commands as a whole, that is, this set of database commands are either executed or not executed .
2. 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.
3. Transactions are suitable for scenarios where multiple users are simultaneously operating database systems, such as banks, insurance companies, and securities trading systems.
4. 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.

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.

2.1 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.

Case: When
A transfers 100 yuan to B, it only executes the deduction statement and submits it. At this time, if there is a sudden power failure, account A has already deducted, but account B has not received the addition, it will happen in life. Cause disputes. In this case, the atomicity of the transaction is required to ensure that the transaction is either executed or not executed.

2.2 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 complete, 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.

Case:
Regarding a bank transfer transaction, regardless of the success or failure of the transaction, 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.

2.3 Isolation

Refers to 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.

The interaction between transactions is divided into several types, namely:
1) Dirty read: A transaction reads the uncommitted data of another transaction, and this data may be rolled back.
2) Non-repeatable read: Two identical queries within a transaction return different data. This is caused by the commit of other transaction modifications in the system during the query.
3) Phantom read: A transaction modifies the data in a table, and this modification involves all data rows in the table. At the same time, another transaction also modifies the data in this table. This modification is to insert a new row of data into the table. Then, the user operating the previous transaction will find that there are still unmodified data rows in the table, as if an illusion occurred.
4) Lost update: Two transactions read the same record at the same time. A first modifies the record, and B also modifies the record (B does not know that A has modified it). After B submits the data, the modification result of B overwrites the modification result of A.

Mysql and transaction isolation level:
1) read uncommitted: read uncommitted data, do not solve dirty read
2) read committed: read submitted data, can solve dirty read
3) repeatable read: reread read, can be solved Dirty reads and non-repeatable reads-------------mysql default
4) serializable: serialization, which can solve dirty reads, non-repeatable reads and virtual reads----------- -----Equivalent to lock table

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

Transaction isolation level Dirty read Non-repeatable Phantom reading The first type of update is lost The second type of update is lost
READ_UNCOMMITTED allow allow allow Prohibit allow
READ_COMMITTED Prohibit allow allow Prohibit allow
REPEATABLE_READ Prohibit Prohibit allow Prohibit Prohibit
SERIALIZABLE Prohibit Prohibit Prohibit Prohibit Prohibit

2.3.1 Transaction related query commands

  • Query the global transaction isolation level:
show global variables like '%isolation%';
或者
SELECT @@global.tx_isolation;

Insert picture description here

  • Query session transaction isolation level:
show session variables like '%isolation%';
SELECT @@session.tx_isolation; 
SELECT @@tx_isolation;

Insert picture description here

  • Set the global transaction isolation level:
set global transaction isolation level read committed;

Insert picture description here

  • Set the session transaction isolation level:
set session transaction isolation level read committed;

Insert picture description here

2.4 Endurance

  • 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.

3. Transaction control statement

1. Related sentences

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

2. Case

2.1 Create a table and add data

create database school;
use school;
create table Fmoney(
id int(10) primary key not null,  
name varchar(20),  
money decimal(5,2));

insert into Fmoney values ('1','zhangsan','200');
insert into Fmoney values ('2','lisi','200');
select * from Fmoney;

Insert picture description here

2.2 Test commit transaction

begin;
update Fmoney set money= money - 100 where name='zhangsan';
commit;
quit

mysql -u root -p
use school;
select * from Fmoney;

Insert picture description here

2.3 Test rollback transaction

begin;
update Fmoney set money= money - 50 where name='zhangsan';
select * from Fmoney;
rollback;

select * from Fmoney;

Insert picture description here

2.4 Test multi-point rollback

begin;
update Fmoney set money= money + 11 where name='zhangsan';
select * from Fmoney;
SAVEPOINT S1;
update Fmoney set money= money + 22 where name='lisi';
select * from Fmoney;
SAVEPOINT S2;
insert into Fmoney values(3,'wangwu',666);
select * from Fmoney;

ROLLBACK TO S1;
select * from Fmoney;

Insert picture description here

Insert picture description here

4. Use set to set the control transaction

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

Insert picture description here

Note: If automatic commit is not turned on, all operations of mysq1 connected to the current session will be treated as a transaction until you enter rollback I commit; the current transaction will not be considered as the end. 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 lrollback; are all independent affairs.

Two, MySQL storage engine

2.1 Introduction to storage engine concepts

1) The data in MySQL is stored in files using a variety of different technologies. Each technology uses different storage mechanisms, indexing techniques, and locking levels, and ultimately provides different functions and capabilities. These different technologies and supporting The function is called the storage engine in MySQL.

2) The storage engine is the storage method or storage format that MySQL stores data in the file system.
3) The commonly used storage engines of MySQL are:

  • MylSAM
  • InnoDB

4) The components in the MySQL database are responsible for performing the actual data I/O operations.
5) In the MySQL system, the storage engine is on the file system. Before the data is saved to the data file, it will be transferred to the storage engine, and then according to each storage engine Storage format for storage.

2.2 Introduction to the characteristics of MyISAM

1) MyISAM does not support transactions, nor does it support foreign key constraints. It only supports full-text indexing. The data files and index files are stored separately.
2) The access speed is fast and there is no requirement for transaction integrity.
3) MyISAM is suitable for query and insertion-based Application
4) MylSAM is stored in three files on the disk, the file name and table name are the same, but the extensions are:

  • .frm file storage table structure definition
  • The extension of the data file is .MYD (MYData)
  • The extension of the index file is .MYI (MYIndex)

5) Table-level locking mode, the entire table is locked when the data is updated
6) The database blocks each other in the process of reading and writing

  • Will block the reading of user data during the data writing process
  • It will also block the user's data writing during the data reading process

7) The data is written or read separately, the speed is faster and the resources are relatively small
8) The storage format supported by MyIAM

  • Static table
  • Dynamic table
  • Compression table

(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 takes up more space than the dynamic table .
(2) Dynamic table The
dynamic table contains variable fields, and the records are not of fixed length. The advantage of this storage is that it takes up less space. However, frequent updates and deletions of records will generate fragments. It is necessary to periodically execute the OPTIMIZETABLE statement or the myisamchk -r command. 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.

2.3 Examples of production scenarios applicable to MyISAM

1) The company's business does not require transaction support
2) Unilaterally reads or writes more data services
3) MylSAM storage engine data reads and writes are frequently read and write scenarios are not suitable
4) Use read and write concurrent access to relatively low business
5 ) Businesses with relatively few data modifications
6) Businesses that do not require very high data business consistency
7) Server hardware resources are relatively poor

2.4 InnoDB features introduction

1) Support transactions, support 4 transaction isolation levels
2) MySQL from version 5.5.5, the default storage engine is InnoDB
3) Read and write blocking is related to the transaction isolation level
4) Very efficient cache index and data
5) Table Stored in clusters with the primary key
6) Support for partitions and table spaces, similar to oracle database
7) Support for foreign key constraints, full-text indexing is not supported before 5.5, and full-text indexing after 5.5
8) Where hardware resource requirements are still relatively high
9) Row-level locks, but full table scans will still be table-level locks, such as

update table set a=1 where user like '%zhang%';

10) InnoDB does not save the number of rows of the table, such as select count(*) from table;, InnoDB needs to scan the entire table to calculate how many rows there are, but MyISAM simply reads the saved number of rows. It should be noted that *MyISAM also needs to scan the entire table when the count( ) statement contains the where condition.
11) For self-growing fields, InnoDB must contain an index with only this field, but it can be combined with other fields in the MyISAM table. Index
12) When emptying the entire table, InnoDB deletes row by row, which is very slow. MyISAM will rebuild the table.

2.5 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 queries are completed through indexes
3) Scenarios where business data is updated frequently

  • Such as: forums, Weibo, etc.

4) Higher business data consistency requirements

  • Such as: banking

5) The hardware device has a large memory, use InnoDB's better cache capacity to improve memory utilization and reduce the pressure of disk IO

2.6 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

  • All engines support common data types
  • But not all engines|engines support other field types, such as binary objects

3) Lock type: different storage engines support different levels of lock

  • 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 indexes at all

5) Support for transaction processing

  • Improve the reliability during the update and insertion of information to the table
  • The storage engine can be selected according to whether the enterprise business needs to support transactions

2.7 Commands for viewing and setting storage engine

1. Check the storage engines supported by the system

show engines;

Insert picture description here

2. View the storage engine used by the table

Method 1: View directly

show table status from 库名 where name='表名'\G;
例:
show table status from SCHOOL where name='class'\G;

Insert picture description here

Method 2: Enter the database to view

use 库名;
show create table 表名\G;

例:
use SCHOOL;
show create table class\G;

Insert picture description here

3. Modify the storage engine

Method 1: Modify through alter table

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

例:
use SCHOOL;
alter table class engine=MYISAM;

Insert picture description here

Method 2: By modifying the /etc/my.cnf configuration file, specify the default storage engine and restart the service

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

systemctl restart mysql.service

Note: This method is only valid for newly created tables after modifying the configuration file and restarting the mysql service. The existing tables will not be changed.

Insert picture description here

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

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

例:
mysql -u root -p
use SCHOOL;
create table test06(
id int(10) not null,
name varchar(20) not null
) engine=MyISAM;

Insert picture description here

Guess you like

Origin blog.csdn.net/qq_35456705/article/details/113994377