The underlying principle and optimization of MySQL

1. Execution principle

2. Storage engine

View storage engine

SHOW ENGINES;

2.1.InnoDB

  • default storage engine
  • Support business
  • Natural support for row locks, manual support for table locks
  • Support for foreign keys
  • Use a clustered index - only the primary key index is a clustered index, the other indexes are non-clustered indexes

2.2.MyISAM CODE

  • does not support transactions
  • Only table locks are supported, row locks are not supported
  • foreign keys are not supported
  • Use a nonclustered index

3. Index

A data structure that improves query performance

The bottom layer uses a B+ tree

Classification

  • Clustered (set) index [primary key index]
  • Non-clustered (set) index [non-primary key index]

Detailed reference for trees

(27 messages) Detailed Explanation of b+ Tree

B+ tree

  • Non-leaf nodes - store the index value and a pointer to the next index value
  • Leaf nodes - Redundantly store index values ​​of non-leaf nodes, store index values ​​and data, and pointers exist between leaf nodes to improve interval access efficiency

Why not choose hash? ——Because hash only supports equivalent query, not range query

primary key index

The table of the InnoDB engine must require a primary key, if not created, the database will automatically create and maintain a primary key index

Non-leaf nodes store primary key values

Leaf nodes store row data

It is recommended to use primary key auto-increment:

Because the inserted data will always be placed at the end, the insertion position can be quickly found without additional overhead, such as moving the data position, rotating the tree, etc.

non-primary key index

Index Creation Requirements

  • No more than 5 single-table indexes
  • Joint index has no more than five fields
  • Fields that are frequently added, deleted, and modified are not suitable for index creation
  • Enumerated value fields are not suitable for indexing
  • Infrequently modified, frequently queried fields are suitable for index creation

Back to the table:

Every time an index is created, a B+ tree will be created at the bottom of the database. The non-leaf nodes store the indexed fields, and the leaf nodes store the primary key value. When there is no index in the field you want to query, the database will first query the index The B+ tree finds the primary key value, and then obtains row data through the primary key value, and performs two query trees. If you want to optimize SQL, you should reduce the generation of return tables.

4. SQL optimization

 1. Reasonably design the table structure

Field data type, length...

Reference 58 Military Regulation Edition

Interpretation of the 30 Military Regulations of 58 Daojia Database_w3cschool

2. Query SQL optimization

leftmost prefix rule

  • The ultimate principle: Try to make SQL hit the index to improve query performance
  • Leftmost prefix rule: When using a joint index, it must satisfy the matching index column from the left
  • When writing query conditions, they should be written sequentially from the leftmost column of the joint index

 In case of index failure

 

Guess you like

Origin blog.csdn.net/xzxailh/article/details/129100374