MySQL basic study notes-the choice of table type (storage engine)

Selection of table type (storage engine)

1. Overview of MySQL storage engine

The plug-in storage engine is one of the most important features of the MySQL database. Users can choose how to store and index data, whether to use transactions, etc. according to the needs of the application. MySQL supports multiple storage engines by default to meet the needs of database applications in different fields.

Storage engines supported by MySQL 5.x include MyISAM, InnoDB, BDB, MEMORY, MERGE, EXAMPLE, NDB Cluster, ARCHIVE, CSV, BLACKHOLE, FEDERATED, etc. Among them, InnoDB and BDB provide transaction-safe tables .

If you do not specify a storage engine when creating a table, the system will use the default storage engine. The default engine of MySQL is InnoDB. If you want to modify the default storage engine, you can set default_storage_engine in the parameter file. To view the current default storage engine, you can use the following command:

mysql> show variables like '%storage_engine%';
+----------------------------------+--------+
| Variable_name                    | Value  |
+----------------------------------+--------+
| default_storage_engine           | InnoDB |
| default_tmp_storage_engine       | InnoDB |
| disabled_storage_engines         |        |
| internal_tmp_disk_storage_engine | InnoDB |
+----------------------------------+--------+
4 rows in set (0.00 sec)

You can query the storage types supported by the current database through the following methods:

mysql> show engines \G
*************************** 1. row ***************************
      Engine: InnoDB
     Support: DEFAULT
     Comment: Supports transactions, row-level locking, and foreign keys
Transactions: YES
          XA: YES
  Savepoints: YES
*************************** 2. row ***************************
      Engine: MRG_MYISAM
     Support: YES
     Comment: Collection of identical MyISAM tables
Transactions: NO
          XA: NO
  Savepoints: NO
*************************** 3. row ***************************
      Engine: MEMORY
     Support: YES
     Comment: Hash based, stored in memory, useful for temporary tables
Transactions: NO
          XA: NO
  Savepoints: NO
...

When creating a table, you can set the storage engine of the new table by adding the ENGINE keyword, for example:

mysql> create table ai(
    -> i bigint(20) NOT NULL AUTO_INCREMENT,
    -> PRIMARY KEY(i)
    -> )ENGINE = MyISAM DEFAULT charset = gbk;
Query OK, 0 rows affected (0.00 sec)
mysql> create table country(
    -> country_id SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
    -> conutry VARCHAR(50) NOT NULL,
    -> last_update TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    -> PRIMARY KEY(country_id)
    -> )ENGINE=InnoDB DEFAULT charset=gbk;
Query OK, 0 rows affected (0.01 sec)

You can use alter table to modify an existing table into another storage engine:

mysql> alter table ai engine = innodb;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table ai \G
*************************** 1. row ***************************
       Table: ai
