当前位置:网站首页>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;
边栏推荐
- Mysql优化查询速度
- Adding subscribers to a list using mailchimp's API V3
- 基于C语言实现的足球信息查询系统 课程报告+项目源码+演示PPT+项目截图
- Ebpf XDP mount point analysis
- Microsoft Certification (dynamic 365) test
- Functional analysis of ebpf sockops
- An example illustrates restful API
- Graduation summary of phase 6 of the construction practice camp
- [cloud native learning notes] learn about kubernetes configuration list yaml file
- Arkit与Character Creator动画曲线的对接
猜你喜欢

TDengine可通过数据同步工具 DataX读写

Docking of arkit and character creator animation curves

Oauth2.0 introduction

Adding subscribers to a list using mailchimp's API V3

Static routing job

Rewrite, maplocal and maplocal operations of Charles

自己总结的wireshark抓包技巧

memcached全面剖析–5. memcached的应用和兼容程序

Station B takes goods to learn from New Oriental

Memcached full profiling – 1 Fundamentals of memcached
随机推荐
Distributed basic concepts
Handwritten RPC the next day -- review of some knowledge
Analyse complète Memcached – 2. Comprendre le stockage de mémoire pour Memcached
ping: www.baidu.com: 未知的名称或服务
Adding subscribers to a list using mailchimp's API V3
[cloud native learning notes] kubernetes practice command
Slider控制Animator动画播放进度
Reflect package
自己总结的wireshark抓包技巧
Tdengine can read and write through dataX
Blender's landscape
An example illustrates restful API
SYSCALL_ Define5 setsockopt code flow
Curl command
装修首页自定义全屏视频播放效果gif动态图片制作视频教程播放代码操作设置全屏居中阿里巴巴国际站
how to install clustershell
Auto. JS to automatically authorize screen capture permission
Notes_ Vlan
PHP script calls command to get real-time output
Use of kubernetes storage volumes