MySQL simple optimization

The company upgraded the configuration of the server, and the memory shortage problem was solved, and a few simple ways to optimize MySQL came to mind. First of all, two things are clear. Our business scenario is a common high-concurrency web service, and the query speed is the top priority. In addition, the database must be backed up before optimization, otherwise the problem will only run away.

1. Use a solid-state drive

This optimization method has no technical content, and it is completely to pay for performance, but it has to be said that this method is quite simple, rude, and effective. With the high-frequency read and write speed of SSDs, the performance of MySQL can be greatly improved

2. Add index

When the amount of data reaches a certain level, adding a suitable index is necessary. MySQL is a B+ tree or its variant tree, which sorts the data according to the data structure of the index to optimize the query effect. If the data in the table is frequently inserted and deleted, these operations will damage the index, causing the index to occupy a lot of invalid space. I have seen a data table, so it is much larger than the amount of data, and the query speed is too slow to add an index. The reason is the frequent insertion and deletion. In this scenario, the index needs to be deleted regularly and then regenerated.

3. Remove foreign keys

During college , the teacher emphasized that it is necessary to use foreign keys to constrain data consistency. I also strictly adhered to this view in school, after all, it allowed me to write less code and throw consistency problems to the database. Combined with real business scenarios after work, the database often becomes a performance bottleneck, while the service does not become a performance bottleneck. Therefore, it is necessary to write the solution to the data consistency problem into the service to reduce the pressure on the database.

4. Use InnoDB

Many facts show that InnoDB has advantages over MyISAM. InnoDB's use of memory is more comprehensive, and the access speed of memory is obviously faster than that of disk

5. Set InnoDB memory

The innodb_buffer_pool_size parameter represents the size of the memory allocated to InnoDB. When allocating memory, leave enough memory for the operating system. According to the experience of my colleagues, it is possible to allocate 80% of the server's memory for innodb_buffer_pool_size. Of course, the prerequisite for this is that your service is basically only MySQL. If there are other memory-intensive services such as data parsing on the server, innodb_buffer_pool_size The value should be reduced as appropriate

6. Set up InnoDB multitasking

If the memory allocated to innodb_buffer_pool_size is greater than 2G, we can consider dividing the InnoDB buffer pool into multiple ones, and we can modify the innodb_buffer_pool_instances parameter in the configuration. For high-concurrency services, the performance bottleneck is often multi-threaded access to MySQL. Dividing more buffer pools can effectively alleviate this problem. Of course, the more buffer pools, the better. If the memory of each buffer pool is too low, the advantages of multiple buffer pools cannot be exerted. The official recommendation is that each buffer pool needs at least 1G of memory.

 

Finally, remind everyone that the configuration changes to MySQL need to restart MySQL to take effect.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324394733&siteId=291194637