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:51【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 .
copyright notice
author[PHP Development Engineer ],Please bring the original link to reprint, thank you.
https://en.chowdera.com/2022/175/20210526103539118a.html
The sidebar is recommended
- [Tencent cloud 618 countdown!] Promotion strategy of the promotion activities
- Simple and flexible permission design?
- Making daily menu applet with micro build low code
- 99% of the students can't write good code because of this problem!
- Istio practical skills: using prism to construct multi version test services
- Kubernetes practical technique: setting kernel parameters for pod
- A scheme for crawlers to collect public opinion data
- Essential key steps in the construction of e-commerce live broadcast source code
- How do websites and we media tap user needs? Deeply expose the secrets behind the keywords!
- From theory to practice, decipher Alibaba's internal MySQL optimization scheme in simple terms
guess what you like
Continuous testing | key to efficient testing in Devops Era
It's settled! Bank retail credit risk control just does it!
Encapsulate the method of converting a picture file object to Base64
The pod is evicted due to insufficient disk space of tke node
Post processing - deep camera deformation effects
Tencent released credit risk control results safely: it has helped banks lend more than 100 billion yuan
Interesting erasure code
Five minutes to develop your own code generator
How to make secruecrt more productive
About me, a 19 line programmer
Random recommended
- Kubernetes practical skill: entering container netns
- [programming navigation] the practical code summarized by foreign great God, learned in 30 seconds!
- Design and implementation of high performance go log library zap
- Ghost, a synonym for blog system
- Engage in audio and video development? Several things I have to say about SRT live broadcast protocol
- IOMMU (VII) -vfio and mdev
- [highlights] summary of award-winning activities of Tencent cloud documents
- The programmer's graduation project is still bald after a year
- How to solve the problem that MBR does not support partitions over 2T, and lossless transfer to GPT
- Another prize! Tencent Youtu won the leading scientific and technological achievement award of the 2021 digital Expo
- Use txvideoeditor to add watermark and export video card at 99%? No successful failed callback?
- Reset the password, and the automatic login of the website saved by chrome Google browser is lost. What is the underlying reason?
- [log service CLS] Tencent cloud log service CLS accesses CDN
- [live broadcast of celebrities] elastic observability workshop
- Smart Policing: how to use video intelligent analysis technology to help urban policing visual comprehensive supervision and command system
- SMS SMS
- WPF from zero to 1 tutorial details, suitable for novices on the road
- Dingding, Feishu, and enterprise wechat: different business approaches
- [2022 national tournament simulation] BigBen -- determinant, Du Jiao sieve
- How to do research on plant endophytes? Special topic on Microbiology
- Mlife forum | microbiome and data mining
- 105. simple chat room 8: use socket to transfer pictures
- Parse NC format file and GRB format file dependent package edu ucar. API learning of netcdfall
- Reading notes of returning to hometown
- Generate the NC file of 4-D air pressure and temperature, and then read the code (provide the code)
- Codereview tool chain for micro medicine
- Babbitt | metauniverse daily must read: 618 scores have been announced. How much contribution has the digital collection made behind this satisfactory answer
- On the value foam of digital copyright works from the controversial nature of "Meng Hua Lu"
- Use the open source tool k8tz to gracefully set the kubernetes pod time zone
- How does Argo family bucket make Devops easier?
- A hero's note stirred up a thousand waves across 10 countries, and the first-line big factories sent people here- Gwei 2022 Singapore
- MySQL foreign key impact
- Common special characters in JS and TS
- The text to voice function is available online. You can experience the services of professional broadcasters. We sincerely invite you to try it out
- Who said that "programmers are useless without computers? The big brother around me disagrees! It's true
- what the fuck! I'm flattered. He actually wrote down the answers to the redis interview questions that big companies often ask!
- Concept + formula (excluding parameter estimation)
- [database] final review (planning Edition)
- Troubleshooting and optimization of files that cannot be globally searched by ordinary users in easydss video platform customization project
- How can ffmpeg streaming to the server save video as a file through easydss video platform?