http://dev.cmcm.com/archives/107Posted onOctober 16, 2014by liuding | 7 Reviews
The following optimizations are based on some of the optimized finishing of the CentOS system, with incomplete or controversial locations looking to continue to complement.
First, MySQL level optimization
1. innodb_flush_log_at_trx_commit 设置为2
Setting 0 is the transaction log (IB_LOGFILE0, ib_logfile1) writes to log buffer,1 per second is always write, 2 is to write the file system cache first, and then brush the disk every second, and 0 is the difference is selected 2 even if MySQL crashes will not lose data.
2. innodb_write_io_threads=16 (该参数需要在配置文件中添加,重启mysql实例起效)
Dirty Page writes the number of threads, increasing this parameter can improve the write performance. mysql5.5.
3. innodb_max_dirty_pages_pct 最大脏页百分数
When the percentage of dirty pages in the system exceeds this value, InnoDB writes to write the updated data in the page to the disk file. By default 75, it is hard to reach this ratio for SSD drives that are generally popular today. Can be adjusted according to the actual situation between 75-80
4. innodb_io_capacity=5000
The number of dirty pages refreshed one time when dirty pages are flushed from the buffer. The following are generally recommended settings based on the capacity of disk IOPS:
SAS 200
SSD 5000
Pci-e 10000-50000
5. innodb_flush_method=O_DIRECT(该参数需要重启mysql实例起效)
Controls the open, brush-write mode of the InnoDB data file and redo log. There are three values: Fdatasync (Default), O_dsync,o_direct.
- Fdatasync mode: When writing data, write this step does not need to really write to the disk is completed (may write to the operating system buffer will return to completion), the real completion is the flush operation, buffer to the operating system to flush, Also, the metadata information for the file needs to be updated to disk.
- O_dsync mode: The Write log operation is done in write, and the data file is written in flush this step through fsync.
- O_direct mode: The data file write operation is directly from the MySQL InnoDB buffer to the disk, and does not pass through the operating system buffering, and the real completion is also in the flush this step, the log still goes through the OS buffering.
Three modes such as: through the graph can see O_direct compared to fdatasync advantage is to avoid double buffering, itself InnoDB buffer pool is a buffer, do not need to write to the system buffer, but there is a disadvantage is directly written to the disk, So the order of reading and writing is less efficient than fdatasync.
In a large number of random write environments, O_direct is more efficient than fdatasync, more sequential write, or the default Fdatasync more efficient.
6. innodb_adaptive_flushing 设置为 ON (使刷新脏页更智能)
Affects the number of dirty pages refreshed per second. The rule is changed from "100 Dirty pages to disk" when the original "is greater than innodb_max_dirty_pages_pct" to "through Buf_flush_get_desired_flush_ The reate function determines the rate at which the redo log is generated to determine the most appropriate number of dirty pages to refresh, and a certain amount of dirty pages is refreshed even when the dirty page scale is less than innodb_max_dirty_pages_pct.
7. innodb_adaptive_flushing_method 设置为 keep_average
Affects checkpoint, the more average calculation adjusts the speed of the brush dirty pages, making the necessary flush. (this variable is a variant of MySQL derived version Percona server, native MySQL does not exist)
8. innodb_stats_on_metadata=OFF
Turn off some index statistics that are generated by accessing the following table in the INFORMATION_SCHEMA library.
When the MySQL instance is restarted, MySQL randomly iterates through all the tables to sample the data, which is not used much in practice and is recommended to close.
9. innodb_change_buffering=all
When the page that corresponds to the updated/inserted nonclustered index is not in memory (an update operation on a nonclustered index usually brings random IO), it is placed into an insert buffer, and the changed records are then merge into the page when the page is subsequently read into memory. The background thread also does a merge operation when the server is relatively idle.
Because the main use of the advantages of the merge to reduce IO, but for some scenarios do not make fixed data changes many times, here do not need to update/insert operation to open change_buffering, if the opening is only unnecessary to occupy the buffer_pool space and processing power. This parameter is configured according to the actual business environment.
10. innodb_old_blocks_time=1000
Block in old sublist for a length of 1s, will not be transferred to the new sublist, avoid buffer pool contaminated BP can be considered a long linked list. is divided into young and old two parts, where old defaults to 37% of the size (configured by innodb_old_blocks_pct). A page near the top indicates a recent visit. A page near the end indicates that it has not been accessed for a long time. And the intersection of these two parts became midpoint. Whenever a new page needs to be loaded into BP, the page is inserted into the midpoint location and declared as Old-page. When the page of the old part is accessed, the page is promoted to the top of the list, identified as young.
Because the operation of the table scan is to load the page first, and then immediately triggers a visit. So when innodb_old_blocks_time = 0 o'clock, the page that is required for table scan is not read as the young page is added to the top of the list. Some less frequently used page will be squeezed out of BP, so that the subsequent SQL will generate disk IO, resulting in slower response times.
At this point, although the page accessed by mysqldump is constantly loading on the LRU top, the high-frequency hotspot data access will preempt the page to the LRU top again at a faster rate. The page that causes Mysqldump to load will be quickly brushed down and immediately be evict (obsolete). As a result, time=0 or 1000 does not have a significant impact on access to this stressful environment because the dump data simply grabs the hotspot data. Not only dump, but also when big data is being manipulated.
Second, MySQL system level optimization
1. 关闭 numa=off, 或修改策略为interleave(交织分配内存)防止意外的swap
The NUMA policy introduces the concept of node, where each physical CPU is treated as a node, and each node has a local memory, which is externally accessed relative to node other than node.
The NUMA memory allocation policy has LocalAlloc (default), preferred, Membind, Interleave.
- LOCALALLOC specifies that the process request allocates memory from the current node;
- Preferred more loosely specifies a recommended node to get the memory, and if there is not enough memory on the recommended node, the process can try another node.
- Membind can specify a number of node, and the process can only request memory from those specified node.
- The interleave specifies that the process requests the allocation of memory from a specified number of node Round-roll by interleaving the algorithm.
Each process (or thread) assigns a priority node, and there is a problem with the system's default LocalAlloc policy, and for MySQL, which almost fills up the entire system memory, it is very easy to fill a node's resources, If Linux allocates a large resource to a node that is already full of resources, it will have insufficient resources, causing memory data to be exchanged on disk, or to abandon active data in Buffer_pool. In the actual test found such as NODE0, Node1 Two physical node, when the system load is high, NODE0 resources are full, node1 although there is still some free memory, but the system even if the memory to disk Exchange will not take advantage of the idle resources on the Node1.
It is therefore advisable to turn off NUMA or set policy to allocate memory for interleaving for a large and complex process such as MySQL. But for the number of instances on a machine, each instance can be bound to a CPU core. You can then take advantage of the NUMA features to be more efficient.
2. 增加本地端口,以应对大量连接
Echo ' 1024 65000′>/proc/sys/net/ipv4/ip_local_port_range
This parameter specifies the allocation range for the port, which is the limit for out-of-access. MySQL default listener 3306 Port Even if there are multiple request links, it will not affect. However, since MySQL is a high-memory, high-CPU, high-io application, it is not recommended how much should be used for MySQL mashup on the same machine. Even if the volume of business is small, it can also be achieved by reducing the configuration of a single machine, multiple machines coexistence to achieve better.
3. 增加队列的链接数
echo ' 1048576 ' >/proc/sys/net/ipv4/tcp_max_syn_backlog
The larger the number of linked queues, the better, but from another point of view, it should be more appropriate to use a connection pool in the actual environment, avoiding the performance cost of repeatedly establishing links. With connection pooling, the number of links can be more manageable from the application level.
4. 设置链接超时时间
Echo ' Ten ' >/proc/sys/net/ipv4/tcp_fin_timeout
This parameter is primarily intended to reduce the amount of resource time that time_wait occupies. Especially for HTTP short link service side or MySQL does not adopt connection pool effect is obvious.
Third, other aspects of optimization considerations
For high dau business MySQL instances, it is recommended not to begrudge memory, using 128G or higher memory, InnoDB good use of memory advantages to improve the performance of MySQL, we will give him enough space to play his performance. Disk IO performance is far less than the speed of memory processing, this is understandable, the optimization as far as possible to the requirements of the operation of the IO block to the memory directly back to the client.
Of course, the memory can not be high enough to cache all the data in memory, in most of the actual business is still dependent on the majority of random Io, more like the current comparison of the fire of the Hand tour, is a high-random write business type, each cloud vendors also default to provide SSD even higher demand PCIe flash storage device. The second option is to improve the IO performance of the disk.
Another business if large enough, a single machine will not be able to support, it is necessary to consider the sub-Library, sub-table, most of the products companies do or by business division of the library. If a business is too large for a single machine to withstand, it needs to be operated by a table and a sub-library. In fact, regardless of whether the business will be bigger, when the start of the project should be done to support the sub-Library, can be divided into the table (as far as possible to avoid using the table's self-increment ID as a business ID), sub-business (as far as possible to avoid transaction operations, and even allow some acceptable sacrifices in business, otherwise difficult to Otherwise, only face the embarrassing scenes of reconstruction. The advantage of this is that even if some of the wrong designs are poorly conceived, there is no time to refactor due to the various causes of the business, and you can reduce the impact of a single point by scaling, migrating, and then slowly optimizing later.
It is important to note that the existence of a "killer" SQL statement will invalidate all of the above optimizations. For example, the data of tens of millions of tables without index search, sorting, calculation. Therefore, you must turn on the slow query log to troubleshoot all slow query statements.
The above for the younger brother some of the actual use of some of the collation of MySQL, there are insufficient places also hope to supplement, and gradually improve. Next article will be for a popular hand tour "warm around the world" in the actual environment of the business API, cache, MySQL and other deployment and encountered problems, around the above optimization points to do some summary, there are still some remnants of pits and unhandled points, but also hope to continue pointing. Thank you for reading.
Posted in MySQL. | 571 views
Latest First Hottest
MySQL several optimization points on high memory and IO utilization (Sync+fsync) Cheetah Mobile Technology Blog