当前位置:网站首页>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;
原网站

版权声明
本文为[Wow, it's a piece of cake]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206241436540089.html