Five configuration parameters that affect MySQL Performance

Source: Internet
Author: User

The followingArticleI am mainly introducing the five configuration parameters that closely affect MySQL performance. I saw the five configuration parameters that closely affect MySQL performance on the relevant website a few days ago, let's share it with you. I hope you will have some gains.

(1) Connection

The connection usually comes from the Web server. The following lists some connection-related parameters and how to set them.

1. max_connections

This is the maximum number of connections allowed by the Web server. Remember to use the session memory for each connection (this article will cover the session memory ).

2. max_packet_allowed

The maximum data packet size is usually equal to the size of the largest data set you need to return in a large block. If you are using remote mysqldump, the value of this data packet must be larger.

3. aborted_connects

Check the counters in the system status to make sure they do not grow. If the number increases, it indicates that the client encountered an error when connecting.

4. thread_cache_size

An inbound connection creates a new thread in MySQL, because it is cheap and fast to open and close the connection in MySQL, and it is not like other databases, for example, there are so many persistent connections in Oracle, but the pre-creation of threads will not save time, which is why MySQL thread cache is required.

If you are growing, pay close attention to the created thread so that your thread cache is larger. For thread_cache_size of 2550 or 100, the memory usage is not large.

(2) query Cache

(3) temporary tables

The memory speed is quite fast, so we want all the sorting operations to be carried out in the memory. We can adjust the query to make the result set smaller to achieve memory sorting, or set the variable bigger.



Whenever you create a temporary table in MySQL, it uses the minimum values of these two variables as the critical value. In addition to creating a temporary table on the disk, it also creates many sessions, these sessions will seize limited resources, so it is best to adjust the query instead of setting these parameters higher. At the same time, note that tables with BLOB or text fields will be directly written to the disk. In-depth introduction to MySQL two-way replication technology

(4) Session memory

Each session in MySQL has its own memory, which is the memory allocated to the SQL query, so you want to make it as large as possible to meet your needs. However, you have to balance the number of consistent sessions in the data base at the same time. The black art here is that MySQL allocates cache on demand. Therefore, you cannot just add them and multiply them by the number of sessions. This is estimated to be much larger than the typical MySQL usage.

The best practice is to start MySQL, connect all sessions, and continue to pay attention to the virt column of top-level sessions. The number of mysqld rows is usually relatively stable, which is the actual total memory usage, after all the static MySQL memory areas are subtracted, the actual memory of all sessions is obtained. Then, the average value is obtained by dividing the number of sessions.

1. read_buffer_size

Cached continuously scanned blocks. This cache is cross-storage engine, not just a MyISAM table.

2. sort_buffer_size

The size of the cache for sorting. It is best to set it to 1 m-2 m, and then set it in the session to set a higher value for a specific query.

3. join_buffer_size

The size of the cache area allocated for execution of the joint query is set to 1-2 MB, and then separately set as needed in each session.

4. read_rnd_buffer_size

For sorting and order by operations, it is best to set it to 1 m, and then you can set it as a session variable to a larger value in the session.

(5) Slow query logs

Slow log query is a useful feature of MySQL.

1. log_slow_queries

In MySQL parameters, the log_slow_queries parameter is in my. set it in the CNF file and set it to on. By default, MySQL puts the file in the data directory, and the file uses "host name-slow. log, but you can also specify a name for this option.

2. long_query_time

The default value is 10 seconds. You can set it dynamically. The value ranges from 1 to on. If the database is started, the log is disabled by default. As of 5.1.21 and the version with the Google patch installed, this option can be set in microseconds. This is a great feature, because once you have eliminated all queries whose query time exceeds 1 second, the adjustment is very successful, which helps you eliminate the problem SQL before the problem increases.

3. log_queries_not_using_indexes

Enabling this option is a good idea. It actually records the query that returns all rows.


We have introduced five categories of MySQL parameters. We usually seldom touch them. These parameters are very useful for MySQL performance tuning and fault diagnosis.

Cache queries in MySQL include two resolution query plans and returned datasets. If the basic table data or structure changes, the items in the query cache will be invalid.

1. query_cache_min_res_unit

In the MySQL parameter, the query_cache_min_res_unit query cache blocks are allocated in this size. Use the following formula to calculate the average size of the query cache, and set this variable according to the calculation result, mySQL uses the query cache more effectively, caches more queries, and reduces memory waste.

2. query_cache_size

This parameter sets the total size of the query cache.

3. query_cache_limit

This parameter tells MySQL to discard a query larger than this size. Generally, large queries are rare, such as running a batch to execute statistics on a large report, therefore, those large result sets should not fill the query cache.

CopyCode The Code is as follows: qcache hit ratio = qcache_hits/(qcache_hits + com_select)

UseCopy codeThe Code is as follows: SQL> show status like 'qcache % ';
SQL> show status like 'com _ % ';

Find these variables.Copy codeThe Code is as follows: average query size = (query_cache_size-qcache_free_memory)/qcache_queries_in_cache

use the copy Code Code as follows: SQL> show variables like 'query % ';
qcache _ * status variables you can get with:
SQL> show status like 'qcache %';

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.