Interviewer: Please talk about the optimization of the database

 

One: Introduction

MySQL performance optimization is done by optimizing all aspects, not just optimizing the SQL statement, but optimizing all aspects, optimizing each place, so that the overall performance will be significantly improved.

Two: optimization method

1. Optimize the design of the database table structure

Why does the design of database tables affect performance?

The data type of the field: different data types have different storage and retrieval methods, and the corresponding performance is also different, so it is necessary to choose the data type of the field reasonably. For example, the age of the person can be unsigned tinyint, and there is no need to use the length of the integer data type: the database will eventually be written to the disk, so the length of the field will also affect the I/O operation of the disk. If the length of the field is very long Large, then more I/O is required to read the data, so a reasonable field length can also improve the performance of the database. For example, the length of the user's mobile phone number is 11 digits, and there is no need to use 255 lengths. Table storage engine: Commonly used storage engines include MyISAM, InnoDB, and Memory. Different storage engines have different characteristics. Therefore, the strengths and advantages of each storage engine should be reasonably used to provide data performance. MyISAM does not support transactions and table-level locks, but the query speed is fast. InnoDB supports transactions and row locks. 2. SQL optimization

A very important method of MySQL performance optimization is the optimization of SQL statements. The most important way is to use indexes.

3. Sub-table

When the amount of data in a table is large, the query becomes very slow, so reducing the number of records in the table is an optimized way. This method is to split the data of one table into multiple tables. In this way, the number of each table is reduced, so that the query speed is relatively faster.

Large tables have a certain impact on DDL operations. For example, creating indexes, adding fields, and modifying the table structure require a long time to lock the table, which will cause long-term master-slave delays and affect normal data operations.

4. Big business

Large transaction: the running time is relatively long, and the operation data is relatively large. Transaction risk: locking too much data will cause a lot of blocking and lock timeout, the time required for rollback is relatively long, and the long execution time will easily cause master-slave delay

Avoid processing too much data at once and remove unnecessary select operations in the transaction

5. Database parameter configuration optimization (very important)

MySQL is a highly customized database system that provides many configuration parameters (such as the maximum number of connections, memory occupied by the database, etc.). These parameters have default values. Generally, the default values ​​are not the best configuration, and generally need to be based on the application. The characteristics of the program and hardware conditions adjust the configuration of mysql.

For example, the maximum number of connections is 100 by default. Even if the SQL statement is optimized and the hardware device configuration is high, you will have to wait when the request exceeds 100. This is the unreasonable configuration that prevents MySQL from exerting its maximum capabilities.

6. Master-slave replication, separation of read and write

The number of concurrency supported by a MySQL server at the same point in time is limited. When a large amount of concurrency (such as spike activities, many users access the database at the same time), one database cannot handle it, so increasing the number of MySQL servers is also a way Ways to enhance database performance.

By using MySQL master-slave replication, adding, deleting and modifying operations go to the Master master server, and queries go to the Slaver slave server, thus reducing the pressure of only one MySQL server.

7. Increase the cache layer

Reducing database connections is also an optimization method. Some queries do not need to access the database. You can increase the cache by using cache servers such as redis, memcache, elasticsearch, etc., to reduce database connections

8. Upgrade server hardware

When all optimization methods are used, and performance still needs to be optimized, then only upgrade the MySQL server hardware, faster disk IO devices, stronger CPU, larger memory, larger network card traffic (bandwidth), etc.

In short, the improvement of MySQL performance is improved through various aspects, and each aspect is improved a little, and the overall increase will be significantly improved.

Guess you like

Origin blog.csdn.net/qq_27828675/article/details/106997994