MYSQL05 Advanced_View and modify storage engines, comparison between InnoDB and MyISAM, and introduction to other storage engines

①. View and modify storage engines

  • ①. Check what storage engine mysql provides
mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
  • ②. View the default storage engine
mysql> show variables like '%storage_engine%';
+---------------------------------+-----------+
| Variable_name                   | Value     |
+---------------------------------+-----------+
| default_storage_engine          | InnoDB    |
| default_tmp_storage_engine      | InnoDB    |
| disabled_storage_engines        |           |
| internal_tmp_mem_storage_engine | TempTable |
+---------------------------------+-----------+
4 rows in set (0.01 sec)

mysql> SELECT @@default_storage_engine;
+--------------------------+
| @@default_storage_engine |
+--------------------------+
| InnoDB                   |
+--------------------------+
1 row in set (0.00 sec)
  • ③. Modify the default storage engine.
    If the storage engine of the table is not explicitly specified in the statement to create the table, InnoDB will be used as the storage engine of the table by default. If we want to change the default storage engine of the table, we can write the command line to start the server like this:
# 临时修改
SET DEFAULT_STORAGE_ENGINE=MyISAM;
# 永久生效:或者修改 my.cnf 文件:
default-storage-engine=MyISAM
# 重启服务
systemctl restart mysqld.service
  • ④. Specify the storage engine when creating the table.
    The storage engine is responsible for extracting and writing data in the table. We can set different storage engines for different tables, which means that different tables can have different physical storage structures. , different extraction and writing methods
# 1. 创建表时指定存储引擎
CREATE TABLE 表名(
建表语句;
) ENGINE = 存储引擎名称;
# 2. 修改表的存储引擎
ALTER TABLE 表名 ENGINE = 存储引擎名称;

②. Comparison between InnoDB and MyISAM

  • ①. InnoDB engine: transaction storage engine with foreign key support function, row lock
  1. MySQL includes the InnoDB storage engine starting from 3.23.34. After 5.5 or greater, the InnoDB engine is used by default.
  2. InnoDB is MySQL's default transactional engine, which is designed to handle a large number of short-lived transactions. Can ensure complete commit and rollback of transactions
  3. In addition to adding and querying, update and delete operations are also required. Therefore, the InnoDB storage engine should be given priority. Unless there are very special reasons to use other storage engines, the InnoDB engine should be given priority.
  4. Data file structure:
    table name.frm stores the table structure (in MySQL8.0, merged into table name.ibd)
    table name.ibd stores data and indexes
  5. InnoDB is designed for maximum performance when handling huge data volumes
  6. Compared with MyISAM's storage engine, InnoDB's write processing efficiency is less efficient, and it takes up more disk space to save data and indexes.
  7. MyISAM only caches indexes, not real data. InnoDB not only caches indexes but also caches real data, which requires high memory, and the memory size has a decisive impact on performance.
  • ②. MyISAM engine: the main non-transaction storage engine
  1. MyISAM provides a large number of features, including full-text indexing, compression, spatial functions (GIS), etc., but MyISAM does not support transactions, row-level locks, and foreign keys . There is no doubt that MyISAM cannot safely recover after a crash.
  2. Default storage engine before 5.5
  3. The advantage is that the access speed is fast, there are no requirements for transaction integrity or applications that are mainly SELECT and INSERT
  4. There is additional constant storage for statistics. Therefore, the query efficiency of count(*) is very high
  5. Data file structure
    Table name.frm storage table structure
    Table name.MYD storage data (MYData)
    Table name.MYI storage index (MYIndex)
  6. Application scenarios: read-only applications or read-based businesses
  • ③. Comparison between MyISAM and InnoDB
    Insert image description here

③. Archive engine - archive

  • ①. Archive means archive and only supports two functions: insertion and query (rows cannot be modified after they are inserted)

  • ②. Support index function after MYSQL5.5

  • ③. It has a good compression mechanism and uses the zlib compression library to compress in real time when recording requests. It is often used as a warehouse.

  • ④. Acchive is suitable for log and data collection (archiving) applications. It is suitable for storing large amounts of independent data as historical records. It has a high insertion speed, but has poor support for queries.
    Insert image description here

④. Blackhole engine loses data

  • ①. The Blackhole engine does not implement any storage mechanism. It will discard all inserted data without any saving.

  • ②. The server will record the log of the Blackhole table, so it can be used to copy data to the standby database, or simply record it to the log. However, this application will encounter many problems, so it is not recommended.

⑤. CSV - Engine

  • ①. The use cases are as follows
mysql> SELECT @@default_storage_engine;
+--------------------------+
| @@default_storage_engine |
+--------------------------+
| InnoDB                   |
+--------------------------+
1 row in set (0.00 sec)

mysql>  CREATE TABLE test (i INT NOT NULL, c CHAR(10) NOT NULL) ENGINE = CSV;
Query OK, 0 rows affected (0.03 sec)

mysql> INSERT INTO test VALUES(1,'record one'),(2,'record two');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM test;
+---+------------+
| i | c          |
+---+------------+
| 1 | record one |
| 2 | record two |
+---+------------+
2 rows in set (0.00 sec)
  • ②. Creating a CSV table will also create a corresponding meta file, which is used to store the status of the table and the number of rows present in the table. The name of this file is the same as the name of the table, with the suffix CSM
    Insert image description here
  • ③. If you check the test.CSV file in the database directory created by executing the above statement, its content is opened with Notepad++ as follows:
"1","record one"
"2","record two"
  • ④. This format can be read and even written by spreadsheet applications such as Microsoft Excel. Use Microsoft Excel to open as shown in the figure
    Insert image description here

⑥. Memory engine - memory table

  • ①. The logical medium used by Memory is memory, and the response speed is very fast, but when the mysqld daemon crashes, the data will be lost. In addition, the data stored is required to be in a format with a constant data length. For example, Blob and Text type data are not available (the length is not fixed).

  • ②. Main features

  1. Memory supports both hash (HASH) index and B+ tree index
  2. Memory tables are at least an order of magnitude faster than MyISAM tables
  3. The size of the MEMORY table is limited. The size of the table mainly depends on two parameters, namely max_rows and max_heap_table_size. Among them, max_rows can be specified when creating the table; the size of max_heap_table_size defaults to 16MB and can be expanded as needed.
  4. Data files and index files are stored separately
  5. Disadvantages: Its data is easily lost and its life cycle is short. Based on this flaw, you need to be particularly careful when choosing a MEMORY storage engine.
  • ③. Scenarios using Memory storage engine
  1. The target data is relatively small and is accessed very frequently. The data is stored in the memory. If the data is too large, it will cause memory overflow. You can control the size of the Memory table through the parameter max_heap_table_size and limit the maximum size of the Memory table.
  2. If the data is temporary and must be available immediately, it can be placed in memory
  3. It doesn’t matter much if the data stored in the Memory table is suddenly lost.

⑦. Federated engine - access remote tables

  • The Federated engine is a proxy for accessing other MySQL servers. Although the engine seems to provide a good cross-server flexibility, it often causes problems and is therefore disabled by default.

⑧. Merge engine-manage multiple MyISAMs

  • Merge engine: manages a table collection composed of multiple MyISAM tables

⑨. NDB engine - dedicated to clusters

  • NDB engine is also called NDB Cluster storage engine. It is mainly used in MySQL Cluster distributed cluster environment, similar to Oracle's RAC cluster.

Guess you like

Origin blog.csdn.net/TZ845195485/article/details/131624285