当前位置:网站首页>SQL index learning notes

SQL index learning notes

2022-06-26 10:41:00 MervynLammm

SQL Index essence

Indexes (index) Help MySQL Efficient Access to data data structure .

stay RDBMS in , The index is stored in the hard disk .

Hash - Hash index

AHI-adaptive hash index - adaptive hash index

Calculate on field hash value , take hash The value and the row pointer correspond to each other hash In the table .

Why? innodb The engine does not support manual use hash Indexes :

  1. In memory , Large amount of data , Memory explodes easily
  2. hash Query line records , It is not suitable to specify the columns to be checked
  3. Press hash sorted , Not helpful for range lookup , If there is order, You need a second order
  4. Hash collision , Calculated hash Same value . One hash Value points to a multiline pointer , You need to find them one by one

memory The engine can be used manually hash Indexes , because memory The engine uses memory .

Generally, the intermediate table uses memory engine .

B+Tree Indexes

Binary search tree

A node can be divided into two nodes at most , The child nodes are small on the left and large on the right .

shortcoming : The limit case , hypothesis 1 Root node , The next nodes are 2、3、4、5 wait , To query 5 need 5 Time .

Balanced binary trees

A node can be divided into two nodes at most , The absolute value of the depth difference between two child nodes cannot be greater than 1.

  1. IO Too many times , With more data , The depth of the tree grows .
  2. Too little target data , Cause great IO Waste of resources .

B-Tree Multiple search trees 、 Multi fork balanced lookup tree

  1. Multiple key numbers
  2. Multiplexer number
  3. The depth of each road is the same 、 Absolute balance

Every node , The maximum number of keys = Number of routes - 1.

Assume that the node is :1,5,10

Then the branch is [ Negative infinity ,1),[1, 5),[5,10),[10, It's just infinite )

Each node has a data area .

B+Tree

Number of key nodes and paths 1:1.

Assume that the node is :1,5,10

Then the branch is [1, 5),[5, 10),[10, It's just infinite )

The data area is stored in the leaf node , So all queries must find leaf nodes , Although some queries are better than B-Tree More time consuming , But stable .

advantage :

  1. Strong ability to scan tables based on indexes
  2. Strong ability to sort based on index
  3. Query all data to the leaf node , Time stability .
  4. Cancel the contents of the data area , Better literacy

Clustered index

myisam engine

myi file , Storage index , Leaf node storage address

myd file , Store the data corresponding to the address

innodb engine

ibd file :

  • primary key : Leaf node mount line record

  • Non primary key index : The leaf node saves the primary key value

innodb In the engine , Only the primary key is a clustered index , Other indexes are nonclustered indexes .

primary key

If you do not manually create a primary key index , Will automatically use hidden columns _rowid As a primary key index , Occupy 6byte, and int Occupy only 4byte.

And row locks will be upgraded to table locks

Excellent index

Discreteness of columns

count distinct col : count col

Number of non repeating Columns : Number of all columns

The less column repetition the better .

Joint index

Left most matching principle : Any prefix of a federated index enables federated indexing

create index idx_name_ph_age(name,phone,age)

name

name, phone

name, phone, age

All three of the above can enable federated indexing , and

phone

phone, age

phone, name ,age

Cannot enable federated indexing

Whether the following two queries enable union query , Used several :

  1. where name=‘peter’ and phone > 1333333 and age = 18

    Union index used ,1 individual .“ After the range, it all fails ”

    because phone>1333333 Too much scope , and age=18 High repetition rate .

  2. where phone = 1333333 and name = ‘peter’

    Use union index 2 individual ,mysql Optimizer , Will put the last column of selectivity in where On the far left of .

The connector : Manage connections , Authority verification

analyzer : Lexical analysis , Syntax analysis

Optimizer : Execution plan generation , Index selection

actuator : Operate the engine , Return results

Overlay index

Do not trigger the table return operation .

The column of the lock query can be directly returned through the information of the index item , Then the index becomes a query SQL Coverage index of .

Try to use overlay index , Because it can speed up the query .

Back to table operation :

According to the above innodb engine

  • primary key : Leaf node mount line record
  • Non primary key index : The leaf node saves the primary key value

Find out the primary key according to the non primary key index , The operation of querying row records according to the primary key index is the back to table operation .

Suppose the table has :

  1. primary key
  2. Non primary key index name
  3. Joint index name, phone

be :

select * from user where name = 'mervyn' Trigger the table return operation , Through non primary key index name Query out id after , And find out the row data according to the primary key index .

select id, name from user where name = 'mervyn' Table return operation is not triggered , Through non primary key index name You can directly query id and name.

select id, phone, name from user where name = 'mervyn' Table return operation is not triggered , Joint index

Samsung index

  1. where The more index keyword columns you can match, the better , The more accurate the scanned data , The less, the better. .
  2. Avoid reordering .
  3. Apply overlay indexes as much as possible . Reduce the return operation .
原网站

版权声明
本文为[MervynLammm]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202170529156791.html