当前位置:网站首页>Concept of storage engine

Concept of storage engine

2022-07-06 01:58:00 Crazy Stuka

The concept of storage engine

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
MylSAM
lnnoDB

MyISAM Introduction to the characteristics of

MylSAM Unsupported transaction , Foreign key constraints are not supported either , Full text indexing only , Data files and index files are kept separately 
 Fast access , There is no requirement for transaction integrity MylSAM Suitable for inquiry 、 Insertion based applications 
MylSAM Store three files on disk , The file name and table name are the same , But the extensions are :
.frm Definition of file storage table structure 
 The extension of the data file is .MYD (MYData)
 The extension of the index file is .MYI (MYlndex)

 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 

MylSAM Three different 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 .

lnnoDB 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 indexes, data tables and primary keys very efficiently and store them 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 After supporting full-text indexing, the hardware resource requirements are still relatively high 
 Row level locking , But full table scan will still be table level locking 

lnnoDB The number of rows in the table is not saved , Such as select count(*) from table; when , lnnoDB Need to scan ─ Go through the whole table to calculate how many rows there are , however MylSAM Simply read out the number of saved lines . 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 A combined index can be established with other fields in the table. When emptying the whole table ,InnoDB It's line by line deletion , Efficiency is very slow .MylSAM The table is rebuilt 


Check out the storage engines supported by the system

show engines;

 Insert picture description here

1. Look at the storage engine that the table uses

Method 1 :

show table status from  Library name  where name=' Table name '\G

 Insert picture description here

Method 2 :

use  Library name ;
show create table  Table name ;

 Insert picture description here


2. Modify the storage engine

(1). adopt alter table modify

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

 Insert picture description here

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

 Insert picture description here

 Insert picture description here

3. adopt create table Specify the storage engine when creating the table

 Insert picture description here


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 .

 Insert picture description here

 Insert picture description here


Deadlock

Deadlocks are transactions waiting for each other's resources , Finally formed a loop .

Case study

create table t1(id int primary key,name char(3), age int);
insert into t1 values(1, 'aaa',22);
insert into t1 values(2, 'bbb',23);
insert into t1 values(3, 'aaa',24);
insert into t1 values(4, 'bbb',25);
insert into t1 values(5, 'ccc',26);
insert into t1 values(6, 'zzz',27);

 conversation 1:      conversation 2
beging;    begin;

 conversation 1
delete from t1 where id=5;

 conversation 2
select * from t1 where id=1 for update;

 conversation 1
delete from t1 where id=1;   ## A deadlock occurs 

 conversation 2
update t1 set name='qqq' where id=5;    ## A deadlock occurs 

for update  An exclusive lock can be placed on a row in the database , When the operation of a transaction is not completed , Other transactions can be read but not written or updated 

 Insert picture description here

 Insert picture description here

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 business 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 Ra< Adjusted for Rc, A lot can be avoided because gap A deadlock caused by a lock .
5) Add a reasonable index to the table . If you do not use indexes, you will add locks to each row of records in the table , The probability of deadlock is greatly increased .
原网站

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