MySQL system optimization and problem search bitsCN.com
MySQL system optimization and problem search
Performance Optimization Related status parameters
Show status like 'value ';
Connections
Uptime start time
Show_queries slow query count
Com_select query operation count
Com_insert insert operation count
Com_update update operation count
Com_delete delete operation count
Analysis query statement
EXPLAIN/desc select;
Disable/enable index
Alter table table DISABLE/enable keys;
Disable Unique Indexes
SET UNIQUE_CHECK = 0/1
Analyze, check, and optimize tables
Analyze table table1 [, table2...]
Check table table1 [, table2...]
Optimize table table1 [, table2. ..]
Analyze SQL statements
Explain select count (*), max (id), min (id) from user/G
Analyze the SQL statement through explain to learn the execution status.
Use of Profiling
Open profiling parameters
> Set profiling = 1;
After executing some SQL statements, you can view the profile information of the query.
> Show profiles;
> Show profiles cpu, blockio for query 6;
# View the cpu I/O resources used by query 6
Restrictions in indexes
1. The total length of the MyISAM engine index cannot exceed 1000 bytes.
2. you can only create prefix indexes for BLOB and TEXT columns.
3. MySql does not support function indexing.
4. use not equal (! = Or <> ).
5. indexes cannot be used after a filter field uses a function (for example, abs (column ).
6. indexes cannot be used when the Join condition fields in the Join statement have different types.
7. when using the Like operation, if the condition starts with a wildcard ('% abc...'), the index cannot be used.
8. hash indexes cannot be used when non-equivalent queries are used.
Query efficiency test tool mysqlslap
$ Mysqlslap -- create-schema = example -- query = "select * from group_message where user_id = 3 AND subject like 'weiurezs % '-- iterations = 10000
# It is used to test the query execution efficiency and provides average, maximum, and minimum execution time.
Force index (INDEX name) enforces the use of indexes
EXPLAIN select * from group_message
Force index (idx_group_message_author_subject)
Where user_id = 3 AND author = '3' AND subject like 'weiurazs % '/g
Performance Tuning-log settings
Mysql log items include error log, update log, binary log, query log, and slow query log.
Binlog
> Show variables like '% binlog % ';
Slow query
> Show variables ables like 'log _ slow % ';
> Show variables like 'Long _ query % ';
The minimum value of long_query_time is 1 second. to further reduce the time limit for slow queries, you can use the microslow-path (http://www.mysqlperformanceblog.com/2008/04/20/updated-msl-microslow-path-installation-walk-through/) provided by Percona)
Performance Optimization-Query Cache
View Query Cache system variables
> Show variables like '% query_cache % ';
Query Cache usage
> Show status like 'qcache % ';
Cache hit rate = Qcache_hits/(Qcache_hits + Qcache_inserts)
Qcache_hits/(Qcache_hits + Com_select) should be more accurate
Disadvantages: 1. hash operations and search resources of Query statements increase CPU resource consumption
2. Query Cache failure (high failure rate may occur when the table is updated frequently
3. the Result Set cached in the Query Cache, instead of the page, may cause excessive memory consumption and a decrease in the hit rate caused by too many swap-in and swap-out due to insufficient memory.
Countermeasures:
1. specify the SQL Hint of SQL _NO_CACHE for frequently updated records, and force MySQL not to cache.
2. specify SQL _CACHE for most static data and use the CACHE.
3. for those queries with large Result sets, either use SQL _NO_CACHE to force no CACHE, or Set the query_cache_limit parameter to control the maximum Result Set in the Query. the default value is 1 MB, result Set greater than this value will not be cached.
Query Cache restrictions
1. versions earlier than 5.1.17 cannot Cache variable Query. However, since 5.1.17, Query Cache has started to support variable Query;
2. external query SQL statements in all subqueries cannot be cached;
3. the Query in Procedure, Function, and Trigger cannot be cached;
4. the Query that contains many other functions that may obtain different results each time cannot be cached.
Performance Tuning-other common optimizations
Max_connections (maximum number of connections): generally set to around-
Max_user_connections (maximum number of connections allowed by each user): generally, no limit is imposed.
Net_buffer_length (network transmission cache): By default, 16 KB is sufficient.
Thread_cache_size (number of connection threads that the Thread Cache pool should store): it should not be smaller than the actual number of concurrent requests sent to the database by the application system, generally between 50. It works well for short connections.
Related system settings and status values
> Show variables like 'thread % ';
> Show status like 'connections ';
> Show status like '% thread % ';
Thread Cache hit rate: (It should be above 90%)
Thread_Cache_hit = (Connections-Threads_created)/Connections * 100%;
MyISAM engine optimization
Application scenarios of the MyISAM engine: a read-based non-transactional data system with excellent performance when the data accuracy requirements are not high.
System parameters:
Key_buffer_size: index cache size
Key_buffer_block_size: Cache Block Size in the index Cache:
Key_cache_pision_limit: the demarcation between Hot Area and Warm Area in the LRU linked list (range: 1-100). The default value is 100, and only Warm Cache is available.
Key_cache_aeg_threshold: Controls the restriction that the Cache Block is reduced from Hot Area to Warm Area.
Performance parameters:
Key_block_not_flushed has been changed but has not been refreshed to the Dirty Cache Block on the disk.
Key_blocks_unused number of Cache blocks not used currently
Total number of times the key_read_requests Cache Block is requested to be read
Key_read: the number of times the Key information to be read is not found in the Cache Block and then read to the. MYI file
Key_write_requests: the total number of times the Cache Block is requested to be modified
Key_writes: the number of times the Key information to be modified cannot be found in the Cache Block and then read to the. MYI file.
Parameter rationality judgment indicator:
Key_buffer_UsageRatio = (1-Key_blocks_used/(key_blocks_used + key_blocks_unused) * 100%
(It should be above 99%. if the value is too low, it indicates that key_buffer_size is too large, and MySQL cannot be used up)
Key_buffer_read_HitRatio = (1-Key_reads/key_read_requests) * 100%
(The value should be greater than 99%. if the value is too low, it indicates that the key_buffer_size is too small and needs to be increased. it may also be because key_cache_age_threshold and key_cache_pision_limit are improperly set, resulting in. In general, in actual application scenarios, the values of key_cache_age_threshold and key_cache_pision_limit parameters are rarely adjusted. most of them use the default values of the system)
Key_buffer_write_HitRatio = (1-Key_writes/key_Write_requests) * 100%
Multi-Cache system
MySQL officially recommends that you set up three Key caches on a busy system:
A Hot Cache uses 20% of the size to store indexes of tables that are frequently used and rarely updated;
A Cold Cache uses 20% of the size to store indexes of frequently updated tables;
A Warm Cache uses 60% of the remaining space as the default Key Cache for the entire system;
Key Cache Mutex: currently, MySQL is prone to Cache Block locks when the number of Active threads is high.
Key Cache pre-loading
In MySQL, in order to ensure that the system does not have any data in the Cache after startup, the short-term load is too high or
It is a problem that the response is not timely enough. MySQL provides the Key Cache pre-loading function. you can use the relevant commands (load index into cache tb_name_list ...), load all indexes of the specified table to the memory. you can also use relevant parameters to determine whether to Load only the root node and the branch node, or Load all the page nodes, this is mainly used to consider the capacity of the Key Cache.
You can use the init_file parameter of MySQL to set related commands for loading operations immediately after startup, as shown below:
Mysql @ sky :~ $ Cat/usr/local/mysql/etc/init. SQL
Set global hot_cache.key_buffer_size = 16777216
Set global cold_cache.key_buffer_size = 16777216
Cache index example. top_message in hot_cache
Cache index example. event in cold_cache
Load index into cache example. top_message, example. event IGNORE LEAVES
Here, in my init file, we first set two Key caches (hot Cache and cold cache) to 16 MB each, and then cache the indexes of the tables with few changes in top_message to Hot Cache respectively, then, Cache the INDEX of the table with frequent event changes to the Cold Cache, and LOAD the top_message through the load index into cache command, the data of all the nodes and non-leaf nodes indexed by the event and user tables in the groups table is sent to the Key Cache to improve the response capability at the beginning of the system startup.
Other optimizations
1. run the OPTIMIZE command to sort out files in the MyISAM table. This is like we use the Windows operating system to perform disk fragmentation every time after a period of time, so that the files in the system can use continuous space as much as possible to improve the file access speed. MyISAM uses OPTIMIZE to OPTIMIZE and organize the entire file. it also cleans up the fragments caused by data deletion and update. Generally, an OPTIMIZE operation is required after a large data deletion operation. In addition, there should be one OPTIMIZE maintenance operation every quarter.
2. setting myisam_max _ [extra] _ sort_file_size is large enough, which may greatly improve the repair table efficiency.
3. you can adjust the value of the myisam_sort_buffer_size parameter at the session level before executing create index or repair table that requires large sorting operations to improve the efficiency of sorting operations.
4. enable the delay_key_write function to reduce IO synchronization operations and improve write performance.
5. adjust bulk_insert_buffer_size to improve the overall performance of the INSERT... SELECT... bulk insert operation. The performance of load data infile... can also be improved. Of course, when Setting this parameter, we should not blindly pursue a great deal. in many cases, the extreme pursuit of transition will affect the overall system performance. after all, the system performance is from the overall perspective, instead of simply targeting one or more types of operations.
Optimization of MySql MyISAM and INNODB table fragments
Optimize table table_name SQL statements are used to clear fragments for MyISAM TABLE types.
The Clustered Index used by InnoDB is bound together with the Index and data, which is unrealistic. therefore, the MyISAM OPTIMIZE is not supported, but bound to the alter table command. you can execute the following statement to sort the shards and increase the index speed:
Alter table table_name ENGINE = Innodb;
This is actually a NULL operation. on the surface, nothing is done. In fact, fragments are reorganized. during the optimization operation, an empty ALTER command is actually executed, but this command also plays an optimization role. it will recreate the entire table and delete unused blank space.
Innodb storage engine optimization
There are four main differences between the Innodb storage engine and the MyISAM storage engine. The first is the cache mechanism, the second is transaction support, the third is lock implementation, and the last is the difference in data storage methods. In terms of overall performance, the two storage engines Innodb and MyISAM differ significantly in different scenarios, mainly because of the four main differences above.
Innodb_buffer_pool_size
Assume that a host is used separately for MySQL. The total physical memory size is 8 GB, the maximum number of connections to MySQL is 500, and the MyISAM storage engine is also used, how should we allocate the overall memory?
The memory is allocated to the following parts:
1. used by the system. assume that 800 M is reserved;
2. exclusive thread, about 2 GB = 500*(1 MB + 1 MB + 1 MB + kB). The composition is roughly as follows:
Sort_buffer_size: 1 MB
Join_buffer_size: 1 MB
Read_buffer_size: 1 MB
Read_rnd_buffer_size: 512KB
Thread_statck: 512KB
3. MyISAM Key Cache, which is assumed to be approximately 1.5 GB;
4. maximum Innodb Buffer Pool: 8 GB-800 MB-2 GB-1.5 GB = 3.7 GB
The real-time status information of the Buffer Pool is used to determine whether the InnoDB Buffer Pool is used efficiently:
> Show status like 'innodb _ buffer_pool _ % ';
Innodb_Buffer_pool_HitRatio = (Innodb_buffer_pool_read_requests-Innodb_buffer_pool_reads)/Innodb_buffer_pool_read_requests * 100%
Buffer pool Usage = Innodb_buffer_pool_pages_data/Innodb_buffer_pool_pages_total * 100%
Use of the innodb_log_buffer_size parameter
As the name suggests, this parameter is used to set the Innodb Log Buffer size. the default value is 1 MB. The main function of Log Buffer is to Buffer Log data and improve the I/O performance of Log writing. Generally, if your system does not have a very high write load and a large transaction volume, the size within 8 MB is enough.
> Show status like 'innodb _ log % '; (check whether innodb_log_buffer_size is set properly)
The physical storage structure of the Innodb storage engine:
Minimum unit: page (16 KB by default) --> extent (64 consecutive page) --> segment (one or more extent) --> tablespace (maximum physical structure unit, consists of multiple segments)
InnoDB performance monitoring
> Show innodb status/G
How to continuously obtain status information: create table innodb_monitor (a int) engine = innodb;
After an innodb_monitor empty table is created, InnoDB will output information every 15 seconds and record it to the Error Log, and stop monitoring by deleting the table.
In addition, you can enable and disable innodb_tablespace_monitor, innodb_lock_monitor, and innodb_table_monitor in the same way.
MySQL high availability solution
1. MySQL Replication
2. MySQL Cluster
3. DRDB
BitsCN.com