当前位置:网站首页>Mysql database - storage engine

Mysql database - storage engine

2022-06-21 21:12:00 [email protected]

1 MySQL Storage engine

1.1 Introduction to 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

1.2 MySQL Common storage engines

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
1、MyISAM
2、InnoDB

1.2.1 MyISAM

1、MyISAM Characteristics
MyISAM 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
MyISAM Suitable for inquiry 、 Insertion based applications

MyISAM 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 (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、MyISAM Table support 3 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 .

3、MyISAM 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

1.2.2 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 all Table scanning will still be table level locking , Such as
update table set a=1 where user like ‘%lic%’;

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 , however MyISAM 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 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 View storage engine

2.1 Check out the storage engines supported by the system

show engines;

2.2 Look at the storage engine that the table uses

Method 1

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

 example :show table status from SCHOOL where name='CLASS'\G;

Method 2

use  Library name ;
show create table  Table name ;

 example :use SCHOOL;
show create table CLASS;

3 Modify the storage engine

Method 1 : adopt alter table modify

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

 example :use SCHOOL;
alter table CLASS engine=myisam;
show create table CLASS;

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

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

systemctl restart mysqld.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 .

systemctl restart mysqld.service
 Remember to restart after modification mysql 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 .

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 hellolic (name varchar(10),age char(4))engine=myisam;
原网站

版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/172/202206211932347124.html