当前位置:网站首页>Database - MySQL storage engine (deadlock)

Database - MySQL storage engine (deadlock)

2022-07-06 04:25:00 Enthusiastic masses

Catalog

1、 Concept

( One ) Storage engine concept

  • MySQL The data in is stored in files with various technologies , Each technology uses a different storage mechanism 、 Indexing techniques 、 Lock levels and ultimately provide different functions and capabilities , These different technologies and supporting functions are in MySQL It's called storage engine in
  • The storage engine is MySQL Storage mode or storage format of data stored in file system
  • MySQL Common storage engines
    MyISAM
    InnoDB
  • MySQL Components in the database , Responsible for executing the actual data I/O operation
  • MySQL In the system , The storage engine is on top of the file system , The data is transferred to the storage engine before it is saved to the data file , Then, it is stored according to the storage format of each storage engine

( Two )MyISAM

1、MyISAM Introduction to the characteristics of

  • MyISAM Unsupported transaction , Foreign key constraints are not supported either , Full text index support , Data files and index files are kept separately
  • Fast access , There is no requirement for transaction integrity
  • MyISAM Suitable for inquiry 、 Insertion based applications
  • MyISAM On disk . Stored in three files on the , The file name and table name are the same , But the extensions are :
    1、.frm Definition of file storage table structure
    2、 The extension of the data file is .MYD(MYData)
    3、 The extension of the index file is .MYI(MYIndex)
  • Table level locking form , Lock the entire table when the data is updated
  • Databases block each other in the process of reading and writing
    It will block the reading of user data in the process of data writing
    It will also block the user's data writing in the process of data reading
  • Data is written or read separately , The process is fast and takes up less resources

2、MyIAM Supported storage formats

(1) static state ( Fixed length ) surface

  • Static tables are the default storage format . Fields in static tables are immutable , So every record is a fixed length , The advantage of this storage method is that the storage is very fast , Easy to cache , It's easy to recover in case of failure ; The disadvantage is that it usually takes up more space than a dynamic table .

(2) Dynamic table

  • Dynamic tables contain variable fields , Records are not fixed length , The advantage of this storage is that it takes up less space , But frequent updates 、 Deleting records will cause fragmentation , It needs to be carried out on a regular basis OPTIMIZE TABLE Sentence or myisamchk -r Command to improve performance , And it's relatively difficult to recover in case of failure .

(3) Compression meter

  • The compressed table consists of myisamchk Tool creation , Take up a very small space , Because each record is compressed individually , So there's only a very small cost of access .

3、MyISAM Examples of applicable production scenarios

  • Business doesn't need the support of business
  • Read or write data unilaterally
  • MyISAM The storage engine reads and writes data frequently, which is not suitable for the scenario
  • Using read-write concurrency to access relatively low business
  • Business with relatively little data modification
  • Services that do not require very high consistency of data services
  • The server hardware resources are relatively poor

( 3、 ... and )InnoDB

1、InnoDB characteristic

  • Support transactions , Support 4 Transaction isolation levels
  • MySQL from 5.5.5 Version start , The default storage engine is InnoDB
  • Read and write blocking is related to transaction isolation level
  • It can cache index and data very efficiently
  • Tables and primary keys are stored in clusters
  • Support partition 、 Table space , similar Oracle database
  • Support for foreign key constraints ,5.5 Full text indexing is not supported before ,5.5 Full text index is supported after
  • The requirement for hardware resources is relatively high
  • Row level locking , But full table scan will still be table level locking , Such as
    updata table set a=1 where user like ‘%zhang%’
  • InnoDB The number of rows in the table is not saved , Such as select count() from table; when ,InnoDB You need to scan the entire table to calculate how many rows there are , but MyISAM Simply read the number of lines of the save number , It should be noted that , When count() The statement contains where When the conditions MyISAM You also need to scan the entire table
  • For self growing fields ,InnoDB Must contain only the index of this field , But in MyISAM You can create a composite index with other fields in the table
  • When you empty the entire table ,InnoDB It's line by line deletion , Efficiency is very slow ,MyISAM The table is rebuilt

2、InnoDB It is suitable for production scenario analysis

  • Business needs transaction support
  • Row level locking has a good adaptability to high concurrency , But you need to make sure that the query is done through the index
  • Scenarios where business data is updated frequently
    Such as : Forum , Microblogging, etc
  • High requirements for business data consistency
    banking business
  • The memory of hardware device is large , utilize InnoDB Better cache capacity to improve memory utilization , Reduce disk IO The pressure of the

3、 Enterprises choose storage engine based on

  • We need to consider the different core functions and application scenarios provided by each storage engine
  • Supported fields and data types
    All engines support common data types
    But not all engines support other field types , Like binary objects
  • Lock type : Different storage engines support different levels of locking
    Table locking :MyISAM Support
    Row lock :InnoDB Support
  • Index support
    Indexing can significantly improve performance when searching and recovering data in a database
    Different storage engines provide different indexing techniques
    Some storage engines don't support indexing at all
  • Transaction processing support
    Improve reliability during updating and inserting information into tables
    The storage engine can be selected according to whether the enterprise business needs to support transactions

( Four ) View and set storage engine commands

1、 Check out the storage engines supported by the system

