Mysql-storage engine

1. What is a storage engine

Library built in mysql ===> folder

Tables created in the library ===>Files

In real life, there are different types of files that we use to store data, and each file type corresponds to its own different processing mechanism: for example, txt type is used for processing text, excel is used for processing tables, and png is used for processing pictures, etc.

The tables in the database should also have different types. Different types of tables correspond to different access mechanisms of MySQL. The table types are also called storage engines.

 

To put it bluntly, the storage engine is the implementation
method . Because the storage of data in a relational database is stored in the form of a table, the storage engine can also be called a table type (that is,
the type that stores and manipulates this table)

In databases such as Oracle and SQL Server, there is only one storage engine, and all data storage management mechanisms are the same. The MySql
database provides a variety of storage engines. Users can choose different storage engines for data tables according to different needs, and users can also
write their own storage engines according to their needs

 

Components such as SQL parser, SQL optimizer, buffer pool, storage engine, etc. exist in every database, but not every database has so many storage engines. MySQL's plug-in storage engine allows developers of the storage engine layer to design the storage layer they want. For example, some applications need to meet the requirements of transactions, and some applications do not need such strong requirements for transactions; some hope that data It can be stored persistently, and some only want to be placed in memory to temporarily and quickly provide queries on data. 

2. Storage engines supported by mysql

MariaDB [(none)]> show engines\G #View all supported storage engines
MariaDB [(none)]> show variables like 'storage_engine%'; #View the storage engine in use

Introduction to MySQL storage engine

# The InnoDB storage engine 
supports transactions, and its design goal is mainly for online transaction processing (OLTP) applications. That
Features are row lock design, support for foreign keys, and support for Oracle-like non-locking reads, that is, the default read operation does not generate locks. Since MySQL version 5.5.8 is the default storage engine.
The InnoDB storage engine puts data in a logical tablespace, which is managed by the InnoDB storage engine itself like a black box. Starting from MySQL 4.1 (including 4.1 ), the tables of each InnoDB storage engine can be stored in a separate ibd file. In addition, the InnoDB storage engine supports the use of raw devices (row disks) for building its tablespaces.
InnoDB achieves high concurrency by using multi-version concurrency control (MVCC), and implements four isolation levels of the SQL standard, the default is the REPEATABLE level, and uses a strategy called netx-key locking to avoid phantom reads. ) phenomenon occurs. In addition, the InnoDB storage engine also provides high-performance and high-availability functions such as insert buffer, double write, adaptive hash index, and read ahead. Function.
For the storage of data in the table, the InnoDB storage engine adopts a clustered method, and each table is stored in the order of the primary key. If the primary key is not explicitly specified in the table definition, the InnoDB storage engine will be A row generates a 6 -byte ROWID and uses this as the primary key.
The InnoDB storage engine is the most commonly used engine for MySQL databases. The successful applications of Facebook, Google, Yahoo and other companies have proved that the InnoDB storage engine has high availability, high performance and high scalability. The mastery and understanding of its underlying implementation also requires the accumulation of time and technology. If you want to learn more about the working principle, implementation and application of the InnoDB storage engine, you can refer to the book "MySQL Technology Insider: InnoDB Storage Engine".

# MyISAM storage engine 
does not support transactions, table lock design, and full-text indexing, mainly for some OLAP database applications, and is the default storage engine before MySQL 5.5.8 (except for Windows versions). A big difference between the database system and the file system is the support for transactions. The MyISAM storage engine does not support transactions. At its core, this is not difficult to understand. Do users need transactions in all applications? In a data warehouse, if there is no ETL for these operations, do you need transaction support simply by querying reports? In addition, another unique feature of the MyISAM storage engine is that , its buffer pool only caches index files, not data files, which is different from most databases.

# NDB storage engine 
In 2003, MySQL AB acquired the NDB storage engine from Sony Ericsson. NDB storage engine is a cluster storage engine, similar to Oracle's RAC cluster, but different from Oracle RAC's share everything structure, its structure is a share nothing cluster architecture, so it can provide a higher level of high availability. The feature of the NDB storage engine is that all data is placed in memory (starting from version 5.1, non-indexed data can be placed on disk), so the speed of primary key lookups is extremely fast, and NDB data storage nodes can be added online (data node) to increase database performance linearly. It can be seen that the NDB storage engine is a database cluster system with high availability, high performance and high scalability, and it is also oriented to the database application type of OLTP.

# Memory storage engine 
As its name suggests, the data in the Memory storage engine is stored in memory, and the data in the table will disappear when the database restarts or crashes. It is very suitable for temporary tables for storing temporary data in OLTP database applications, and can also be used as dimension tables for data warehouses in OLAP database applications. The Memory storage engine uses hash indexes by default instead of the commonly familiar B+ tree indexes.

# Infobright Storage Engine 
Third-party storage engine. Its characteristic is that the storage is in accordance with columns rather than rows, so it is very suitable for OLAP database applications. Its official website is http://www.infobright.org/ , there are many successful data warehouse cases for analysis.

# NTSE storage engine 
NetEase developed a storage engine for its internal use. The current version does not support transactions, but provides features such as compression, row-level caching, and memory-oriented transaction support in the near future.

# BLACKHOLE 
black hole storage engine, which can be applied to the distribution master library in the primary and secondary replication.

There are many other storage engines for MySQL databases, the above are just some of the most commonly used engines. If you like, you can write your own engine completely. This is the ability that open source gives us, and it is also the charm of open source.
Introduction to specific storage engines

3. Use the storage engine

Method 1: Specify when creating the table

MariaDB [db1]> create table innodb_t1(id int,name char)engine=innodb;
MariaDB [db1]> create table innodb_t2(id int)engine=innodb;
MariaDB [db1]> show create table innodb_t1;
MariaDB [db1]> show create table innodb_t2;

Method 2: Specify the default storage engine in the configuration file

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

Check

[root@duoduo db1]# cd /var/lib/mysql/db1/
[root@duoduo db1]# ls
db.opt  innodb_t1.frm  innodb_t1.ibd  innodb_t2.frm  innodb_t2.ibd

practise

Create four tables and use innodb, myisam, memory, and blackhole storage engines respectively for insert data testing

MariaDB [db1]> create table t1(id int)engine=innodb;
MariaDB [db1]> create table t2(id int)engine=myisam;
MariaDB [db1]> create table t3(id int)engine=memory;
MariaDB [db1]> create table t4(id int)engine=blackhole;
MariaDB [db1]> quit
[root@duoduo db1]# ls /var/lib/mysql/db1/ #Found that the latter two storage engines only have table structure and no data
db.opt  t1.frm  t1.ibd  t2.MYD  t2.MYI  t2.frm  t3.frm  t4.frm

#memory, after restarting mysql or restarting the machine, the data in the table is cleared
#blackhole, inserting any data into the table is equivalent to throwing it into a black hole, and there will never be records in the table

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325848066&siteId=291194637