当前位置:网站首页>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:00 【PHP 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 .
other
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 :http://github.crmeb.net/u/defu
come from “ Open source world ” , link :http://ym.baisou.ltd/post/616.html, If you want to reprint , Please indicate the source , Otherwise, the legal liability will be investigated .
边栏推荐
- In depth analysis, from ordinary clock system to various time service modes
- Go basic series | 1 Leading
- Kubernetes practical skills: use cert manager to issue free certificates for DNSPod domain names
- SCRM, a breakthrough in the new consumption era
- Concentrate on research preparation, Tencent cloud, see you next year!
- How does easygbs, a national standard platform, solve the problem that information cannot be carried across domains?
- Troubleshooting and optimization of files that cannot be globally searched by ordinary users in easydss video platform customization project
- 钉钉、飞书、企业微信:迥异的商业门道
- 使用开源工具 k8tz 优雅设置 Kubernetes Pod 时区
- How to make Baidu quickly include its own personal website?
猜你喜欢

Pycharm中使用Terminal激活conda服务(终极方法,铁定可以)

Dingding, Feishu, and enterprise wechat: different business approaches

The text to voice function is available online. You can experience the services of professional broadcasters. We sincerely invite you to try it out

“我这个白痴,招到了一堆只会“谷歌”的程序员!”

微医CodeReview工具链

Use the open source tool k8tz to gracefully set the kubernetes pod time zone

Reading notes of returning to hometown

How to do research on plant endophytes? Special topic on Microbiology

mLife Forum | 微生物组和数据挖掘

MySQL 外键影响
随机推荐
pipeline groovy
How does easygbs, a national standard platform, solve the problem that information cannot be carried across domains?
Post processing - deep camera deformation effects
Easynvr user login is modified to share the modification process of ip+ user name restriction
一文讲透植物内生菌研究怎么做 | 微生物专题
Use the open source tool k8tz to gracefully set the kubernetes pod time zone
Tencent cloud and the ICT Institute launched the preparation of the "cloud native open source white paper" to deeply interpret cloud native
巴比特 | 元宇宙每日必读:618成绩已然揭晓,在这份还算满意的答卷背后,数字藏品做出了多少贡献?...
Who said that "programmers are useless without computers? The big brother around me disagrees! It's true
What if the WordPress website forgets its password
Experience of IOS interview strategy - App testing and launching
How to do research on plant endophytes? Special topic on Microbiology
Istio practical skills: using prism to construct multi version test services
[day ui] affix component learning
2021-06-02: given the head node of a search binary tree, it will be transformed into an ordered two-way linked list with head and tail connected.
用一个软件纪念自己故去的母亲,这或许才是程序员最大的浪漫吧
A hero's note stirred up a thousand waves across 10 countries, and the first-line big factories sent people here- Gwei 2022 Singapore
Discussion on redis communication protocol
The world's largest meat processor has been "blackmailed", how many industries will blackmail virus poison?
What is the reason why the video intelligent analysis platform easycvr is locally controllable but the superior equipment cannot control the subordinate equipment?