当前位置:网站首页>Rethinking about MySQL query optimization
Rethinking about MySQL query optimization
2022-07-05 23:18:00 【bapijun】
About mysql Rethinking of query optimization
I wrote an article about mysql Optimization thinking article ,mysql Ten experiences of sentence optimization , Now I want to come for a few years , I have been in contact with several optimization problems at ordinary times , It's time to continue writing a new article .
About index hits
Many times for short queries (where There are only a few sentences , There is no table Association ), Just hit an index , The speed will be very fast , At the same time, even if the small table does not hit , Basically, it doesn't matter ( The quantity set problem will be mentioned later )
But for the vast majority of short queries used, we know that they will basically hit the index ( Unless the table has no index ). Therefore, the real problem is often multi table joint query , In many cases , complex on Index hits under statements are often random ( It can't be said to follow fate , But for non dba Development of , The optimization mechanism of index is black box , Only try to guess and write by experience ), Many you think will hit , It often doesn't hit , Use at this time explain Often the advantage . But I have also encountered hit in the test environment , In the case of a miss in the formal environment , Guess is under different quantities , The optimization mechanism is different .
Joint index
Once the union index hits , The speed will improve very fast , But the hit rule of the joint index is to hit left , So many times people will limit the way they write ( At least in my current 5.7 It's like this , I don't know what will happen next mysql Will it optimize )
Use of slow queries
Open slow query and cooperate with various logs to quickly find the query statements with problems . Especially for those slow queries that often appear .
However, there should also be a concept of cost in development . For example, many of our current slow queries are generated by generating query logs in the early morning . The emergence of these slow queries is actually within our estimated range , There is no need to invest development costs to optimize . For example, I mentioned this article before mysql The associated index hit .md It often happens , It is necessary to optimize the query specifically . Although it took several hours to query and optimize , But it's worth it .
Order of magnitude
Code development , Order of magnitude is a commonly used concept , For example, backstage , Every page has 20 strip , I use short queries for every one , Although there are 20*n Short query for , But there is no obvious difference in speed compared with some associated queries that do not hit the index , It will be faster to write . Problems are also easier to find .
Another example is the optimization I mentioned above , Because of the size table driven problem , Association query , Driven by size table , Once the index is not hit , Generate millions of full table queries , The speed will become extremely touching . This is also the reason for the size table driving problem often mentioned in many articles .
Weigh development time , Frequency of occurrence can also be regarded as an order of magnitude concept , Put a bunch of long queries into the cache , After that, avoiding re query is also a common optimization method .
Syncopation query
Sometimes we need to divide and conquer a big query , Every query is exactly the same , Only a small part is returned at a time .
For example, we need to delete messages of a certain month , Disposable delete There is a lot of pressure on the server , We can delete only 10000 strip , Loop processing , Greatly reduce the impact on the server , Colleagues reduce the holding time of locks when deleting .
At the same time, avoid accessing too much data at one time , It can also effectively avoid orders of magnitude , As I mentioned before, try to avoid using *, For multi table queries , No, limit Time is especially important , Under large quantity set , Send only 1/4 The data of , It can also greatly save time .
Processing in a programming language
I mentioned in my previous article , In many cases , Extract the data and process it in the programming language , It's also an advantage , Before helping interns optimize code, there was a problem . The logic of interns is to put data processing and inspection into mysql Inside , It takes a lot of time and energy to study how to hit the index . My experience is , Use a short query , Process a table , use yii Inside index sentence , Extract the previous data and clean out the useless data , Then put the remaining dozen useful data to query the database , In this way, the speed is increased to 20 times .
Waste a little cpu And memory resources . It greatly reduces the pressure and development cost of the database .
边栏推荐
- 芯源&立创EDA训练营——无刷电机驱动
- 3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
- 利用LNMP实现wordpress站点搭建
- 【Note17】PECI(Platform Environment Control Interface)
- 判断二叉树是否为完全二叉树
- Matlab smooth curve connection scatter diagram
- Hcip day 12 (BGP black hole, anti ring, configuration)
- 3D point cloud slam
- regular expression
- [digital signal denoising] improved wavelet modulus maxima digital signal denoising based on MATLAB [including Matlab source code 1710]
猜你喜欢
Go语言实现原理——锁实现原理
3: Chapter 1: understanding JVM specification 2: JVM specification, introduction;
Leetcode weekly The 280 game of the week is still difficult for the special game of the week's beauty team ~ simple simulation + hash parity count + sorting simulation traversal
2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
Expectation, variance and covariance
一文搞定垃圾回收器
终于搞懂什么是动态规划的
CorelDRAW plug-in -- GMS plug-in development -- new project -- macro recording -- VBA editing -- debugging skills -- CDR plug-in (2)
Selenium+pytest automated test framework practice
Three. JS VR house viewing
随机推荐
YML configuration, binding and injection, verification, unit of bean
媒体查询:引入资源
Use of shell:for loop
One article deals with the microstructure and instructions of class
(4)UART应用设计及仿真验证2 —— RX模块设计(无状态机)
UVA – 11637 Garbage Remembering Exam (组合+可能性)
无刷驱动设计——浅谈MOS驱动电路
数学公式截图识别神器Mathpix无限使用教程
Hcip day 11 (BGP agreement)
代码农民提高生产力
February 13, 2022 -5- maximum depth of binary tree
Marginal probability and conditional probability
Common JVM tools and optimization strategies
MySQL (2) -- simple query, conditional query
Sum of two numbers, sum of three numbers (sort + double pointer)
Debian 10 installation configuration
Object detection based on impulse neural network
2022 G3 boiler water treatment simulation examination and G3 boiler water treatment simulation examination question bank
Multi sensor fusion of imu/ electronic compass / wheel encoder (Kalman filter)
3D reconstruction of point cloud