


Interview question: Talk about how to optimize MYSQL database queries, mysql database_PHP tutorial
Interview question: Talk about how to optimize MYSQL database query, mysql database
1. Optimize data type
There are many data types in MySQL. If you are a DBA, you are strictly checking the data types according to the principle of optimization, but developers may choose the solution they think is the simplest to speed up coding. Speed, or choosing the most obvious choice, so you may not be faced with the best choice, and if possible, you should try to use general guidelines to change these decisions.
(1) Avoid using NULL
NULL requires special handling for most databases, and MySQL is no exception. It requires more code, more checks, and special index logic. Some developers are completely unaware that NULL is used when creating a table. is the default value, but most of the time you should use NOT NULL, or use a special value such as 0, -1 as the default value.
(2) Only smaller fields may be used
After MySQL reads the data from the disk, it stores it in memory, and then uses cpu cycles and disk I/O to read it. This means that the smaller the data type, the smaller the space it takes up. Reading from the disk Or packing it into memory is more efficient, but don't be too obsessed with reducing the data type. If there are any changes in the application in the future, there will be no space. Modifying the table will require reconstruction, which may indirectly cause code changes. This is a headache, so a balance needs to be found.
2. Be careful with character set conversion
The character set used by the client or application may be different from the character set of the table itself. This requires MySQL to perform implicit conversion during operation. In addition, it is necessary to determine whether the character set such as UTF-8 is supported. Multibyte characters, so they require more storage space.
3. Optimize count(my_col) and count(*)
If you use a MyISAM table, using count(*) without a where clause is very fast, because the statistics of the number of rows are very accurate, so MySQL will not look for them row by row. , and then get the number of rows. If there is no null value in the my_col column, then the situation will be the same as mentioned before, that is, count(my_col) will be very fast.
If you use count() when there is a where clause, basically no more optimization can be done. In the where clause, the obvious index columns are exceeded. For complex where clauses, you can only use covering indexes. It's useful.
In addition to the suggestions above, you can also use summary tables. They allow you to keep the contents of the table updated. You can use triggers, or application logic to keep the summary table always up to date, or periodically. Run a batch job to keep the population populated with the latest data information. If you do the latter, your information will be very close, but not exact. It depends on how often the batch job is run. This needs to be weighed against the application's need for accurate information. , and the system overhead of keeping data updated, a balance point must be found between the two.
4. Optimize subquery
When encountering subqueries, the MySQL query optimization engine is not always the most efficient. This is why subqueries are often converted into join queries. The optimizer can already handle join queries correctly. Of course One thing to note is to ensure that the connection column of the connection table (the second table) is indexed. On the first table, MySQL usually performs a full table scan relative to the query subset of the second table. This is Part of the nested loop algorithm.
5. Optimize UNION
Using UNION is an interesting optimization method when spanning multiple different databases. UNION returns data from two unrelated tables, which means that there will be no duplicate rows, and it must also be Sorting data, we know that sorting is very resource-intensive, especially sorting large tables.
UNION ALL can greatly speed up the process. If you already know that your data will not include duplicate rows, or you don’t care whether duplicate rows will appear, using UNION ALL is more suitable in both cases. In addition, some methods can be used in the application logic to avoid duplicate rows, so that the results returned by UNION ALL and UNION are the same, but UNION ALL will not be sorted.
Original text from [Bit Network]: http://soft.chinabyte.com/database/254/11335754.shtml
Related reading
MySQL optimization (how to query SQL statements with low execution efficiency in MySQL)
How to perform joint query on multiple tables with the same structure in mysql tens of millions of databases? How to optimize or set up to improve query speed?
Query optimization and paging testing of tens of millions of data in mysql database
mysql learning experience
8 ways to optimize Mysql database

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP development practice: Use PHPMailer to send emails to users in the MySQL database Introduction: In the construction of the modern Internet, email is an important communication tool. Whether it is user registration, password reset, or order confirmation in e-commerce, sending emails is an essential function. This article will introduce how to use PHPMailer to send emails and save the email information to the user information table in the MySQL database. 1. Install the PHPMailer library PHPMailer is

As the amount of data continues to increase, database performance has become an increasingly important issue. Hot and cold data separation processing is an effective solution that can separate hot data and cold data, thereby improving system performance and efficiency. This article will introduce how to use Go language and MySQL database to separate hot and cold data. 1. What is hot and cold data separation processing? Hot and cold data separation processing is a way of classifying hot data and cold data. Hot data refers to data with high access frequency and high performance requirements. Cold data

To what extent can I develop MySQL database skills to be successfully employed? With the rapid development of the information age, database management systems have become an indispensable and important component in all walks of life. As a commonly used relational database management system, MySQL has a wide range of application fields and employment opportunities. So, to what extent do MySQL database skills need to be developed to be successfully employed? First of all, mastering the basic principles and basic knowledge of MySQL is the most basic requirement. MySQL is an open source relational database management

How to use MySQL database for time series analysis? Time series data refers to a collection of data arranged in time order, which has temporal continuity and correlation. Time series analysis is an important data analysis method that can be used to predict future trends, discover cyclical changes, detect outliers, etc. In this article, we will introduce how to use a MySQL database for time series analysis, along with code examples. Create a data table First, we need to create a data table to store time series data. Suppose we want to analyze the number

How to use MySQL database for image processing? MySQL is a powerful relational database management system. In addition to storing and managing data, it can also be used for image processing. This article will introduce how to use a MySQL database for image processing and provide some code examples. Before you begin, make sure you have installed a MySQL database and are familiar with basic SQL statements. Create a database table First, create a new database table to store the image data. The structure of the table can be as follows

As the amount of data increases, database backup becomes more and more important. For the MySQL database, we can use the Go language to achieve automated incremental backup. This article will briefly introduce how to use Go language to perform incremental backup of MySQL database data. 1. Install the Go language environment. First, we need to install the Go language environment locally. You can go to the official website to download the corresponding installation package and install it. 2. Install the corresponding library. The Go language provides many third-party libraries for accessing MySQL databases, among which the most commonly used ones are

With the large amount of data that needs to be stored and processed, MySQL has become one of the most commonly used relational databases in application development. The Go language is becoming more and more popular among developers due to its efficient concurrency processing and concise syntax. This article will lead readers to implement reliable MySQL database connection through Go language, allowing developers to query and store data more efficiently. 1. Several ways for Go language to connect to MySQL database. There are usually three ways to connect to MySQL database in Go language, which are: 1. Third-party library

In recent years, the Go language has become increasingly popular among developers and has become one of the preferred languages for developing high-performance web applications. MySQL is also a popular database that is widely used. In the process of combining these two technologies, caching is a very important part. The following will introduce how to use Go language to handle the cache of MySQL database. The concept of caching In web applications, caching is a middle layer created to speed up data access. It is mainly used to store frequently requested data to
