current position:Home>Are you still working hard to select *? Then put away these skills

Are you still working hard to select *? Then put away these skills

2022-06-24 12:46:51PHP Development Engineer

️Photo by Kamil S on Unsplash

Applications are as slow as cattle , There are many reasons , Maybe it's the Internet 、 Maybe it's the architecture of the system , And maybe it's the database .

So how to improve the database SQL Statement execution speed ? Some people will say that performance tuning is Database Administrator (DBA) What happened , However, performance tuning has a lot to do with programmers .

A line embedded in a program SQL sentence , If you use some optimization tips , It will achieve twice the result with half the effort .

skill 1  The comparison operator can use “=” You don't have to “<>”

“=” Increased index usage .

skill 2  Knowing that there is only one query result , Please use “LIMIT 1”

“LIMIT 1” It can avoid full scan , If you find the corresponding result, you won't continue scanning .

skill 3  Choose the appropriate data type for the column

It works TINYINT You don't have to SMALLINT, It works SMALLINT You don't have to INT, You know the truth , The smaller the disk and memory consumption, the better .

skill 4  Will be big DELETE,UPDATE or INSERT Query becomes multiple small queries

Can write dozens of lines 、 Hundreds of rows. SQL Does the sentence seem to be forced high ? However , In order to achieve better performance and better data control , You can turn them into multiple queries .

skill 5  Use UNION ALL Instead of UNION, If the result set allows repetition

because UNION ALL No weight removal , Efficiency is higher than UNION.

skill 6  To get multiple executions of the same result set , Please keep SQL The sentences are consistent

The purpose of this is to make full use of the query buffer .

For example, according to the region and products id Check the product price , First use :

So the second same query , Please keep the above statements consistent , For example, don't put where In the sentence id and region The order of position change .

skill 7  Avoid using “SELECT *”

If you don't query all the columns in the table , Avoid using SELECT *, Because it will do a full table scan , Can't use index effectively , Increased the load on the database server , And the network between it and the application client IO expenses .

skill 8  WHERE The columns in the clause are indexed as much as possible

It's just “ As far as possible ” Oh , Not all the columns . Adjust measures to local conditions , Adjust according to the actual situation , Because sometimes too many indexes can also degrade performance .

skill 9  JOIN The columns in the clause are indexed as much as possible

It's just the same “ As far as possible ” Oh , Not all the columns .

skill 10  ORDER BY The columns of are indexed as much as possible

ORDER BY If the columns of are indexed , Performance will be better .

skill 11  Use LIMIT Implement paging logic

Not only improved performance , At the same time, it reduces unnecessary network transmission between database and application .

skill 12  Use EXPLAIN Keyword to view the execution plan

EXPLAIN You can check index usage and rows scanned .


SQL There are many ways to tune , The same query results can be queried in many different ways . In fact, the best way is to test in the development environment with the most realistic dataset and hardware environment , And then release it to the production environment .

Complete example :

come from “ Open source world ” , link :, If you want to reprint , Please indicate the source , Otherwise, the legal liability will be investigated .

copyright notice
author[PHP Development Engineer ],Please bring the original link to reprint, thank you.

Random recommended