MySQL Basics: What are the storage engines? Please talk about their characteristics and applicable scenarios.

Common MySQL storage engines

The MySQL database provides multiple storage engines, and each storage engine has its own characteristics and applicable scenarios. The following are some common MySQL storage engines:

  1. InnoDB:

    • Features: InnoDB is the default storage engine of MySQL. It supports transaction processing (ACID characteristics), provides row-level locking and foreign key constraints. InnoDB uses clustered indexes and supports crash recovery mechanisms and concurrency performance optimization.
    • Applicable scenarios: It is suitable for applications with high concurrency and transaction processing requirements, especially for scenarios with high data integrity and reliability requirements.
  2. MyISAM:

    • Features: MyISAM is another commonly used storage engine for MySQL. It has high insertion and query speed and supports full-text indexing. MyISAM does not support transaction processing and row-level locking, but has lower system resource consumption.
    • Applicable scenarios: Suitable for read-intensive applications, such as large data warehouses, log analysis, and report generation for read-only data.
  3. Memory (or Heap):

    • Features: The Memory storage engine stores tables in memory, provides very high read and write performance, and is suitable for processing temporary data and cached data. It does not support persistent storage, because the data is stored in memory, and the data will be lost after the database is restarted.
    • Applicable scenarios: suitable for temporary data storage, cache storage and storage of temporary calculation results, as well as applications that require very high read and write performance.
  4. Archive:

    • Features: The Archive storage engine has a high compression ratio and fast insertion speed, but the query performance is low. It is used to store large amounts of historical or archive data to save disk space.
    • Applicable scenarios: Suitable for storage of sparse or archival data, such as logging and audit data.

In addition to the above common storage engines, MySQL also provides other storage engines, such as CSV, Blackhole, Merge, etc. Each storage engine has its unique characteristics and applicable scenarios. Selecting a suitable storage engine requires evaluation and decision-making based on application requirements, data access patterns, and performance requirements.

The default storage engine before MySQL 5.5 and MySQL 8

Both MySQL 5 and MySQL 8 can use multiple storage engines, but their default storage engines are slightly different.

The default storage engine for MySQL 5.x is MyISAM. This means that in MySQL 5.x, if you do not specify a storage engine explicitly, the created table will use MyISAM as the default storage engine.

In the MySQL 8.x version, the default storage engine was changed to InnoDB. Since MySQL 5.5, the default storage engine has been changed to InnoDB, and MySQL 8.x continues this default setting.

It should be noted that both MySQL 5 and MySQL 8 support multiple storage engines, and you can choose a suitable storage engine according to your specific needs. If you want to explicitly specify a specific storage engine, you can use the ENGINE keyword to specify the desired storage engine when creating a table. For example, the following statement can be used to specify the use of the InnoDB storage engine when creating a table:

CREATE TABLE my_table (
  id INT,
  name VARCHAR(100)
) ENGINE=InnoDB;

To sum up, the default storage engine of MySQL 5 is MyISAM, while the default storage engine of MySQL 8 is InnoDB. However, you can still use a variety of different storage engines in both editions to meet specific needs.

globally specified storage engine

To specify a storage engine at one time, you can modify the MySQL configuration file (my.cnf or my.ini) to set the default storage engine. Here are the steps to specify the default storage engine in the configuration file:

  1. Locate and open the MySQL configuration file. On Linux systems, usually at /etc/mysql/my.cnfor /etc/my.cnf. On Windows systems, it is usually located at C:\Program Files\MySQL\MySQL Server 5.x\my.ini.

  2. Find [mysqld]the paragraph . If you don't have that section, you can add the following to the end of the file to create it:

[mysqld]
  1. [mysqld]Add the following line under the paragraph to specify the default storage engine. For example, if you want to set the default storage engine to InnoDB:
default_storage_engine=InnoDB
  1. Save the configuration file and restart the MySQL service for the configuration changes to take effect.

This way, each time a new table is created, MySQL will use the default storage engine set in the configuration file if you do not specify a storage engine explicitly.

It should be noted that even if the default storage engine is set, you can still use ENGINEthe keyword .

Guess you like

Origin blog.csdn.net/a772304419/article/details/131259853
Recommended