MySQL architecture, MySQL storage engine, index

MySQL architecture

connection layer

Responsible for establishing connections and authentication with clients and programs...

service layer

sql interface parser query optimizer cache

engine layer

Responsible for connecting to the data file system and writing data

physical file layer

Responsible for storing table data and log files, which relies on logs

MySQL storage engine

The engine is a technology that specifically interacts with files in the database. Different engine implementation methods are different.

Database engine, specific implementer

Commonly used engines

Myisam does not support transactions and is suitable for many queries. It does not support foreign keys, does not support row locks, supports table locks, supports full-text indexes, and stores the total number of rows in the table.

select count(*) from admin can directly get the total number of rows, which is fast

innodb supports transactions, suitable for additions, deletions and modifications, supports transactions, table locks, row locks, supports caching, supports primary key auto-increment, supports full-text indexes, and does not store the total number of rows in the table

select count(*) from admin Self-calculation is slow

Enter this statement in the database to query the engines supported by the database

show engines

index

Array index can quickly find data at a certain location through the index

Why have an index?

If you do not use an index, the query starts from the first row and searches backwards row by row until the data we need is found. If the amount of data is not large, the query efficiency is relatively low.

what is index

Indexes are data structures that help MySQL obtain data efficiently.

Sorted data structure for fast search

Maintain data in a data structure for easy search

Index principle

The index is similar to the catalog of a book. We can quickly query our data through the catalog and narrow the scope of the query.

Advantages of indexing

Improve the efficiency of data retrieval and reduce the IO cost of the database;

Sorting data through index columns reduces the cost of data sorting and reduces CPU consumption;

Disadvantages of indexing

Also requires memory space

When adding, modifying, or deleting operations on a table, you also need to modify the index information while operating the data.

Principles of index creation

Although indexes are good, don’t use them indiscriminately

When is an index needed?

The primary key automatically creates a unique index, primary key index

Columns used as query conditions are suitable for creating indexes

It is recommended to create an index for foreign keys

Sorting and grouping fields are suitable for adding indexes

When is it not recommended to use indexes?

Too few table records

Add, modify, and delete frequently used tables. Split the original table into separate tables to separate reading and writing.

Not a query condition

The data is repeated and evenly distributed, for example: gender

Index classification

Primary key index:

After setting the primary key, the database will automatically create an index. It cannot be empty. It is unique. A table can only have one primary key.

Single value index:

An index contains one column, and a table can have multiple single-value indexes.

Unique index:

Data cannot be repeated

Combined index (composite index)

Multiple columns in an index

Leftmost prefix principle for combined indexes

When using a combined index, the leftmost column must appear, otherwise the index will be invalid.

For example, there are three columns in the table: a, b, and c. Create a combined index for the two columns a and b.

For example, select * from table where a=''and b='' index takes effect

select * from table where b=''and a='' index takes effect

select * from table where a=''and c='' index takes effect

select * from table where b=''and c='' index does not take effect

Use fuzzy query name like %张%; Written like this, it will cause the index of the name column to become invalid. It is not recommended to use fuzzy query for like.

It is recommended to use full-text index in mysql8

Full text index

When fuzzy query is needed, the general index is invalid, and then the full-text index can be used.

CREATE FULLTEXT INDEX 索引名 ON 表名(字段名) WITH PARSER ngram; 

SELECT 结构 FROM 表名 WHERE MATCH(列名) AGAINST(搜索词') 

View index

SHOW INDEX FROM 表名;

Index data structure

The MySQL Innodb engine uses B+ numbers as the data structure to store indexes by default.

Sorted, one node can store multiple data. Horizontal expansion reduces the height of the data.

Non-leaf nodes do not store data, only indexes, and more indexes can be placed

Data records are stored in leaf nodes. If you find the index, you will find the data.

There is a chain pointer between all leaf nodes, which is very suitable for interval queries such as: age>20 age<50

Clustered index and non-clustered index

Clustered index :

Once the index is found, the data is found, then this index is a clustered index.

The primary key can directly find the data

If you query the student number based on the student number value, you can directly hit the student number. In this scenario, the student number is clustered.

Non-clustered index :

The index is found but the data is not found. You need to query the table again based on the primary key.

Query the student number and name based on the student number value. Although the student number is indexed, the name still needs to be queried. The primary key needs to be found based on the student number, and the query is returned to the table through the primary key. This scenario is non-clustered.

The MyISAM engine uses a non-clustered design, even for primary key indexes. The index file and data file are not in the same file.

Guess you like

Origin blog.csdn.net/crraxx/article/details/122610739