MySQL transaction and storage engine

Transaction overview

A transaction is a mechanism, a sequence of operations comprises a set of database operations command and all commands to be submitted as a whole along with the request to undo or system, i.e., the set of database commands are either executed or not executed
transaction It is an inseparable work logic unit. When concurrent operations are performed on the database system, the transaction is the smallest control unit. It is
suitable for scenarios where multiple users are simultaneously operating database systems, such as banks, insurance companies, and securities trading systems
. Integrity to ensure data consistency.
If a part of the transaction is successful but part of it is unsuccessful, a rollback is performed, back to the starting point of the transaction, and the operation is restarted.

Three characteristics of transaction

Atomicity

A transaction is a complete operation. The elements of the transaction are indivisible (atomic)
. 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

Consistency

When the transaction is complete, the data must be in a consistent state!
Before the transaction starts, 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

Isolation

All concurrent transactions that modify data are isolated from each other, which means 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 these before another transaction that uses the same data starts. Data, or access the data after another transaction using the same data ends

Durability

Transaction durability means that the results of transaction processing are permanent regardless of whether the system fails or not.
Once the transaction is committed, the effect of the transaction will be permanently retained in the database

Transaction method

By default, MySQL transactions are automatically submitted. When the sql statement is submitted, the transaction is automatically submitted

Common transaction control methods

The method of manually controlling the
transaction Transaction processing command control
Use set to set the transaction processing method

Control transaction commands

bein: start the transaction
commit: commit the transaction
rollback: roll back
savepoint: create a rollback point
Use the set command to control
set autocommit=0: disable automatic commit
set autocommit=1: enable automatic commit

Note: Commit and rollback can end the transaction and retain data, but rollback to archive point cannot

bring it on! Show!

mysql> CREATE DATABASE GUNPLA;
Query OK, 1 row affected (0.00 sec)

mysql> BEGIN;    ##开始事务
Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE GAODA(ID INT(3) NOT NULL AUTO_INCREMENT PRIMARY KEY,NAME VARCHAR(20) NOT NULL,BRITH VARCHAR(10) DEFAULT '未知');
Query OK, 0 rows affected (0.01 sec)

mysql> commit;  ##提交事务
Query OK, 0 rows affected (0.00 sec)

mysql> begin;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO GAODA VALUES(1,'00Q','2010');
Query OK, 1 row affected (0.02 sec)

##查看当前表内容
mysql> SELECT * FROM GAODA;
+----+------+-------+
| ID | NAME | BRITH |
+----+------+-------+
|  1 | 00Q  | 2010  |
+----+------+-------+
1 row in set (0.00 sec)

##回滚
mysql> ROLLBACK ;
Query OK, 0 rows affected (0.00 sec)

##再次查寻表内容
mysql> SELECT * FROM GAODA;
Empty set (0.00 sec)

##开始事务
mysql> BEGIN;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO GAODA VALUES(1,'00Q','2010');
Query OK, 1 row affected (0.00 sec)

##创建存档点 H1
mysql> SAVEPOINT H1;
Query OK, 0 rows affected (0.00 sec)

mysql> INSERT INTO GAODA VALUES(2,'EXIA','2008');
Query OK, 1 row affected (0.00 sec)

##查看表内容
mysql> INSERT INTO GAODA VALUES(1,'00Q','2010');
ERROR 1062 (23000): Duplicate entry '1' for key 'PRIMARY'
mysql> SELECT * FROM GAODA;
+----+------+-------+
| ID | NAME | BRITH |
+----+------+-------+
|  1 | 00Q  | 2010  |
|  2 | EXIA | 2008  |
+----+------+-------+
2 rows in set (0.00 sec)

##回滚到 H1
mysql> ROLLBACK TO H1;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT * FROM GAODA;
+----+------+-------+
| ID | NAME | BRITH |
+----+------+-------+
|  1 | 00Q  | 2010  |
+----+------+-------+
1 row in set (0.00 sec)

Storage engine

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 are in MySQL The
storage engine is the storage method or storage format in which MySQL stores data in the file system.

Two storage engines commonly used by MySQL,
MyISAM
InnoDB
MySQL storage engine is a component in the MySQL database server, responsible for performing actual data I/O operations for the database

One of the main advantages of using a special storage engine is that it
only needs to provide the characteristics required by the special application
. The system overhead in the database is small
. The database performance is more effective and higher. In the
MySQL system, the storage engine is above the file system. Before saving to a data file, it will be transferred to the storage engine, and then stored according to the storage format of each storage engine

