foreword
Different storage engine files are different, we can view the data file directory:
show VARIABLES LIKE 'datadir';
Each InnoDB table has two files ( .frm and .ibd ),
MyISAM tables have three files (.frm, .MYD, .MYI).
One is the same file .frm
, .frm
which is the file defining the table structure in MySQL. No matter which storage engine you choose when creating the table, it will be generated, so we will not read it.
Let's mainly look at how the other two files implement the indexes of different storage engines of MySQL.
Let's take a look at MyISAM first.
MyISAM
Inside MyISAM, there are two other files:
-
One is
.MYD
a file, and D stands for Data, which is a MyISAM data file that stores data records, such as all table data in our user_myisam table. -
One is
.MYI
the file, I stands for Index, which is the index file of MyISAM, which stores the index. For example, if we create a primary key index on the id field, then the primary key index is in this index file.
In other words, in MyISAM, the index and data are two separate files. So how do we find data based on the index?
In the B+Tree of MyISAM, the leaf nodes store the disk addresses corresponding to the data files. So .MYI
after finding the key value from the index file, it will go to the data file .MYD
to get the corresponding data record.
What is drawn here is the primary key index. If it is an auxiliary index, what is the difference?
In MyISAM, the auxiliary index is also .MYI
in this file. There is no difference between the auxiliary index and the primary key index in the way of storing and retrieving data. The disk address is found in the index file, and then the data is obtained in the data file.
InnoDB
InnoDB has only one file (.ibd file), so where is the index placed?
In InnoDB, it uses the primary key as the index to organize data storage, so the index file and the data file are the same file, both in the .ibd
file.
On the leaf node of InnoDB's primary key index, it directly stores our data.
What is a clustered index (clustered index)?
That is, the logical order of index key values is consistent with the physical storage order of table data rows. (For example, the directory of the dictionary is sorted by pinyin, and the content is also sorted by pinyin. This kind of directory sorted by pinyin is called a clustered index).
In InnoDB, the way it organizes data is called (clustered index organize table), so the primary key index is a clustered index, and the non-primary key is a non-clustered index.
How do indexes other than the primary key, such as the ordinary index we built on the name field, store and retrieve data?
In InnoDB, primary key indexes and auxiliary indexes are divided into primary and secondary.
Secondary indexes store secondary index and primary key values. If you use the auxiliary index query, you will query in the primary key index according to the primary key value, and finally get the data.
For example, if we use the name index to query name='Qingshan', it will find the primary key value in the leaf node, that is, id=1, and then go to the leaf node of the primary key index to get the data.
Another question, what if a table doesn't have a primary key?
1. If we define the primary key (PRIMARY KEY), then InnoDB will choose the primary key as the clustered index.
2. If the primary key is not explicitly defined, InnoDB will select the first unique index that does not contain NULL values as the primary key index.
3. If there is no such unique index, InnoDB will choose the built-in 6-byte long ROWID as a hidden clustered index, and it will increment the primary key as row records are written.
select _rowid name from t2;
so what? There is no table without a primary key.
Summarize
Through the above analysis, we know what the specific landing form of the index is in the two major storage engines, MyISAM and InnoDB.