当前位置:网站首页>MySQL optimizes query speed
MySQL optimizes query speed
2022-06-24 21:35:00 【Wow, it's a piece of cake】
Use the index for the fields to be sorted
When the query result needs order by When , Can be in order by Add an index to the field of , Because the index is already in order , So you can finish sorting faster , Instead of sorting the query results every time , It consumes a lot of memory and time .
Use as much as possible union all instead of union
Unless you really need the server to eliminate duplicate rows , Otherwise, be sure to use union all, So there is no all keyword ,mysql Will add... To the temporary table during query distinct Key words of , The cost of this operation is high .
exists and join How to choose
join
Multiple tables are required for connection , And the fields to be queried are not from a table , such as a.name,b.dept This situation requires the use of join
exists
The fields to be queried are all in a surface , But there is a very complicated condition , have access to exists Clause to describe the following complex query conditions
In reality ,exists Use less , Everyone can't remember to use ha ha ha
and Priority over or
Like a query , Need to filter the name as AAA, And the age is 20 perhaps 21 The data of ,sql The statement is as follows ,
select * from tab_a where name = 'AAA' and age = 20 or age = 21;
The query result will change to
name = ‘AAA’ and age = 20 The data and
age=21 The data of , Query result error . Need to be transformed sql The statement is as follows :
select * from tab_a where name = 'AAA' and (age=20 or age=21)
Try not to join More than three tables
The single table index is controlled in 5 Within a
More indexes ,b+ The bigger the tree , Will affect insertion 、 Delete efficiency
The field of combined index cannot exceed 5 individual
key(a,b,c,d,e) Not too much , Due to the leftmost matching principle , If the query column is on the right , When querying, you need to supplement the conditions in the first few columns
also , The length of the index is too long b+ The tree is too big , Consume storage space
limit Optimize
limit The grammar is as follows :
Return from the first line , Return to the former n Data
select * from tab_a order by a limit n
From m Row data begins to return , Go back to the back n Data
select * from tab_a order by a limit m,n;
for example , If you want to query tab_a Table No 3 page , each page 10 Data , It can be done as follows :
select * from tab_a order by a limit 20,10;
limit It's like a pointer , After traversing the previous data , Find the data you need , And back to the user , If the amount of data is very large , for example limit(25000,20), It scans the whole table ,limit Efficiency will become very low , The tuning method for this case is as follows :
Use index column subqueries for tuning
Before tuning :
select film_id,description from film order by title limit 50,5
After tuning :
explain select film.film_id,film.description from film inner join (select film_id from film order by title limit 1500,5) as lim using(film_id);
After tuning , Avoided limit The pointer scans the entire table to obtain data , Instead, use the primary key first id The query ,id In the query , Only use b+ Tree to access data , You don't need to change the whole table io, Faster , And there's no need to go back , Then compare the query results with the main table join, Return the data .
Avoid querying the database for unnecessary data
The database service layer will query all the results , Form a result set , Get front n Close the result set after data , To avoid unnecessary result sets , have access to limit Speed up
for example :
select * from tab_a where id = 'sdasafdf676d8' limit 1;
Avoid using select *
If you need to repeatedly query , Use redis Cache
Try to use associations instead of subqueries
Because the sub query results will be placed in the temporary table during the execution of the sub query , Added io, Large memory overhead , and join You can use join buffer Make a quick match , It runs faster .
group by, distinct, order by It is recommended to use index columns
Use custom variables
What is a custom variable ?
set @one: = 1;
set @current_actor: = select actor_id from actor order by last_update desc limit 1 ;
set @last_week :=current_date-interval 1 week;
-- Use as follows
select count(1) from actor where last_update < @last_week;
select * from message where actor_id = @current_actor;
Custom variable usage scenarios :
- Optimization of sorting
set @rownum:= 0;
select actor_id,@rownum:=@rownum+1 as rownum from actor limit 10;
边栏推荐
- Distributed basic concepts
- Understanding openstack network
- [cloud native learning notes] deploy applications through yaml files
- Pytest test framework II
- Please open online PDF carefully
- Pattern recognition - 0 introduction
- Different WordPress pages display different gadgets
- Web automation: web control interaction / multi window processing / Web page frame
- Poj1061 frog dating (extended Euclid)
- Station B takes goods to learn from New Oriental
猜你喜欢

Pytest test framework II

Tdengine can read and write through dataX

Volcano becomes spark default batch scheduler

Tutorial on obtaining JD cookies by mobile browser

EditText controls the soft keyboard to search

Understanding openstack network

What does CTO (technical director) usually do?

升哲科技 AI 智能防溺水服务上线

Capture the whole process of accessing web pages through Wireshark

JMeter implementation specifies concurrent loop testing
随机推荐
Pytest test framework II
Pod lifecycle in kubernetes
ping: www.baidu.com: 未知的名称或服务
Requests requests for web page garbled code resolution
Poj1061 frog dating (extended Euclid)
Web project deployment
Blender's simple skills - array, rotation, array and curve
使用 Go 编程语言 66 个陷阱:Golang 开发者的陷阱和常见错误指北
BBR bandwidth per second conversion logic
Different WordPress pages display different gadgets
Minimum cost and maximum flow (template question)
Remember the frequently forgotten problem of continuously reading pictures -%04d
Learn to use a new technology quickly
The virtual currency evaporated $2trillion in seven months, and the "musks" ended the dream of 150000 people becoming rich
188. the best time to buy and sell stocks IV
Memcached comprehensive analysis – 2 Understand memcached memory storage
Pattern recognition - 9 Decision tree
Decoration home page custom full screen video playback effect GIF dynamic picture production video tutorial playback code operation settings full screen center Alibaba international station
Alibaba cloud lightweight servers open designated ports
Football information query system based on C language course report + project source code + demo ppt+ project screenshot