当前位置:网站首页>Advanced learning of MySQL (learning from Shang Silicon Valley teacher Zhou Yang)

Advanced learning of MySQL (learning from Shang Silicon Valley teacher Zhou Yang)

2022-06-26 04:28:00 Attacking polar bear

Network disk data link address : link :https://pan.baidu.com/s/1KpqfII6odIZDwcCz8OkeMg Extraction code :3452

One 、mysql Logical architecture

1.1、 Architecture diagram

 Insert picture description here summary : Compared with other databases ,MySQL It's a little different , Its architecture can be applied in many different scenarios and play a good role . Mainly reflected in the storage engine architecture , The plug-in storage engine architecture separates query processing from other system tasks and data storage and extraction . This architecture can choose the right storage engine according to the needs of the business and the actual needs .

Two 、 Seven kinds join theory ( a key )

 Insert picture description here

3、 ... and 、 Common commands

 Insert picture description here

Four 、mysql Installation position of

 Insert picture description here

5、 ... and 、 Indexes

5.1 The essence of index

Quick search in order data structure ( Stored on hard disk )

5.2 Type of index

 Insert picture description here
Missing a primary key index : A primary key index is a unique index , But it doesn't include null value

5.3 The principle of indexing

 Insert picture description here

5.3.1 Lookup process

If you are looking for a data item 29, So the first thing you do is you take the disk block 1 Loaded by disk into memory , It happens once IO, It is determined in memory by binary search 29 stay 17 and 35 Between , Locked disk block 1 Of P2 The pointer , Memory time is very short ( disk-to-disk IO) Negligible , Through the disk block 1 Of P2 Pointer to the disk address to the disk block 3 Loaded by disk into memory , Happen a second time IO,29 stay 26 and 30 Between , Locked disk block 3 Of P2 The pointer , Load disk blocks with Pointers 8 To the memory , The third time IO, At the same time do binary search in memory to find 29, The end of the query , A total of three times IO.
Lookup process :

5.3.2 btree summary

summary : Here's the real story ,3 Layer of b+ Trees can represent millions of data , If it only takes three times to find millions of data IO, The performance improvements will be huge , If there is no index , Each data item has to happen once IO, So it's going to take a million IO, Obviously the cost is very, very high .

5.4 Which situations are suitable for indexing

5.5 Which situations are not suitable for indexing

5.6 explain Optimize ( Viewing performance )

5.6.1 id(*)

Sort the summary :
 Insert picture description here

5.6.2 select_type

data type : For example, simple query , Complex queries , Derived query , The subquery etc.
 Insert picture description here

5.6.3 type(*)

 Insert picture description here

5.6.4 possible_keys and key(*)

possible_keys : Represents possible indexes ( All possible indexes will be displayed )
key: The index actually used by the current statement

5.6.5 ref

Shows which column of the index is used , If possible , It's a constant . Which columns or constants are used to find values on index columns .

5.6.6 rows

rows Columns show MySQL The number of rows that it must check to execute the query . The less, the better. !

5.6.7 extra

 Insert picture description here

5.7 Index analysis ( Single table , Double table , Three watches )

If dual meter , be left join The query field indexing on the right can improve the query speed
If three watches , be left join 2 The query fields on the right can improve the query speed
summary : Small tables drive large tables , The query conditions of large tables are quoted

5.8 Index failure ( Precautions to avoid index invalidation !)

1. Full value matching my favorite
2. The best left prefix rule ( If more than one instance is indexed , Follow the leftmost prefix rule . It means that the query starts from the left front of the index and does not skip the columns in the index .)-------------------------------------- This is the most important ( Leading brother can't die , The middle brother can't break !)
3. Do nothing on the index column ( Calculation 、 function 、( Automatically or Manual ) Type conversion ), It will cause index invalidation and turn to full table scan (where Do not add calculation to the fields of the index conditions added later 、 Functions, etc )
4. The storage engine cannot use the column to the right of the range condition in the index ( The right side of the range is invalid , for example age > 18 and name = ‘jarry’, there and Then it will fail , Because the front is the range ).
5. Try to use overlay index ( Queries that only access the index ( The index column is consistent with the query column )), Reduce select

6. mysql In use is not equal to (!= perhaps <>) Unable to use the index will result in a full table scan
7. is null,is not null You can't use indexes
8. like Start with a wildcard (‘$abc…’)mysql Index failure will become a full table scan operation (a、 Percent sign like Add right ;b、 If you add... On both sides %, Use overlay index , That is to say, get data from the index )
9. String index is invalid without single quotation marks
10. To use less or, When you connect with it, the index will fail
*
summary :
【 Optimize the summary formula 】
Full value matching my favorite , The leftmost prefix should follow ;
Leading brother can't die , The middle brother can't break ;
Less computation on index columns , After the range, it all fails ;
Like 100% right , Overlay index does not write stars ;
I don't want empty values and or, Index failure should be used less ;
VAR Don't throw quotation marks ,SQL It's not hard to be advanced !

Overlay index

The queried columns should be overwritten by the index , It means to get the required data from the index

5.9 General recommendations for indexing

 Insert picture description here

6、 ... and 、 Query interception analysis

6.1 Query optimization

6.1.1 Small tables drive large tables

  1. left join Such , Keep the data of the left table smaller than that of the right table
  2. exist and in The difference between :
    in: The table in the bracket on the right is smaller than the table on the outside
    exist: The table in the right bracket will only return true perhaps false, On the left ( outside ) The table in brackets is smaller than the table in brackets

6.1.2 order by Optimize

 Insert picture description here

6.1.3 group by Optimize

Same as 6.1.2 Graph , It's just one more

where higher than having, It can be written in where Don't go to... Under certain conditions having Limit the .

6.2 Slow log query

 Insert picture description here
summary : Slow log queries are dba Or the operation and maintenance manager ; Slow log query needs to be started manually , What can be found through the slow log after going online sql Query speed is slow , Can accurately locate sql, Then solve the optimization .

6.3 The batch sql Script ( Hang in the air – The explanation is a little annoying , Then study )

6.4 show profiles

1、 If explain、 Slow logs can't find the problem , Then use more sophisticated show profiles . The display effect is as follows :
 Insert picture description here 2、 But in show profiles It needs to be turned on before , The commands are as follows :
 Insert picture description here

7、 ... and 、 lock

7.1 Table locks

7.1.1 Read the lock

Turn on 2 A window to lock and unlock the test :
window 1:
 Insert picture description here
window 2:
 Insert picture description here

7.1.2 Write lock

Writing lock is my only honor , When adding a write lock , When the read and write operations are performed by one side , The other side needs to wait , Until the lock is acquired .

7.1.3 summary

 Insert picture description here
important :
 Insert picture description here

7.2 Row lock (innobd The default is table lock , When testing, start manual submission for testing )

Lock the rows of the table

7.3 Index failure , Row lock changes table lock

Turn on manual submission , When updating field values , Write a string type as a number type , although mysql The bottom layer will be processed , But it will turn row locks into table locks

7.4 The danger of clearance lock

A transaction updates the data in a range , Another transaction needs to wait to update the data in the scope .

7.5 Interview questions : How to lock a row

 Insert picture description here
1、begin
2、 Find the data that needs to be locked , And then add for update.( When other affairs are executed , Will wait , until commit Execution completed )
3、 When you're done ,commit Submit

7.6 Row lock contention on the system

command :show status like ‘innodb_row_lock%’;

7.7 Optimization Suggestions

 Insert picture description here

8、 ... and 、 Master slave copy ( Special trival )

原网站

版权声明
本文为[Attacking polar bear]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260421424635.html