当前位置:网站首页>MySQL daily experience [02]

MySQL daily experience [02]

2022-06-22 10:47:00 Anna and her notebook

️ ️ ️
1、 What is the leftmost prefix principle ? What is the leftmost matching principle ?
2、 What are the data table types
3、 What are the transactions in the database ?
4、 Please briefly describe the types of indexes commonly used ?
5、 Say overlay index ?

1、 What is the leftmost prefix principle ? What is the leftmost matching principle ?

Leftmost prefix principle , It's the top left priority , When creating a multi-column index , According to business needs ,where The most frequently used column in the clause is at the far left .

When we create a composite index , Such as (k1,k2,k3), It's equivalent to creating (k1)、(k1,k2) and (k1,k2,k3) Three indexes , This is the left most matching principle .

Leftmost prefix matching principle and union index Index storage structure and retrieval method It matters .

In the composite index tree , The bottom leaf node follows the first column a Columns are arranged incrementally from left to right , however b Column sum c The columns are out of order ,b Column only in a When the column values are the same, the small range increases in order , and c Columns can only be in a,b When the two columns are equal, the order is increased in a small range .

Like the query above ,B+ The tree will first compare a Column to determine which direction should be searched next , Left or right . If a Compare the same column b Column . But if the query criteria are not a Column ,B+ The tree doesn't know which node to start from in the first step .

It can be said that created idx_abc(a,b,c) Indexes , It's equivalent to creating (a)、(a,b)(a,b,c) Three indexes .

2、 What are the data table types

answer :MyISAM、InnoDB、HEAP、BOB,ARCHIVE,CSV etc. .

MyISAM: mature 、 Stable 、 Easier to manage , Fast read . Some features do not support ( Affairs, etc. ), Table lock .
InnoDB: Support transactions 、 Properties such as foreign keys 、 Data row locking . It takes up a lot of space , Full text indexing, etc. is not supported .

3、 What are the transactions in the database ?

Business (transaction) Is a set of ordered database operations as a unit . If all operations in the group succeed , The transaction is considered successful , Even if only one operation fails , Things don't work . If all operations are complete , Transaction commit , Its modification will affect all other database processes . If an operation fails , The transaction will be rolled back , The influence of the operation of the firm will be cancelled .ACID Four characteristics , Atomicity 、 Isolation, 、 Uniformity 、 persistence .

4、 Please briefly describe the types of indexes commonly used ?

General index : That is to create an index for the database table
unique index : Similar to a normal index , The difference is that :MySQL The value of the database index column must be unique , But you can have an empty value
primary key : It is a special unique index , No null values are allowed . It is common to create a primary key index while building a table
Composite index ( Joint index ): For further extraction MySQL The efficiency of , Consider building a composite index .
That is to combine multiple fields in a database table as a composite index .

5、 Say overlay index ?

Covering an index is not an index structure , Overlay index is a very common Optimization means . Because when using secondary indexes , We can only get the primary key value , It is equivalent to obtaining data and then querying the primary key index according to the primary key to get the data . But imagine the next situation , on top abc_innodb When the composite index query in the table , If I just need abc Field , Does that mean that when we query the leaf node of the composite index, we can directly return , You don't have to go back to the table . This is the case of overlay index .

For example :
Suppose we only need to query the name of the product 、 Price information , What can we do to avoid going back to the table ? We can build a composite index , I.e. commodity code 、 name 、 Price as a composite index . If this data exists in the index , The query will not retrieve the primary key index again , So as to avoid returning to the table .

Query records from the secondary index , It does not need to query through the cluster index ,MySQL Call it the overlay index . The benefits of using overlay indexes are obvious , We don't need to query all the information that contains the entire row of records , So you can reduce a lot of I/O operation .

Usually in InnoDB in , In addition to query some fields can use overlay index to optimize query performance , Statistics also use . for example , SELECT COUNT(*) when , If no secondary index exists , This will

Count rows by querying the aggregate index , If a secondary index happens to exist at this time , The number of rows will be counted by querying the auxiliary index , Reduce I/O operation .

Overlay index :

  • In a query , Indexes k already “ covers ” Our query needs , Called overlay index .
  • Covering the index can reduce the number of searches in the tree , Significantly improve query performance , Therefore, using overlay index is a common performance optimization method .
原网站

版权声明
本文为[Anna and her notebook]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/173/202206221034090755.html