MySQL index, transaction and storage engine

One, MySQL index, transaction and storage engine

1. The concept of affairs

  • It 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 all Not executed
  • It is an inseparable logical unit of work. When performing concurrent operations on the database system, the transaction is the smallest unit of control
  • Suitable for scenarios where multiple users are simultaneously operating database systems, such as banks, insurance companies and securities trading systems, etc.
  • Ensure data consistency through the integrity of the transaction

2. Three paradigms of database tables

  • The first normal form (to ensure that each column maintains atomicity) atomicity guarantees that each element in the table can no longer be separated

  • The second paradigm (ensure that each column in the table is related to the primary key) each column and the primary key, not part of the data and the primary key can be related to determine the uniqueness of the entity

  • Third normal form (ensure that each column is directly related to the primary key column, not indirectly related)

3. ACID characteristics of transactions

1. Atomicity

  • 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

2. Consistency

  • 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

3. Isolation

  • 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

4. Durability

  • 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

4. Transaction control statement

  • MySQL transactions are automatically committed by default, and the transaction is automatically committed when the SQL statement is submitted
  • Transaction control statement
  1. BEGIN or START TRANSACTION-start
  2. COMMIT—commit, end of transaction
  3. ROLLBACK—Rollback
  4. SAVEPOINT identifier—Set the save point
  5. RELEASE SAVEPOINT identifier-delete the save point
  6. ROLLBACK TO identifier—roll back to a certain archive point
  7. SET TRANSACTION—Set transaction

5. Methods of manually controlling transactions

1. Transaction processing command control transaction

  • bebin: start a transaction
  • commit: commit a transaction
  • rollback: roll back a transaction

2. Use the set command to control (automatic submission by default)

  • set autocommit=0: Disable automatic submission
  • set autocommit=1: Turn on autocommit

6. Transaction control statement

Three situations end the transaction: rollback, commit, set autocommit=1

Two kinds of transaction start: begin, set autocommit=0, start transaction

Storage engine: innodb premise

7. Introduction to storage engine concepts

  • 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 Storage engine

  • The storage engine is the storage method or storage format in which MySQL stores data in the file system

  • MySQL commonly used storage engine
    1, MyISAM-does not support transactions
    2, InnoDB-supports transactions

  • Components in the MySQL database responsible for performing actual data I/O operations

  • In the MySQL system, the storage engine is above the file system, and the data will be transferred to the storage engine before being saved to the data file, and then stored according to the storage format of each storage engine

8. Introduction of MyISAM

  • MyISAM does not support transactions or foreign keys
  • Fast access
  • No requirement for transaction integrity
  • MyISAM is stored as E files on the disk.
    1, .frm file storage table definition
    2, the extension of the data file is .MYD (MYData)
    3, the extension of the index file is .MYI (MYIndex)
  • Table-level locking mode, the entire table is locked when the data is updated
  • The database blocks each other during the reading and writing process.
    1. It will block the user data reading during the data writing process.
    2. It will also block the user data writing during the data reading process.
  • Data is written or read separately, the speed process is faster and the resources are relatively small
  • Storage format supported by MyIAM
    1, static table
    2, dynamic table
    3, compressed table

9. Examples of production scenarios applicable to MyISAM

  • The company's business does not require the support of affairs
  • Services that unilaterally read or write more data
  • MyISAM storage engine data reads and writes are frequent, not suitable for scenarios
  • Use read and write concurrent access to relatively low business
  • Businesses with relatively little data modification
  • Data business-business that does not require very high compliance
  • Server hardware resources are relatively poor

10. InnoDB features introduction

  • Support 4 transaction isolation levels
  • Row-level locking, but full table scan will still be table-level locking
  • Read and write blocking is related to transaction isolation level
  • Can cache indexes and data very efficiently
  • Tables and primary keys are stored in clusters
  • Support partition and table space, similar to oracle database
  • Support foreign key constraints, full-text index is not supported before 5.5, full-text index is supported after 5.5
  • Where the hardware resource requirements are still relatively high

11. InnoDB applicable production scenario analysis

  • Business needs support
  • Row-level locking has good adaptability to high concurrency, but you need to ensure that the query is completed through the index
  • Scenarios where business data is updated frequently-such as forums, Weibo, etc.
  • Business data consistency requirements are high-such as: banking
  • Hardware devices have large memory, use Innodb's better caching capabilities to improve memory utilization and reduce the pressure on disk I0

12. The basis for enterprises to choose storage engines

  • Need to consider the core functions and application scenarios provided by each storage engine
  • 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
  • Lock type: different storage engines support different levels of lock
    1, table lock
    2, row lock
  • Index support
    1. Indexing can significantly improve performance when searching and restoring data in the database
    2. Different storage engines provide different indexing technologies
    3. Some storage engines do not support indexes at all
  • Transaction processing support
    1. Improve the reliability during the period of updating and inserting information in the table
    2. The storage engine can be selected according to whether the business needs to support transactions

13, modify the storage engine

  • Method 1: alter table to modify
    alter table table_name engine=engine;
  • Method 2: Modify my.cnf, specify the default storage engine and restart the service
    default-storage-engine=InnoDB
  • Method 3: Specify the storage engine when create table create table
    create table table name (field) engine= engine
  • Method 4: Mysql_ convert_ table_ format conversion storage engine (available in versions prior to 5.7, but not available in later versions)
    Mysql_ convert_ table_ format -user=root -password=password-sock=/tmp/mysql.sock-engine=engine library Watch name

mark

Guess you like

Origin blog.csdn.net/weixin_39608791/article/details/108097259