Introduction to MyISAM

MyISAM storage engine is the default storage engine of MySQL relational database system before version 5.5. The predecessor is ISAM.
ISAM is a well-defined and time-tested data table management method. The design takes into account that the number of times the database is queried is much greater than it is updated. the number of
ISAM features
advantages: ISAM speed read operation is performed quickly
advantage: do not take up a lot of memory and storage resources
disadvantages: does not support transactions
Cons: can not fault tolerant
MyISAM manage non-transactional table is lSAM extended format
to provide ISAM A large number of functions of index and field management that are not available in
MyISAM uses a table locking mechanism to optimize multiple concurrent read and write operations.
MyISAM provides high-speed storage and retrieval, as well as full-text search capabilities, and is favored by web development.

MyISAM features

Does not support transaction
table-level locking. The entire table is locked when the data is updated. The
database blocks each other during the read and write process. It
blocks the user
data reading during the data writing process and also blocks the user data writing during the data reading process. The
cache index can be set by key_buffer_size to improve access performance and reduce disk I/O pressure.
However, the cache only caches index files, and does not cache data. The
MyISAM storage engine data is used to write or read separately. The speed process is faster and The
MyISAM storage engine does not support foreign key constraints and only supports full-text indexing.
Each MyISAM is stored in three files on the disk. The name of each file starts with the name of the table. The extension indicates the file type
MyISAM is on the disk. Stored file.
Frm file storage table definition
Data file extension is .MYD (MYData)
index file extension is .MYI (MYIndex)

MyISAM is suitable for production scenarios

The company’s business does not require transaction support.
Generally, businesses that unilaterally read more data or unilaterally write more data.
MyISAM storage engine data read and write are more frequent. Scenarios are not suitable for
businesses with relatively low concurrent read and write access.
Services
with relatively little data modification, services that do not require very high data service consistency,
server hardware resources are relatively poor

InnoDB introduction

InnoDB features

Support transaction: supports 4 transaction isolation levels.
Row-level locking, but full table scan will still be table-level locking.
Read-write blocking is related to transaction isolation level. It
has very efficient caching features: it can cache indexes, data
tables and primary keys. Clustered storage
Supports partitions and table spaces, similar to oracle databases.
Supports foreign key constraints. Full-text indexing is not supported before 5.5. Full-text indexing is supported after version 5.5. Where
hardware resource requirements are still relatively high

InnoDB applicable to production scenarios

Business needs transaction support.
Row-level locking has a good ability to adapt to high concurrency, but it is necessary to ensure that the query is completed through the index. The
business data update is more frequent, such as: forums, microblogs and other
business data consistency requirements are high. For example: Banking business
hardware equipment with large memory, use Innodb's better cache capacity to improve memory utilization and reduce disk I/O pressure

Selection basis for common storage engines in production environments

Need to consider what different core functions and application scenarios each storage engine provides

Supported fields and data types
All engines support common data types,
but not all engines support other field types, such as binary objects

Lock type: Different storage engines support different levels of locking,
table locking,
row locking

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.

Transaction processing support

事务处理功能通过提供在向表中更新和插入信息期间的可靠性
可根据企业业务是否要支持事务选择存储引擎

View the storage engine of the table

mysql> SHOW CREATE TABLE GAODA(表名);
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                    |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| GAODA | CREATE TABLE "GAODA" (
  "ID" int(3) NOT NULL AUTO_INCREMENT,
  "NAME" varchar(20) NOT NULL,
  "BRITH" varchar(10) DEFAULT '未知',
  PRIMARY KEY ("ID")
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8   |
+-------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

Modify storage engine

Method 1
Direct modification

alter table 表名 engine=引擎;
mysql> ALTER TABLE GAODA ENGINE=MYISAM;
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

Method two
modify the configuration file

[root@5centos etc]# vim /etc/my.cnf
[mysqld]
……省略部分……
default-storage-engine=InnoDB

Method 3
Specify the engine when creating the table

mysql> CREATE TABLE YINQING(ID INT(3) NOT NULL PRIMARY KEY AUTO_INCREMENT ,
    -> NAME VARCHAR(20) NOT NULL,
    -> AGE INT(3) 
    -> )ENGINE=MYISAM;
Query OK, 0 rows affected (0.00 sec)

Guess you like

Origin blog.csdn.net/Ora_G/article/details/108092272