Examples from real lifeHave we seen that many query statements in the company are select * xxxx The idea in my mind must be that others have written select *, so I will write it and save a lot of trouble. Slow query
Three indicators of slow query overhead in the database
If you go to index select * from stu where id = 1; The index will optimize the query and only return ten data. If there is no id index, it will be estimated that hundreds or thousands of rows of data will be accessed. Tips: Use the EXPLAIN command in MySQL to view the number of rows corresponding to the rows In fact, the best situation is to filter out unmatched records through where at the storage engine level. The second best situation is to cover the index hit scan and filter out unmatched records through where at the server level without returning to the table for query. The best situation is to return data from the data table and then filter out records that do not meet the conditions. How to optimizeUse index coverage to put all the columns we need in the index, so that we can avoid going back to the table to query and can separate the table 3. Refactor the query (you can break down a large query into smaller ones) For example: count, limit, max() countThe best optimization for count is to add a summary table, because count inevitably needs to scan a large number of rows. limitLimit is very common when we do paging, as shown in the following code select id from stu order by id limit 1000,20; This statement will query 1020 data and then discard the first 1000 and return the 20 data from 1000 to 1020. The best way to optimize is to use the index, so that the limit query can be turned into a query with a known position. Maximum value and minimum value min&maxFirst, let's imagine that if we use the primary key index, then the first value when we query is the minimum value we want to return. We can also use the primary key index and use limit to control the amount of data, thus achieving the effect of the min() function, thereby replacing min select id from stu use index(primay) where address = 'bj' limit 1; This will scan as few records as possible. Finally, let’s do two questions to relax. Find the second highest salary in the `Employee` table (Salary) +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ Results +---------------------+ | SecondHighestSalary | +---------------------+ | 200 | +---------------------+ Answer: select max(Salary) SecondHighestSalary from Employee where salary < (select max(salary) from Employee) Find all duplicate email addresses in the `Person` table. +----+---------+ | Id | Email | +----+---------+ | 1 | [email protected] | | 2 | [email protected] | | 3 | [email protected] | +----+---------+ Results+---------+ | Email | +---------+ | [email protected] | +---------+ Answer: select Email from Person group by Email having count(Email) >= 2; This is the end of this article about MySQL select optimization solutions. For more relevant MySQL select optimization content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: CSS style to center the HTML tag in the browser
>>: Fixed a bug caused by scrollbar occupying space
1: Differences in speed and loading methods The di...
Installing and deploying a private Docker Registr...
Install Install ts command globally npm install -...
Standalone hbase, let’s talk about it first. Inst...
Operation effect html <head> <meta chars...
When I was working on a project recently, I found...
In the previous article, we wrote about how to de...
By default, Docker runs over a non-networked UNIX...
This article shares the specific code of vue+vide...
Use JS to zoom in and out when the mouse is on th...
1. Nginx installation steps 1.1 Official website ...
Brief description Suitable for readers: Mobile de...
I've been learning about stacking contexts re...
Written in front I don’t know who first discovere...
HTML reuse is a term that is rarely mentioned. Tod...