Introduction to MySQL Database Engine

Simply put, when you access the database, whether it is manual access or program access, you do not directly read and write database files, but access database files through the database engine. Taking a relational database as an example, you send SQL statements to the database engine, and the database engine interprets the SQL statements, extracts the data you need and returns it to you. Therefore, to the visitor, the database engine is the interpreter of the SQL statement.


The architecture of
mysql is composed of management service and tool components, connection pool components, sql interface components, query analyzer components, optimizer components, cache components, plug-in storage engines, and physical files.




1. What is a storage engine?
Data in MySQL is stored in files (or in memory) using a variety of different technologies, each of which uses different storage mechanisms, indexing techniques, locking levels and ultimately provides different functions and capabilities. The technology and supporting related functions are called storage engines (also known as table types) in MySQL.

MySql supports multiple storage engines, including an engine that handles transaction-safe tables and an engine that handles non-transaction-safe tables:
  1. ISAM is a well-defined and time-tested data table management method. The number of queries is much greater than the number of updates. As a result, ISAM performs read operations very fast and does not consume a lot of memory and storage resources. The two main drawbacks of ISAM are that it doesn't support transactions, and it's not fault-tolerant : if your hard drive crashes, the data files can't be recovered.

  2. MyISAM manages non-transactional tables and is an extended format of ISAM. In addition to providing extensive functionality for index and field management not found in ISAM, MyISAM also uses a table locking mechanism to optimize multiple concurrent read and write operations. It provides high-speed storage and retrieval, and full-text search capabilities, favored by web developers. MyISAM is supported in all MySQL configurations and is the default storage engine.

  3. The MEMORY storage engine provides "in-memory" tables, is officially identified as a HEAP engine, and also handles non-transactional tables. HEAP allows temporary tables that only reside in memory. Being in-memory makes HEAP faster than both ISAM and MyISAM, but the data it manages is volatile and all data is lost if not saved before shutdown. HEAP also doesn't waste a lot of space when rows are deleted. HEAP tables are useful when you need to use SELECT expressions to select and manipulate data. Remember to delete the form when you are done using it.

  4. InnoDB and BDB (BerkleyDB) storage engines provide transaction-safe tables. Although much slower than the ISAM and MyISAM engines, InnoDB and BDB include support for transactions and foreign keys that the previous two engines did not.

  5. NDB Cluster is a storage engine used by MySQLCluster to implement tables divided into multiple computers. Currently, it is only supported by Linux, Solaris and Mac OS X.



2. How to replace the engine?
  1. Globe: One of the easiest ways is to change the server configuration and set it directly to the engine you need. This can be done by changing the default-storage-engine item in mysql.ini or my.ini in the server installation directory under win, or you can make simple settings by running the MySQL ServerInstance Configuration Wizard.

  2. PerTable: In addition to the global method, there is a more flexible configuration method, which is to set the engine according to the table, so that we can set the tables that need to use transaction processing to InnoDB, and the others to MyISAM. The performance is improved to the extreme, and the setting method is relatively simple:
  when creating a new table, you can tell MySQL what type of table to create by using the ENGINE or TYPE option in the CREATE statement:
  CREATE TABLE t (i INT) ENGINE = INNODB;
  CREATE TABLE t (i INT) TYPE = MEMORY;
  If the ENGINE or TYPE option is omitted, the default storage engine is used. When MySQL is installed on Windows platforms using the MySQL Configuration Wizard, the InnoDB storage engine replaces the MyISAM storage engine as the default. When an unavailable type is specified, an InnoDB table is automatically used instead.

  3. You can also transfer a table from one type to another, using the ALTERTABLE statement:
  ALTER TABLE t ENGINE = MYISAM;
  ALTER TABLE t TYPE = BDB;
  SHOW TABLE STATUS FROMDBname can be used when the engine of each table in the current database is unclear to check.



3. How to choose a storage engine?
Regarding this issue, we need to consider what different core functions each storage engine provides. Generally, these core functions are divided into 4 categories: supported fields and data types, locking types, indexes, and transaction processing.

1. Supported fields and data types
Although all of these engines support common data types, such as integer, real, and character, not all engines support other field types, especially BLOG (binary large object) or TEXT text type.

2. Locking type
The locking mechanism is mainly to prevent multiple processes from updating the same data at the same time. Different storage engines support different levels of locking: table locking, page locking, and row locking.
  Table locking: low overhead and fast locking; no deadlock; large locking granularity, the highest probability of lock conflict, and the lowest concurrency. The most supported is table locking, which is supported by MyISAM and MEMORY. There are two modes of table-level locks in MySQL: table shared read locks and table exclusive write locks. The read operation of the MyISAM table will not block the read requests of other users to the same table, but will block the write request of the same table; the write operation of the MyISAM table will block the read and write operations of other users to the same table; MyISAM table read and write operations, and between write operations are serial!
  Row locking: high overhead, slow locking; deadlocks; the smallest locking granularity, the lowest probability of lock conflicts, and the highest concurrency. InnoDB tables do row-level locking.
  Page locking: Overhead and locking time are between table locks and row locks; deadlocks can occur; locking granularity is between table locks and row locks, and the degree of concurrency is average. The BerkeleyDB engine supports page locking.

3. Indexing
Indexing can significantly improve performance when searching and restoring data in the database. Different storage engines provide different techniques for making indexes. Some storage engines do not support indexes at all.

4. Transaction processing
The transaction processing function provides reliability during updates and insertions of information into tables.



Guess you like

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