A summary of MYSQL table optimization methods is comprehensive.

Source: Internet
Author: User

At the same time, the online access volume continues to increase. For servers with 1 GB of memory, the server even crashes every day or gets stuck from time to time. This problem has plagued me for using MySQL for more than half a month. scalable algorithms, therefore, you can usually run with a small amount of memory or store MySQL more for better performance.

After installing mysql, the preparation file should be in the/usr/local/mysql/share/mysql directory. There are several preparation files, including my-huge. cnf my-medium.cnf my-large.cnf my-small.cnf, different traffic of the site and different preparation of the server environment, of course, need to have different preparation files.

In general, my-medium. the configuration file cnf can meet most of our needs. We usually copy the configuration file to/etc/my. cnf only needs to modify this configuration file. You can use mysqladmin variables extended-status-u root-p to see the current parameters. Three configuration parameters are the most important, that is, key_buffer_size, query_cache_size, and table_cache.

Key_buffer_size only applies to the MyISAM table,

Key_buffer_size specifies the size of the index buffer, which determines the index processing speed, especially the index reading speed. Generally, we set the value to 16 M. In fact, the number of sites that are slightly larger is far from enough. By checking the status values Key_read_requests and Key_reads, we can check whether the key_buffer_size setting is reasonable. The ratio of key_reads/key_read_requests should be as low as possible, at least and (the above STATUS values can be obtained using show status like 'key _ read % ). Or if you have installed phpmyadmin, you can see it through the server running status. I recommend you use phpmyadmin to manage mysql. The following status values are all my instance analysis obtained through phpmyadmin:

This server has been running for 20 days

Key _buffer_size-128 M
Key_read_requests-650759289
Key_read-79112

Ratio close to 1: 8000 healthy

Another way to estimate the key_buffer_size is to add up the size of the index space of each table in your website database. Take this server as an example: the number of large table indexes is about 125 MB, which will increase as the table grows.

MySQL provides a query buffer mechanism starting from 4.0.1. Using the Query Buffer, MySQL stores the SELECT statement and query result in the buffer. In the future, the same SELECT statement (case sensitive) will be read directly from the buffer. According to the MySQL user manual, query buffering can achieve a maximum efficiency of 238%.

You can check whether query_cache_size is set properly by adjusting the following parameters.

Qcache inserts
Qcache hits
Qcache lowmem prunes
Qcache free blocks
Qcache total blocks

The value of Qcache_lowmem_prunes is very large, which indicates that the buffer is insufficient frequently. The value of Qcache_hits is very large, which indicates that the query buffer is frequently used. In this case, the value of Qcache_hits is not large, this indicates that your query repetition rate is very low. In this case, using the Query Buffer will affect the efficiency, so you can consider not to use the query buffer. In addition, adding SQL _NO_CACHE to the SELECT statement explicitly indicates that no Query Buffer is used.

Qcache_free_blocks. If this value is very large, it indicates that many fragments in the buffer zone query_cache_type specify whether to use the query buffer.

My settings:

Query_cache_size = 32 M
Query_cache_type = 1

Obtain the following status values:

Qcache queries in cache 12737 indicates the number of items currently cached
Qcache inserts 20649006
Qcache hits 79060095 seems that the repeat query rate is quite high
Qcache lowmem prunes 617913 has so many times that the cache is too low
Qcache not cached 189896
Qcache free memory 18573912
Qcache free blocks 5328 seems to be a little big.
Qcache total blocks 30953

If the memory allows 32 MB, you should add some more

Table_cache specifies the table cache size. When MySQL accesses a table, if there is space in the table buffer, the table is opened and put into it, so that the table content can be accessed more quickly. Check the status values Open_tables and Opened_tables of the peak time to determine whether to increase the value of table_cache. If you find that open_tables is equal to table_cache and opened_tables is growing, you need to increase the value of table_cache (the preceding STATUS values can be obtained using show status like 'open % tables ). Note that you cannot blindly set table_cache to a large value. If it is set too high, the file descriptor may be insufficient, resulting in unstable performance or connection failure.

For machines with 1 GB memory, the recommended value is 128-256.

I set table_cache = 256

The following status is displayed:

Opening tables 256
Opened tables 9046

Although open_tables is already equal to table_cache, opened_tables has a very low value after 20 days of running compared to the server running time. Therefore, increasing the value of table_cache should be of little use. If the preceding value appears after six hours, you need to increase the value of table_cache.

If you do not need to record binary logs, you can disable this function. After you disable it, you cannot recover the data before the problem occurs. You need to manually back up the data, the binary log contains all the statements for updating data. It is used to restore the data to the final state as much as possible when restoring the database. In addition, if you perform Replication, you also need to use binary logs to transmit modifications.

Log_bin specifies the log file. If no file name is provided, MySQL generates its own default file name. MySQL automatically adds a numeric reference after the file name. A new binary file is generated every time the service is started. In addition, you can use log-bin-index to specify the index file, binlog-do-db to specify the database for the record, and binlog-ignore-db to specify a database without record. Note: binlog-do-db and binlog-ignore-db specify only one database at a time and multiple statements are required for multiple databases. In addition, MySQL will change all database names to lowercase letters, and all database names must be in lower case when specifying the database, otherwise it will not work.

To disable this function, you only need to add the # sign before it.

# Log-bin

Enabling the slow query log (slow query log) is very useful for querying problematic traces. It records all long_query_time queries. If needed, you can also record records that do not use indexes. The following is an example of slow log query:

To enable slow query logs, you must set the log_slow_queries, long_query_times, and log-queries-not-using-indexes parameters.

Log_slow_queries specifies the log file. If no file name is provided, MySQL generates its own default file name. Long_query_times specifies the threshold for slow queries. The default value is 10 seconds. Log-queries-not-using-indexes is a parameter introduced after 4.1.0. It indicates that the record does not use an index for queries. The author sets long_query_time = 10

Settings:

Sort_buffer_size = 1 M
Max_connections = 120
Wait_timeout = 120
Back_log = 100
Read_buffer_size = 1 M
Thread_cache = 32
Interactive_timeout = 120
Thread_concurrency = 4

Parameter description:

Back_log

The number of connections that MySQL can have. When the main MySQL thread receives a lot of connection requests in a very short period of time, this works, and then the main thread takes some time (although very short) to check the connection and start a new thread. The back_log value indicates how many requests can be stored in the stack within a short time before MySQL temporarily stops answering new requests. Only if you want to have many connections in a short period of time, you need to increase it. In other words, this value is the size of the listener queue for the incoming TCP/IP connection. Your operating system has its own limit on the queue size. The Unix listen (2) System Call manual page should have more details. Check your OS document to find the maximum value of this variable. Trying to set back_log to be higher than your operating system limit will be invalid.

Max_connections

The maximum number of concurrent connections. If 120 exceeds this value, it will be automatically restored. If a problem occurs, it can be automatically solved.

Thread_cache

No specific instructions are found, but it is useful to create more than 400 threads 20 days after 32, and thousands of threads have been created one day before.

Thread_concurrency

# Set your cpu count x2. For example, if there is only one cpu, then thread_concurrency = 2
# With 2 CPUs, thread_concurrency = 4
Skip-innodb
# Remove innodb support



Certificate -----------------------------------------------------------------------------------------------------------------------------------

Code:

# Example MySQL config file for medium systems.
#
# This is for a system with little memory (32 M-64 M) where MySQL plays
# An important part, or systems up to 128 M where MySQL is used together
# Other programs (such as a web server)
#
# You can copy this file
#/Etc/my. cnf to set global options,
# Mysql-data-dir/my. cnf to set server-specific options (in this
# Installation this directory is/var/lib/mysql) or
#~ /. My. cnf to set user-specific options.
#
# In this file, you can use all long options that a program supports.
# If you want to know which options a program supports, run the program
# With the "-- help" option.


# The following options will be passed to all MySQL clients
[Client]
# Password = your_password
Port = 3306
Socket =/tmp/mysql. sock
# Socket =/var/lib/mysql. sock
# Here follows entries for some specific programs


# The MySQL server
[Mysqld]
Port = 3306
Socket =/tmp/mysql. sock
# Socket =/var/lib/mysql. sock
Skip-locking
Key_buffer = 128 M
Max_allowed_packet = 1 M
Table_cache = 256
Sort_buffer_size = 1 M
Net_buffer_length = 16 K
Myisam_sort_buffer_size = 1 M
Max_connections = 120
# Addnew config
Wait_timeout = 120
Back_log = 100
Read_buffer_size = 1 M
Thread_cache = 32
Skip-innodb
Skip-bdb
Skip-name-resolve
Join_buffer_size = 512 k
Query_cache_size = 32 M
Interactive_timeout = 120
Long_query_time = 10
Log_slow_queries =/usr/local/mysql4/logs/slow_query.log
Query_cache_type = 1
# Try number of CPU's * 2 for thread_concurrency
Thread_concurrency = 4


# End new config
# Don't listen on a TCP/IP port at all. This can be a security enhancement,
# If all processes that need to connect to mysqld run on the same host.
# All interaction with mysqld must be made via Unix sockets or named pipes.
# Note that using this option without enabling named pipes on Windows
# (Via the "enable-named-pipe" option) will render mysqld useless!
#
# Skip-networking


# Replication Master Server (default)
# Binary logging is required for replication
# Log-bin


# Required unique id between 1 and 2 ^ 32-1
# Defaults to 1 if master-host is not set
# But will not function as a master if omitted
Server-id = 1


# Replication Slave (comment out master section to use this)
#
# To configure this host as a replication slave, you can choose
# Two methods:
#
#1) Use the change master to command (fully described in our manual )-
# The syntax is:
#
# Change master to MASTER_HOST =, MASTER_PORT =,
# MASTER_USER =, MASTER_PASSWORD =;
#
# Where you replace, by quoted strings and
# By the master's port number (3306 by default ).
#
# Example:
#
# Change master to MASTER_HOST = '192. 564.12.1 ', MASTER_PORT = 125,
# MASTER_USER = 'job', MASTER_PASSWORD = 'secret ';
#
# OR
#
#2) Set the variables below. However, in case you choose this method, then
# Start replication for the first time (even unsuccessfully, for example
# If you mistyped the password in master-password and the slave fails
# Connect), the slave will create a master.info file, and any later
# Change in this file to the variables 'values below will be ignored and
# Overridden by the content of the master.info file, unless you shutdown
# The slave server, delete master.info and restart the slaver server.
# For that reason, you may want to leave the lines below untouched
# (Commented) and instead use change master to (see above)
#
# Required unique id between 2 and 2 ^ 32-1
# (And different from the master)
# Defaults to 2 if master-host is set
# But will not function as a slave if omitted
# Server-id = 2
#
# The replication master for this slave-required
# Master-host =
#
# The username the slave will use for authentication when connecting
# To the master-required
# Master-user =
#
# The password the slave will authenticate with when connecting
# The master-required
# Master-password =
#
# The port the master is listening on.
# Optional-defaults to 3306
# Master-port =
#
# Binary logging-not required for slaves, but recommended
# Log-bin


# Point the following paths to different dedicated disks
# Tmpdir =/tmp/
# Log-update =/path-to-dedicated-directory/hostname


# Uncomment the following if you are using BDB tables
# Bdb_cache_size = 4 M
# Bdb_max_locked = 10000


# Uncomment the following if you are using InnoDB tables
# Innodb_data_home_dir =/var/lib/mysql/
# Innodb_data_file_path = ibdata1: 10 M: autoextend
# Innodb_log_group_home_dir =/var/lib/mysql/
# Innodb_log_arch_dir =/var/lib/mysql/
# You can set .. _ buffer_pool_size up to 50-80%
# Of RAM but beware of setting memory usage too high
# Innodb_buffer_pool_size = 16 M
# Innodb_additional_mem_pool_size = 2 M
# Set .. _ log_file_size to 25% of buffer pool size
# Innodb_log_file_size = 5 M
# Innodb_log_buffer_size = 8 M
# Innodb_flush_log_at_trx_commit = 1
# Innodb_lock_wait_timeout = 50


[Mysqldump]
Quick
Max_allowed_packet = 16 M


[Mysql]
No-auto-rehash
# Remove the next comment character if you are not familiar with SQL
# Safe-updates


[Isamchk]
Key_buffer = 20 M
Sort_buffer_size = 20 M
Read_buffer = 2 M
Write_buffer = 2 M


[Myisamchk]
Key_buffer = 20 M
Sort_buffer_size = 20 M
Read_buffer = 2 M
Write_buffer = 2 M


[Mysqlhotcopy]
Interactive-timeout

----------------------------------------------------------------------------------

Supplement

Optimize table_cachetable_cache to specify the table cache size. When MySQL accesses a table, if there is space in the table buffer, the table is opened and put into it, so that the table content can be accessed more quickly. Check the status values Open_tables and Opened_tables of the peak time to determine whether to increase the value of table_cache. If you find that open_tables is equal to table_cache and opened_tables is growing, you need to increase the value of table_cache (the preceding STATUS values can be obtained using show status like 'open % tables ). Note that you cannot blindly set table_cache to a large value. If it is set too high, the file descriptor may be insufficient, resulting in unstable performance or connection failure. For machines with 1 GB memory, the recommended value is 128-256.

Case 1: This case comes from a non-particularly busy server table_cache-512open_tables-103opened_tables-1273 uptime-4021421 (measured in seconds). in this case, table_cache seems to be too high. During the peak time, the number of opened tables is much less than that of table_cache.

Case 2: A Development Server. Table_cache-64open_tables-64opened-tables-431 uptime-1662790 (measured in seconds) Although open_tables is already equal to table_cache, opened_tables has a very low value compared to the server running time. Therefore, increasing the value of table_cache should be of little use. Case 3: In this case, the table_cache-64open_tables-64opened_tables-22423 uptime-19538 of a upderconfiguring server is set too low. Although the running time is less than 6 hours, open_tables reaches the maximum value, and opened_tables has a very high value. In this way, you need to increase the value of table_cache. Optimize key_buffer_sizekey_buffer_size to specify the index buffer size, which determines the index processing speed, especially the index reading speed. Check the status values Key_read_requests and Key_reads to check whether the key_buffer_size setting is reasonable. The ratio of key_reads/key_read_requests should be as low as possible, at least and (the above STATUS values can be obtained using show status like 'key _ read % ). Key_buffer_size only applies to the MyISAM table. This value is used even if you do not use the MyISAM table, but the internal temporary disk table is a MyISAM table. You can use the check status value created_tmp_disk_tables to learn the details. For machines with 1 GB memory, if the MyISAM table is not used, the recommended value is 16 M (8-64 M ).

Case 1: Health Condition key_buffer_size-402649088 (384 M) key_read_requests-597579931key_reads-56188 Case 2: alarm status key_buffer_size-16777216 (16 M) the ratio of key_read_requests-597579931key_reads-53832731 in Case 1 is less than, which is healthy. the ratio in Case 2 reaches, and the alarm has been triggered.

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: info-contact@alibabacloud.com 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.