show engines;

     
  • 1.

 database ——MySQL Storage engine ( Deadlock )_ data

2、 Look at the storage engine that the table uses

(1) Method 1 : Direct view

show table status from  Library name  where name=' Table name '\G;
 example :
show table status from school where name='class'\G;

     
  • 1.
  • 2.
  • 3.

 database ——MySQL Storage engine ( Deadlock )_ Field _02

(2) Method 2 : Enter the database to view

use  Library name ;
show create table  Table name \G;

 example :
use school;
show create table class\G;

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

 database ——MySQL Storage engine ( Deadlock )_ data _03

3、 Modify the storage engine

(1) Method 1 : adopt alter table modify

use  Library name ;
alter table  Table name  engine=MyISAM;

 example :
use school;
alter table class engine=MYISAM;

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

 database ——MySQL Storage engine ( Deadlock )_ Field _04

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

Be careful : This method only changes the configuration file and restarts mysql The newly created table is valid after service , The existing table will not be changed .

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

systemctl restart mysql.service

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

 database ——MySQL Storage engine ( Deadlock )_ Field _05

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

use  Library name ;
create table  Table name ( Field 1  data type ,...) engine=MyISAM;

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

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

 database ——MySQL Storage engine ( Deadlock )_ data _06

Add : Deadlock 、innodb And myisam

innodb and myisam The difference between ?

1、 Focus on direction :myisam  Pay more attention to reading , and  innodb  Focus on writing 
2、 Search for / Access speed :myisam > innodb
3、innodb Support transactions ,myisam Unsupported transaction 
4、 integrity :myisam No requirements for integrity ( Unsupported transaction ,ACID characteristic ),innodb With good integrity ( Support transactions ,ACID characteristic )
5、 Security :innodb>myisam.( Different due to integrity , So security is also different , In the event of an accident ,innodb Than myisam It's easy to recover )
6、 Lock level :myisam Lock for table level ,innodb For row level locking ( Full table scanning is still table level locking )
7、 Anti concurrency ability :myisam Poor capacity and force ,innodb It can accommodate high concurrency 
8、 For hardware requirements :myisam The hardware requirements are not very high ,innodb High requirements for hardware 

 Contrast with these differences , The scenarios used are also different 
 for example :
1、innodb For example, microblog 、 Forums and other scenes with more reading and writing and frequent updates 
2、myisam For example, data archives 、 There are few modifications to data such as commodity warehouse 、 Scenarios that are read-only and do not support transactions 

     
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
myisaminnodb
Transaction and foreign key are not supported Support transactions and foreign keys
Table level locking Row level locking ( use like Fuzzy matching full table scanning is still table level locking )
I won't support it MVCC( Multi version concurrency control , Realized reading - Write , Write - Concurrent execution of reads ) Support MVCC
Full text index support 5.5 Full text indexing is not supported before version ,5.5 Post support
The hardware requirements are not very high , Low resource consumption High requirements for hardware , Especially memory , Can improve cache capacity
1. Table name .frm( File storage table structure ) 2. Table name .MYD (MYData Data files ) 3. Table name .MYI (MYIndex Index file )1.db.opt( Table properties file )2. Table name .frm( Table structure file )3. Table name .ibd( Table data metadata )
myisam For example, data archives 、 There are few modifications to data such as commodity warehouse 、 Mainly reading 、 Scenarios that read and insert separately and do not support transactions innodb For example, microblog 、 More reading and writing in forums 、 Scenarios that require high consistency and support transactions
myisam Pay more attention to reading innodb Focus on writing
myisam Search for 、 Fast access innodb Slightly slower
myisam No requirements for integrity ( Unsupported transaction ,ACID characteristic )innodb With good integrity ( Support transactions ,ACID characteristic )
myisam Low security , Slow recovery in case of accident innodb High safety , Recover faster in an accident
myisam Poor capacity and force innodb It can accommodate high concurrency

InnoDB The relationship between row lock and index

InnoDB Row locking is achieved by locking index entries , If there is no index ,InnoDB The record will be locked by a hidden cluster index .
1)delete from t1 where id=1;

If id Field is primary key ,InnoDB Clustered index is used for primary key , Will directly lock the whole line of records .

2)delete from t1 where name=‘aaa’ ;

If name Fields are normal indexes , Will lock the two rows of the index first , Then the record corresponding to the primary key will be locked .

3)delete from t1 where age=23;

If age Field has no index , Will use full table scanning to filter , At this time, all records on the form will be locked .
Deadlock
Deadlock : Deadlocks are transactions waiting for each other's resources , Finally formed a loop .

How to avoid deadlock as much as possible ?

1) Access tables and rows in a fixed order .

2) Big business, small business . Big business tends to deadlock , If night permits , Break up big business into small ones .

3) In the same transaction , Try to lock all the resources you need at once , Reduce deadlock probability .

4) Reduce isolation level . If business permits , Lowering the isolation level is also a good choice , Let's take the isolation level from RR Adjusted for RC, A lot can be avoided because gap A deadlock caused by a lock .

5) Add a reasonable index to the table . You can see that if you do not index, you will add locks for each row of the table , The probability of deadlock is greatly increased .

原网站

版权声明
本文为[Enthusiastic masses]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132228178347.html