当前位置:网站首页>MySQL optimization
MySQL optimization
2022-06-30 09:45:00 【Rookie ~~】
List of articles
You did MySQL Optimization of ?
First of all, there are some places that can be optimized SQL Statement and index optimization 、 Application optimization and MySQL Server Optimize , Let's go into details .
One 、SQL Statement and index optimization
When the amount of data is large , if SQL The statement is not written properly , It can lead to SQL The execution efficiency of is low , We need to wait a long time to get the results , We will use explain To analyze what SQL Caused by statement , But there are thousands of real enterprise projects SQL, We can't start from scratch, one by one explain To analyze , We should use slow query logs to analyze .
explain Analyze possible problems :
- Too much data , It can be done by limit Pagination
- No index is used
- Using multi column index does not use the first column , The index is not used
- The size table setting of joint query is unreasonable , The index is useless ( Small table is the whole table query , Index only for large tables )
- Multi table query does not need in( Generate intermediate tables ), Replace the belt with an external connection in The process of subquery , Use index reasonably
Two 、 Application optimization
Besides optimization SQL And index , In the actual production environment , Due to the performance limitations of the database server itself , We must optimize the application of the upper layer , So that the pressure of the upper application accessing the database can be minimized
Introduce database connection pool ; Prevent the client from repeatedly shaking hands three times to establish a connection , Four waves to close the connection , Consume network and server resources , We can introduce database connection pool , This is a common optimization method in high concurrency scenarios

Introduce caching ; Used to store hotspot data , If the client's request comes , First in redis Check it out (redis It's a memory based database ), If redis If you find it directly on the Internet, you don't go through MySQL database , If you don't find it, visit MySQL database , visit MySQL After completion , First, send the currently accessed data to redis Let's cache it , And then return the results to the user
introduce redis Cache words , If the interviewer asks redis, Maybe I will ask some questions next : Cache data consistency problem , Cache penetration and cache avalanche , Publish subscribe ( Used by chat server ),redis What else do you know about it ?
3、 ... and 、MySQL Server Optimize
about MySQL Server End optimization , Mainly refers to MySQL Server Profile loaded at startup (my.ini or my.cnf) Optimization of configuration items in , Let's take a look at the optimization parameters that we need to focus on in the configuration file .
1. adaptive hash index
If the frequency of using adaptive hash index is not high , Maintaining an adaptive hash index itself requires a lot of performance , adopt show engine innodb status\G The command looks at adaptive hash index searches less frequently than secondary index tree searches , If the frequency of using adaptive hash index is not high , You can turn off adaptive hash indexing through parameter configuration , You can go through the variables innodb_adaptive_hash_index Turn off adaptive hash indexing .
2. redo log
You can set InnoDB log buffer size (redo log Cache size ),Innodb_buffer_pool_size( Cache size ), To reduce disk I/O frequency , Because the cache is large , Working in the buffer for a long time ,redo log The efficiency is higher .
my.cnf Path to file /etc/my.cnf, The configuration file is as follows :
3. MySQL The query cache
MySQL The query cache is the last time select Record the query results and put them in the cache , The next time you query the same content , Just take it out of the cache , You don't have to do it again SQL Inquire about ( Divide a space in memory for cache )
Query caching is suitable for tables that are not updated frequently , Frequently queried tables , Because when two select Appear in the middle of query insert,update,delete At the time of statement , The query cache will be emptied , Too many queries add and delete cached data , It will affect MySQL Efficiency of execution , Maybe it's better to query from the disk every time ( A cache is a block of memory , Memory I/O Compare disk I/O Much faster )
Can be in MySQL Through the following command , To view the query cache settings :
If a table has more queries and less changes , Consider turning on the query cache
adopt show status command , You can see MySQL Query cache usage , as follows :

