当前位置:网站首页>MySQL optimization
MySQL optimization
2022-06-24 05:42:00 【Yuehai Street Fahrenheit】
If you can build a picture in your mind MySQL The architecture of how the components work together , Helps to understand MySQL The server . The following figure shows MySQL The logical architecture diagram .
MySQL The logic architecture is divided into three layers , The top layer is the client layer , Is not MySQL Unique , Such as : Connection processing 、 Authorized certification 、 Security and other functions are handled in this layer .
MySQL Most of the core services are in the middle tier , Including query parsing 、 analysis 、 Optimize 、 cache 、 Built in functions ( such as : Time 、 mathematics 、 Functions like encryption ). All cross storage engine functions are also implemented at this level : stored procedure 、 trigger 、 View etc. .
At the bottom is the storage engine , He is responsible for MySQL Data storage and extraction in . and Linux The file system under is similar to , Each storage engine has its advantages and disadvantages . The middle service layer passes through API Communicating with the storage engine , these API The interface masks the differences between different storage engines .
MySQL Query process of
mysql Query process of
Next, let's talk about optimization methods
1.Scheme Design and data type optimization
Choose data type as long as you follow the principle of small and simple , Smaller data types are usually faster , Less disk usage 、 Memory , What is needed in handling CPU There are fewer cycles . The simpler the data type, the less CPU cycle , such as , Integer is cheaper than character operation , Thus, integers are used to store ip Address , Use DATETIME To store time , Instead of using strings .
2. Create high-performance indexes
The index is to improve MySQL An important way to query performance , But too many indexes can lead to high disk utilization and high memory consumption , This affects the overall performance of the application . Try to avoid thinking about adding indexes afterwards , Because you may need to monitor a lot of SQL To locate the problem , And the time of adding index must be much longer than the time of adding index initially , It can be seen that the addition of index is also very technical .
3. Specific types of query optimization COUNT() Inquire about
COUNT() Probably the most misunderstood function , It has two different functions , One is to count the number of column values , The second is to count the number of lines . When counting column values , The column value is required to be non empty , It doesn't count NULL. If you confirm that the expression in parentheses cannot be empty , It's actually counting lines . The simplest is when using COUNT(*) when , It's not expanded into all the columns as we think , actually , It ignores all columns and counts the number of rows directly .
4. Optimize associated queries
In the big data scenario , Tables are associated with each other through a redundant field , It's better than using it directly JOIN Better performance . If you really need to use associated queries , Here's the thing to watch out for : Make sure ON and USING There is an index on the column in the sentence . When you create an index, you need to consider the order of associations . Watch A And table B Use column c When connected , If the order of optimizer association is A、B, Then there's no need to be A Create an index on the corresponding column of the table . Indexes that are not used can bring extra burden , Generally speaking , Unless there is any other reason , Just create the index on the corresponding column of the second table in the associated order ( The specific reasons are analyzed below ). Make sure that any GROUP BY and ORDER BY The expression in involves only columns in a table , such MySQL It's possible to use indexes to optimize .
5. Optimize LIMIT Pagination
When paging is needed , You usually use LIMIT Plus the offset to achieve , At the same time, add the right ORDER BY Words and expressions . If there is a corresponding index , Usually the efficiency will be good , otherwise ,MySQL A lot of file sorting operations need to be done .
6. Optimize UNION
MySQL Handle UNION The strategy is to create a temporary table first , Then insert each query result into the temporary table , Finally, we will do the inquiry . So many optimization strategies are UNION There's no way to be very good in the query . It is often necessary to manually set WHERE、LIMIT、ORDER BY Wait for words “ Push down ” Go to each subquery , So that the optimizer can make full use of these conditions to optimize first .
Unless you really need the server to be de duplicated , Otherwise, we must use UNION ALL, without ALL keyword ,MySQL We will add a temporary table DISTINCT Options , This will cause the data of the whole temporary table to be uniqueness checked , The cost of doing so is very high . Of course, even if you use ALL keyword ,MySQL Always put the results in a temporary table , Then read out , And back to the client . Although many times there is no need , For example, sometimes you can directly return the results of each subquery to the client .
边栏推荐
- What is the third level domain name? How to choose a domain name agent
- What should I pay attention to in server hosting and migration?
- How to register an enterprise domain name? Can an enterprise domain name be directly registered by individuals?
- How to set the secondary domain name of the website? What should I pay attention to when setting the domain name?
- How to make a secondary domain name? What are the advantages of secondary domain names?
- How do users check the domain name registrar? What are the conditions for domain name registration?
- What if the domain name is blocked? What can I do to quickly unseal?
- How to register a domain name how to select a domain name registrar
- 2021, how to select a programming language?
- How to resolve the domain name to IP? How long does it take for the domain name resolution to take effect?
猜你喜欢

Answer questions! This article explains the automated testing framework in software testing from beginning to end
Easy to understand JDBC tutorial - absolutely suitable for zero Foundation

How should we learn cloud native in 2022?
What cloud native knowledge should programmers master?
Learning routes and materials for cloud native O & M engineers
随机推荐
[latest offer] 2-core 4G server is limited to 188 yuan, and it is in the process of being snapped up
Massif tool of Valgrind
Disaster recovery series (III) -- cloud network disaster recovery construction
His secret script of hard technology investment
What domain name is cheap? How much does it cost to register a domain name?
Wang Wei, senior architect of coding Devops, was selected as the first batch of tutors in Mulan open source community
What domain name is XYZ? How to register a domain name
How to register a domain name how to make the domain name short and easy to remember
What is the website domain name and how to register the secondary domain name?
How to do reverse domain name resolution? What does reverse domain name resolution mean?
test
How do virtual hosts bind domain names? Can binding failure be used normally?
How to build a website after registering a domain name? Do you need maintenance later?
What is domain name resolution? How much does domain name registration cost
Explain thoroughly and learn rotten binary tree (4): storage structure of binary tree - build heap - Search - sort
What is a first level domain name? What are the steps to purchase a primary domain name?
"Yi Jian Xing" was "Internet stormed". What countermeasures does the game company have other than "rather be broken jade"?
Why should the domain name be filed? What materials are needed for the filing of enterprise domain names?
Spirit breath development log (6)
How to register a secondary domain name? What are the precautions when registering?