当前位置:网站首页>Handling skills of SQL optimization (2)
Handling skills of SQL optimization (2)
2022-06-25 06:05:00 【It mapper】
SQL Optimize , The old driver knows the handling skills (2)
Let's continue with the previous article
Replace subquery with join query
mysql If you need to query data from two tables , There are generally two ways : Subquery and join query , The subquery is as follows :
select * from order where id in(select * from user where status = 1);Subquery statements can be through in Keyword implementation , The condition of one query statement falls on another select Statement in the query result . The program first runs the statements nested in the innermost layer , Running an outer statement , The advantage of subquery statements is that they are simple , structured , If there are not many tables involved, we can use this method , But the subquery needs to create a temporary table , When the query is finished , You need to delete the temporary table again , There are some additional performance costs , At this time, we can change to join query
select * from order inner join user on o.id = u.id and status = 1;
join Don't have too many watches
- According to Alibaba developer manual ,join The number of tables should not exceed three , If join Too many tables for ,mysql Selecting an index can be very complicated , It's easy to make the wrong choice , If you don't hit the index , It will be very slow , We should try to reduce join Table number
- If there is no way to avoid too many in the actual business scenario join Table number , Then do it
join Pay attention to
When we are involved in the joint query of multiple tables , You usually use join keyword , The best use is left join and inner join
left join, When a join query is made between two tables , All the rows in the left table are returned , Even if there is no matching record in the right table
right join, When a join query is made between two tables , All rows of the right table are returned , Even if there is no matching record in the left table
inner join( Internal connection ), When a join query is made between two tables , Keep only the exact matching result sets in both tables
Control the number of indexes
- as everyone knows , Indexing can significantly improve query performance sql Performance of , But the number of indexes is not the more the better , Because when adding data to the table , You need to create an index for him at the same time , The index requires additional storage space , And there will be some performance consumption
- Alibaba stipulated in the development manual that , The number of indexes should not exceed 5 individual ,MySQL Use b+ The tree holds the index , During addition, deletion and modification , There will be additional performance consumption
- If the number of indexes required in the table exceeds 5 What should I do ?
- We can create a federated index without creating a single index , Useless indexes can be deleted , If the amount of data is huge , Let's consider not using mysql, have access to hbase
Select a reasonable field type
char Represents a fixed string type , This type of field has a fixed amount of storage space , It wastes storage space
alter table order add column code char(20) not null;varchar Represents variable length string type , The storage space of this type of field will be adjusted according to the length of the actual data , No waste of storage space
alter table order add column code varchar(20) not null;If a field of fixed length , For example, the mobile phone numbers of users are generally 11 Bit , Then we directly define it as 11 Just a bit , However, if it is a field such as comments, we cannot use char, Waste a lot of space , We should use varchar
When we choose the type , This principle should be followed :
- Can use numeric type , You don't need string types , Because string processing is slower than numbers
- Use as small a type as possible
- Type with fixed length , Such as char type
- Variable length string type , Such as varchar type
- Amount to be used decimal, Avoid loss of accuracy
promote group by The efficiency of
We have a lot of scenarios that use group by keyword , Its main function is de duplication and grouping , Usually with having Use it together , It means that data is filtered according to certain conditions after grouping , Counterexample :
select * from order group by id having id < 200;This writing method is not good , He first looked up all the data and then grouped them , Then filter users id Less than 200 Users of , Grouping is a relatively time-consuming operation , Why can't we narrow it down first ?
select * from order where id < 200 group by id ;Before grouping, we use where Conditions for filtration , Filter out the extra data , In this way, the efficiency of grouping will be improved , Before we do something time-consuming , Reduce the data range as much as possible
Index optimization
- sql Optimization , There is a very important content is : Index optimization
- A lot of times , our sql sentence , Index gone and index not gone , Execution efficiency varies greatly , Index optimization is known as sql The first choice for optimization
sql Optimize
Check sql The statement has no index
explain select * from order where id < 200 group by id ;adopt explain Keyword we can see sql Whether or not to go , And then about explain Please search the Internet , Have time to explain
边栏推荐
- Try with resource close resource flow
- Find command – find and search for files
- Solve some prompt codes that pychar cannot recognize selenium
- Wind farm visualization: wind farm data
- MySQL tuning -- 02 -- slow query log
- Interview experience - list of questions
- Part 33 of SAP ui5 application development tutorial - trial version of responsiveness of SAP ui5 applications
- Summary of 6 common methods of visual deep learning model architecture
- Uni app wechat applet customer service chat function
- Echo command – output a string or extract the value of a shell variable
猜你喜欢

Mongodb basic concept learning - set

Day21 performance test process

Day18 (set, generic, hash table, tree, stack and queue, graph, array and linked list)
Summary of 6 common methods of visual deep learning model architecture

C simple operation mongodb
The e-book "action guide for large organizations to further promote zero code application platform" was officially released!

Create a complete binary tree in array order
Yunda's cloud based business in Taiwan construction 𞓜 practical school

No one reads the series. Source code analysis of copyonwritearraylist
Vscode voice notes to enrich information (medium)
随机推荐
How often should you refactor- How often should you refactor?
[untitled]
Distributed solar photovoltaic inverter monitoring
Ethernet
Differences and connections between sap ui5 and openui5
Guava-IO
Summary of 6 common methods of visual deep learning model architecture
Simple student management system
Wireless industrial Internet of things data monitoring terminal
Uname command – displays system information
No one reads the series. Source code analysis of copyonwritearraylist
An example of recursion, the full permutation problem of 1~n
Technology inventory: past, present and future of Message Oriented Middleware
MySQL transaction learning notes (I) first encounter
Wind farm visualization: wind farm data
Volatile and JMM memory models
Timed thread pool
Curl command – file transfer tool
Invalid bound statement (not found)
Websocket in the promotion of vegetable farmers