当前位置:网站首页>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
边栏推荐
- SAP ui5 application development tutorial 32 - how to create a custom SAP ui5 control
- The locally developed SAP ui5 application is deployed to the ABAP server to perform error analysis
- Create a complete binary tree in array order
- CST8227
- Lesson 8: FTP server setup and loading
- Use of arrays tool class
- Websocket in the promotion of vegetable farmers
- Volatile and JMM memory models
- Leetcode sword finger offer question brushing - day 27
- MySQL -- optimize query statements and use not in with cases
猜你喜欢

Jenkins installation and configuration
Technology inventory: past, present and future of Message Oriented Middleware

RT thread i/o device model and layering

Mongodb basic concept learning - set

Get the first letter of Chinese phonetic alphabet in Excel and capitalize it

Understanding the dynamic mode of mongodb document
Part 33 of SAP ui5 application development tutorial - trial version of responsiveness of SAP ui5 applications
Some common errors and solutions of using SAP ui5 to consume OData services

MySQL tuning --01--- optimization steps and system performance parameters
[kicad image] download and installation
随机推荐
Feignclient reported an error npe:null
Lesson 8: FTP server setup and loading
Uni app wechat applet customer service chat function
CSDN cerebral palsy bug has wasted nearly two hours of hard work
Huawei machine test question: splicing URL
证券如何在线开户?在线开户是安全么?
Count the grid
Lesson 9: workspace introduction
C switch nested syntax
What is hybrid web containers for SAP ui5
[open source sharing] deeply study KVM, CEPH, fuse features, including open source projects, code cases, articles, videos, architecture brain maps, etc
The simplest way to tell you is to hash and not hash
Three tier architecture experiment
Leetcode topic [array] -36- effective Sudoku
50 days countdown! Are you ready for the Landbridge cup provincial tournament?
A + B Again
DOM proficient? What is the difference between node and elment?
SAP ui5 beginner tutorial No. 28 - Introduction to the integration test tool OPA for SAP ui5 applications
MySQL uses the where condition to find strange results: solve
What happens when redis runs out of memory