探究:Mysqlデータベースinnodbとmyisamの2種類のストレージエンジンauto_incrementプロパティの違い

3300 ワード

part 1:再起動の影響
ヘッダーとエンティティを含むビジネスオブジェクトのテーブルを作成するには、ヘッダーが必要です.fid=表体fid.1つの一般的な考え方は、単一のテーブルによってシード列を設定し、この個別のテーブルによってテーブルヘッダとテーブル体に必要なfidを取得し、取得後に個別のテーブルの記録を削除することである.この独立したテーブルはmyisamフォーマットを使用する必要があります.innodbはデータベースの再起動後にauto_を再編成するためです.incrementの次の値.
テスト手順は次のとおりです.
mysql> create table t_myisam(col1  int primary key auto_increment ,col2 char(1)) engine=myisam;
Query OK, 0 rows affected (0.01 sec)

mysql> create table t_innodb(col1  int primary key auto_increment ,col2 char(1)) engine=innodb;
Query OK, 0 rows affected (0.01 sec)

mysql> insert into  t_myisam(col2) values('a'),('b'),('c');
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> insert into  t_innodb(col2) values('a'),('b'),('c');
Query OK, 3 rows affected (0.03 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> delete from  t_myisam where col2='c';
Query OK, 1 row affected (0.00 sec)

mysql> delete from  t_innodb where col2='c';
Query OK, 1 row affected (0.02 sec)

mysql> exit;
Bye
[root@localhost ~]# service mysqld restart;
Redirecting to /bin/systemctl restart  mysqld.service
[root@localhost ~]# mysql -uroot -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.22 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> insert into  t_myisam(col2) values('e');
Query OK, 1 row affected (0.01 sec)

mysql> insert into  t_innodb(col2) values('e');
Query OK, 1 row affected (0.04 sec)

mysql> select * from t_myisam;
+------+------+
| col1 | col2 |
+------+------+
|    1 | a    |
|    2 | b    |
|    4 | e    |
+------+------+
3 rows in set (0.00 sec)

mysql> select * from t_innodb;
+------+------+
| col1 | col2 |
+------+------+
|    1 | a    |
|    2 | b    |
|    3 | e    |
+------+------+
3 rows in set (0.00 sec)

再起動後、システム検出t_innodbの最大idは2で,次の新規idは3である.myisamタイプのテーブルは再起動の影響を受けません.
part 2:インデックスへの依存
auto_incrementプロパティのカラム要件は、インデックスまたは複合インデックスの一部です.複合インデックスの場合、innodbストレージエンジンはカラムが複合インデックスの最初のカラムである必要がありますが、myisamストレージエンジンは他のカラムであってもよいです.auto_incrementプロパティのカラムが複合インデックスの最初のカラムでない場合、このシーケンスは私たちがよく使うテーブル全体のシーケンスとは異なり、パケットのシーケンスです.
mysql> create table t_myisam_mulindex(col1 int,col2 int auto_increment,col3 char(1),key(col1,col2)) engine=myisam;
Query OK, 0 rows affected (0.00 sec)

mysql> insert into t_myisam_mulindex(col1,col3) values(1,'a'),(1,'b'),(1,'c'),(2,'d'),(2,'e'),(3,'g');
Query OK, 6 rows affected (0.00 sec)
Records: 6  Duplicates: 0  Warnings: 0

mysql> select * from t_myisam_mulindex;
+------+------+------+
| col1 | col2 | col3 |
+------+------+------+
|    1 |    1 | a    |
|    1 |    2 | b    |
|    1 |    3 | c    |
|    2 |    1 | d    |
|    2 |    2 | e    |
|    3 |    1 | g    |
+------+------+------+
6 rows in set (0.00 sec)