Can pass set Command to set the above cache parameters to enable MySQL Query cache function , You can also find MySQL Configuration file for (windows yes my.ini,linux yes my.cnf), modify query_cache_type Parameter is 1 That's all right. , And then restart MySQL Server You can use it , as follows :
MySQL Query caching is better than using redis, This is a MySQL The query cache , We as MySQL For service users , Can't control MySQL Cache mode , If we introduce redis Words , Can pass redis Provided API Precisely control the data to be cached , And non cached data
4. MySQL Thread cache
It mainly refers to... In the configuration file thread_cache_size Configuration item .MySQL Server The network module adopts the classic select I/O Reuse + Thread pool model , The reason why thread pool is introduced , Mainly for the purpose of business execution , Not because of temporary creation and destruction of threads , Cause system performance degradation , Because the creation and destruction of threads is very performance consuming .
Thread pool is before business use , First create a fixed number of threads , Waiting for events to happen , When there is SQL Request arrival MySQL Server When , Take a thread from the thread pool to execute the process SQL Just ask , After execution , Don't destroy threads , Instead, return the thread to the thread pool , Wait for the processing of the next task ( The number of threads in the thread pool increases with requests , It can be added dynamically )
End of configuration thread_cache_size, restart MySQL Server It will take effect after service
5. Number of concurrent connections and timeout
MySQL Server As a server , You can set the maximum number of connections and connection timeout of the client , If the number of database connections is large , The values of these two parameters need to be set larger .
In profile (my.ini or my.cnf) At the bottom , Add the configuration max_connections=2000, And then restart MySQL Server, Settings take effect .
MySQL Server For connections that haven't communicated for a long time , It closes the connection . Set timeout , When the set time is exceeded, disconnect without request , The unit is seconds , Add configuration... To the configuration file :wait_timeout = 600
边栏推荐
- Numpy (time date and time increment)
- Niuke rearrangement rule taking method
- Reading notes of "Introduction to deep learning: pytoch"
- ACM intensive training graph theory exercise 3 in the summer vacation of 2020 [problem solving]
- Machine learning note 9: prediction model optimization (to prevent under fitting and over fitting problems)
- Express file upload
- Express - static resource request
- How do I start? (continuously updating)
- Niuke walks on the tree (ingenious application of parallel search)
- Linear-gradient()
猜你喜欢
![[new book recommendation] mongodb performance tuning](/img/2c/e5a814df4412a246c703ca548a4f68.png)
[new book recommendation] mongodb performance tuning

Object detection yolov5 open source project debugging

云技能提升好伙伴,亚马逊云师兄今天正式营业
![[Ubuntu redis installation]](/img/66/d8054ae89007180b317641cf92d1cc.png)
[Ubuntu redis installation]

MySQL优化

I once met a girl whom I most wanted to take care of all my life. Later... No later

Eight sorts (I)

Dart 开发技巧

NTP of Prometheus monitoring_ exporter

Numpy (time date and time increment)
随机推荐
Niuke IOI weekly competition 20 popularization group (problem solving)
Ocx control can be called by IE on some computers, but can not be called by IE on some computers
Configuring MySQL for error reporting
【新书推荐】MongoDB Performance Tuning
Guilin robust medical acquired 100% equity of Guilin Latex to fill the blank of latex product line
Research on lg1403 divisor
qmlplugindump executable not found.It is required to generate the qmltypes file for VTK Qml
Bluetooth BT RF test (forwarding)
Alibaba billion concurrent projects in architecture
JVM notes (III): analysis of JVM object creation and memory allocation mechanism
Enum demo
utils session&rpc
银河麒麟server-V10配置镜像源
MySQL directory
Thrift easy to use
Microsoft. Bcl. Async usage summary -- in Net framework 4.5 project Net framework version 4.5 and above can use async/await asynchronous feature in C 5
What is the difference between ZigBee, Bluetooth and WiFi (copy and reprint)
Machine learning note 9: prediction model optimization (to prevent under fitting and over fitting problems)
抽象类和接口
Flutter 中的 ValueNotifier 和 ValueListenableBuilder