当前位置:网站首页>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
边栏推荐
- How do product managers get started? How do they learn when no one takes them?
- Day21 JMeter usage basis
- Which securities company is good for opening a mobile account? Is it safe to open a mobile account?
- Technology Review: Interpretation of cloud native architecture trend in 2022
- Farewell to Lombok in 996
- An example of recursion, the full permutation problem of 1~n
- Uname command – displays system information
- SAP ui5 application development tutorial XXIX - Introduction to routing and navigation functions of SAP ui5 trial version
- Introduction to the main features of kyma when the cloud native application runs
- [kicad image] download and installation
猜你喜欢
Gavin's insight on transformer live class - line by line analysis and field experiment analysis of insurance BOT microservice code of insurance industry in the actual combat of Rasa dialogue robot pro
[open source sharing] deeply study KVM, CEPH, fuse features, including open source projects, code cases, articles, videos, architecture brain maps, etc
The e-book "action guide for large organizations to further promote zero code application platform" was officially released!
Uni app wechat applet customer service chat function
MySQL transaction learning notes (I) first encounter
C switch nested syntax
Multithreading and thread pool
What happens when redis runs out of memory
Technology inventory: Technology Evolution and Future Trend Outlook of cloud native Middleware
Vscode voice notes to enrich information (Part 1)
随机推荐
Click to send text messages without response is a common problem for many users in building the elegant grass Dragonfly Q system - solve the problem of clicking to send text messages without response
Find command – find and search for files
What happens when redis runs out of memory
What are the reasons why most webmasters choose Hong Kong site group servers?
Trial version of routing history and routing back and history of SAP ui5
PAT (Advanced Level) Practice 1025
What changes have taken place in the project file after SAP ui5 tools ran the Fiori add deploy config command
Guava common collection tool classes
Soft exam information system project manager_ Information system security management - Senior Information System Project Manager of soft test 026
Synchonized introduction
Create a complete binary tree in array order
Oracle SQL statement operand: rounding, rounding, differentiation and formatting
Processes and threads - concepts and process scheduling
Tencent and China Mobile continued to buy back with large sums of money, and the leading Hong Kong stocks "led" the market to rebound?
Interview experience - list of questions
Technology Review: Interpretation of cloud native architecture trend in 2022
Mongodb delete data
SAP ui5 application development tutorial XXIX - Introduction to routing and navigation functions of SAP ui5 trial version
[interview with a large factory] meituan had two meetings. Was there a surprise in the end?
Huawei machine test question: splicing URL