当前位置:网站首页>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
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 )
3、 ... and 、 Common commands
Four 、mysql Installation position of
5、 ... and 、 Indexes
5.1 The essence of index
Quick search in order data structure ( Stored on hard disk )
5.2 Type of index
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
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 :
5.6.2 select_type
data type : For example, simple query , Complex queries , Derived query , The subquery etc.
5.6.3 type(*)
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
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
6、 ... and 、 Query interception analysis
6.1 Query optimization
6.1.1 Small tables drive large tables
- left join Such , Keep the data of the left table smaller than that of the right table
- 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
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
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 :2、 But in show profiles It needs to be turned on before , The commands are as follows :
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:
window 2:
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
important :
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
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
8、 ... and 、 Master slave copy ( Special trival )
边栏推荐
- Realize video call and interactive live broadcast in the applet
- Implementation of seven classes of BlockingQueue interface
- Redis cluster mode
- MySQL's built-in performance testing tool, mysqlslap, performs stress testing
- PHP design function getmaxstr to find the longest symmetric string in a string - [original]
- Zeromq from getting started to mastering
- 35 year old programmer fired Luna millions of assets and returned to zero in three days. Netizen: it's the same as gambling
- Zhimeng CMS will file a lawsuit against infringing websites
- I like you!
- Nailing open platform - applet development practice (nailing applet client)
猜你喜欢
MySQL enable logbin in Qunhui docker
Read / write lock for thread synchronization
微软禁止俄用户下载安装Win10/11
Unity mobile game performance optimization spectrum CPU time-consuming optimization divided by engine modules
CDN with OSS acceleration
Laravel framework Alipay payment fails to receive asynchronous callback request [original]
Notes on enterprise wechat development [original]
Ubuntu installs PostgreSQL and uses omnidb to view
Microsoft prohibits Russian users from downloading and installing win10/11
修改Oracle连接数
随机推荐
微软禁止俄用户下载安装Win10/11
CTF serialization and deserialization
College C language final exam · multiple choice questions · summary notes of mistakes and difficulties
redis集群的方式
一幅脑图总结一下需求分析(工作上实际遇到的情况的补充)
Simple personal summary of tp6 multi application deployment -- Part I [original]
[从零开始学习FPGA编程-45]:视野篇 - 集成电路助力数字化时代高质量发展-2-市场预测
Sixtool- source code of multi-functional and all in one generation hanging assistant
MySQL enable logbin in Qunhui docker
Ueeditor automatically appends P tags to rich text.br tags always wrap.br tag solutions
[learn FPGA programming from scratch -45]: vision chapter - integrated circuits help high-quality development in the digital era -2- market forecast
Using jsup to extract images from interfaces
NPM installation tutorial
[Qunhui] command line acme SH automatically apply for domain name certificate
Performance test comparison between PHP framework jsnpp and thinkphp6
Composer version rollback version switching
Laravel uses phpword to generate word documents
Laravel framework Alipay payment fails to receive asynchronous callback request [original]
mysql自帶的性能測試工具mysqlslap執行壓力測試
go语言泛型在IDE中语法报错