Create Table: CREATE TABLE `ai` (
  `i` bigint(20) NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`i`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk
1 row in set (0.00 sec)

2. Features of various storage engines

Features MyISAM InnoDB MEMORY MEGRE NDB
Storage limit Have 64TB Have No Have
Transaction security stand by
Lock mechanism Table lock Row lock Table lock Table lock Row lock
B-tree index stand by stand by stand by stand by stand by
Hash index stand by stand by
Full-text index stand by
Cluster index stand by
Index cache stand by stand by stand by stand by stand by
Data can be compressed stand by
Space usage low high N/A low low
Memory usage low high medium low high
Bulk insert speed high low high high high
Support foreign keys stand by

2.1、MyISAM

MyISAM does not support transactions, nor does it support foreign keys. Its advantage is that it can be accessed quickly, and applications that do not require transaction integrity or mainly SELECT and INSERT can basically use this engine to create tables .

Each MyISAM is stored as 3 files on the disk, the file names are the same as the table names, but the extensions are:

  • .frm (storage table definition);
  • .MYD (MYData, storage data);
  • .MYI (MYIndex, storage index).

Data files and index files can be placed in different directories, and I/O can be evenly distributed for faster speed.

To specify the path of the index file and data file, you need to specify the DATA DIRECTORY and INDEX DIRECTORY statements when creating the table.

The MyISAM type table may be damaged due to many reasons. The damaged table may not be accessed, and it will prompt that it needs to be repaired or the wrong result will be returned after access. The MyISAM type table provides a repair tool. You can use the CHECK TABLE statement to check the health of the MyISAM table, and use the REPAIRE TABLE statement to repair a damaged table .

MyISAM tables also support 3 different storage formats:

  • Static (fixed length) table;
  • Dynamic table
  • Compress the table.

Among them, the static table is the default storage format. The fields in the static table are all non-variable length fields, so that each record has a fixed length. The advantage of this storage method is that the storage is very fast, easy to cache, and easy to recover from failure; the disadvantage is that it usually takes up more space than dynamic tables . The data of the static table will be filled with spaces according to the column width definition when it is stored, but these spaces will not be obtained when the application accesses them. These spaces have been removed before being returned to the application.

mysql> create table Myisam_char(name char(10))engine=MyISAM;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into Myisam_char values('abcde'),('abcde  '),('  abcde'),(' abcde ');
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> select name,length(name) from Myisam_char;
+---------+--------------+
| name    | length(name) |
+---------+--------------+
| abcde   |            5 |
| abcde   |            5 |
|   abcde |            7 |
|  abcde  |            6 |
+---------+--------------+
4 rows in set (0.00 sec)

The above example shows that the spaces after the inserted record have been removed, and the spaces in front are reserved .

The dynamic table contains variable-length fields, and the record is not of a fixed length. The advantage of this storage is that it takes up less space, but frequent updates and deletions of records will generate fragmentation. You need to periodically execute the OPTIMIZE TABLE statement or the myisamchk -r command to improve Performance, and recovery in the event of a failure is relatively difficult .

The compressed table is created by the myisampack tool and takes up very little disk space .

2.2、InnoDB

The InnoDB storage engine provides transaction security with commit, rollback, and crash recovery capabilities . However, compared with the storage engine of MyISAM, InnoDB write processing efficiency will be worse, and will take up more disk space to retain data and indexes.

The following focuses on the characteristics of the storage engine InnoDB tables that are different from those of other storage engines during use:

2.2.1, automatic growth column

For columns with automatic growth, you can manually insert values, but when you insert 0 or empty, the actual inserted value will be automatically increased .

mysql> create table autoincre_demo(
    -> i smallint not null auto_increment,
    -> name varchar(10),primary key(i)
    -> )engine = innodb;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into autoincre_demo values(1,"1"),(0,'2'),(null,'3'),(5,'4'),(0,'5');
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0

mysql> select * from autoincre_demo;
+---+------+
| i | name |
+---+------+
| 1 | 1    |
| 2 | 2    |
| 3 | 3    |
| 5 | 4    |
| 6 | 5    |
+---+------+
5 rows in set (0.00 sec)

You can use the "alter table table name auto_increment=n;" statement to forcefully set the initial value of the auto-increment column. The default starts from 1, but the initial value is kept in memory . If the value is used before the database restarts, then this is mandatory The default value will be lost.

You can use last_insert_id() to query the value used by the last record inserted by the current thread. If multiple records are inserted at a time, the auto-increment value used by the first record is returned.

mysql> insert into autoincre_demo values(7,"7");
Query OK, 1 row affected (0.00 sec)

mysql> select LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                2 |
+------------------+
1 row in set (0.00 sec)

mysql> insert into autoincre_demo(name) values('8'),('9');
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                8 |
+------------------+
1 row in set (0.00 sec)

For InnoDB tables, the automatic growth column must be an index . If it is a composite index, it must also be the first column of the composite index . But for MyISAM tables, the auto-growth column can also be other columns of the composite index, so that after data is inserted, the auto-growth column is sorted according to the first few columns of the composite index and then incremented . E.g:

mysql> create table autoincre_myisam(
    -> d1 smallint not null auto_increment,
    -> d2 smallint not null,
    -> name varchar(10),
    -> index(d2,d1)		#自动增长列作为了组合索引的第二列
    -> )engine = myisam;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into autoincre_myisam(d2,name) values(2,'2'),(3,'3'),(4,'4'),(2,'2'),(3,'3'),(4,'4');
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select * from autoincre_myisam;
+----+----+------+
| d1 | d2 | name |
+----+----+------+
|  1 |  2 | 2    |
|  1 |  3 | 3    |
|  1 |  4 | 4    |
|  2 |  2 | 2    |
|  2 |  3 | 3    |
|  2 |  4 | 4    |
+----+----+------+
6 rows in set (0.00 sec)
mysql> create table autoincre_innodb(
    -> d1 smallint not null auto_increment,
    -> d2 smallint not null,
    -> name varchar(10),
    -> index(d2,d1)
    -> )engine = innodb;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key		#提示自动增长列必须是组合索引的第一列

2.2.2, foreign key constraints

MySQL supports the storage of foreign keys only InnoDB. When creating a foreign key, the parent table must have a corresponding index, and the child table will automatically create a corresponding index when creating a foreign key.

The following example: There are two tables in the database, country is the parent table, country_id is the primary key index, city is the child table, and the country_id field is the foreign key, which corresponds to the country_id primary key of the country table.

mysql> create table city(
    -> city_id smallint unsigned not null auto_increment,
    -> city varchar(50) not null,
    -> country_id smallint unsigned not null,
    -> last_update timestamp not null default current_timestamp on update current_timestamp,
    -> primary key(city_id),
    -> foreign key (country_id) references country (country_id) on delete restrict on update cascade
    -> )engine=InnoDB default charset=utf8;
Query OK, 0 rows affected (0.00 sec)

When creating an index, you can specify the corresponding operations performed on the child table when deleting or updating the parent table, including RESTRICT, CASCADE, SET NULL, and NO ACTION. Among them, Restrict and NO ACTION are the same, which means that the parent table cannot be updated when the child table has related records; CASCADE means that when the parent table is updated or deleted, the corresponding record from the table is updated or deleted; SET NULL means that the parent table is in When updating or deleting, the corresponding field of the sub-table is SET NULL.

2.2.3, storage method

InnoDB storage tables and index tables have the following two storage methods:

  • Using shared table space storage, the table structure of the table created in this way is stored in the .frm file, and the data and indexes are stored in the table space defined by innodb_data_home_dir and innodb_data_file_path, which can be multiple files.
  • Using multi-table space storage, the table structure of the table created in this way is still stored in the .frm file, but the data and index of each table are stored separately in the .idb. If it is a partition table, each partition corresponds to a separate .idb file, the file name is "table name + partition name", you can specify the location of the data file of each partition when creating the partition, in order to change the IO of the table Evenly distributed on multiple disks.

2.3、MEMORY

The MEMORY storage engine uses content that exists in memory to create tables. Each MEMORY table actually corresponds to only one disk file, the format is .frm. MEMORY type table access is very fast, because its data is stored in memory, and the HASH index is used by default, but once the service is closed, the data in the table will be lost.

mysql> create table tab_memory engine = memory
    -> select city_id ,city,country_id
    -> from city group by city_id;
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select count(*) from tab_memory;
+----------+
| count(*) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

mysql> show table status like 'tab_memory' \G
*************************** 1. row ***************************
           Name: tab_memory
         Engine: MEMORY
        Version: 10
     Row_format: Fixed
           Rows: 0
 Avg_row_length: 155
    Data_length: 0
Max_data_length: 16252835
   Index_length: 0
      Data_free: 0
 Auto_increment: NULL
    Create_time: 2020-08-19 08:24:33
    Update_time: NULL
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.00 sec)

Guess you like

Origin blog.csdn.net/qq_36879493/article/details/108092169