In MySQL 5.5, problems encountered during a test and optimization process and solutions to the problems are provided. For your reference. I. Table Structure Optimization Problem 1. Using different character sets for tables and fields will cause index failure in my own
In MySQL 5.5, problems encountered during a test and optimization process and solutions to the problems are provided. For your reference. I. Table Structure Optimization Problem 1. Using different character sets for tables and fields will cause index failure in my own
MySQL 5.5
The problems encountered during a test and optimization and the solutions to the problems. For your reference.
I. Table Structure Optimization
Problem 1: using different character sets for tables and fields will cause index failure.
I did not try it out on my own machine. It may be related to the small number of records. Sometimes, the number of records is too small, and the result is
Some inconsistencies. However, this problem exists.
Problem 2: The table field is too long, leading to index failure in order.
Mysql> show create table t2 \ G
* *************************** 1. row ***************************
Table: t2
Create Table: create table 't2 '(
'Id' int (11) not null,
'Name' varchar (2000) default null,
Primary key ('id '),
KEY 'idx _ name' ('name' (767 ))
) ENGINE = InnoDB default charset = latin1
1 row in set (0.00 sec)
Mysql> explain select * from t2 order by name \ G
* *************************** 1. row ***************************
Id: 1
Select_type: SIMPLE
Table: t2
Type: ALL
Possible_keys: NULL
Key: NULL
Key_len: NULL
Ref: NULL
Rows: 1
Extra: Using filesort
1 row in set (0.00 sec)
After modification:
Mysql> show create table t2 \ G
* *************************** 1. row ***************************
Table: t2
Create Table: create table 't2 '(
'Id' int (11) not null,
'Name' varchar (100) default null,
Primary key ('id '),
KEY 'idx _ name' ('name ')
) ENGINE = InnoDB default charset = latin1
1 row in set (0.00 sec)
Mysql> explain select * from t2 order by name \ G
* *************************** 1. row ***************************
Id: 1
Select_type: SIMPLE
Table: t2
Type: index
Possible_keys: NULL
Key: idx_name
Key_len: 103
Ref: NULL
Rows: 1
Extra: Using index
1 row in set (0.00 sec)
Problem 3: The number of characters recorded in a row in the table is too long, and the sorting is abnormal.
When the number of records in a table row exceeds 1 K, max_length_for_sort_data defaults, using order by will cause sorting
Abnormal slow. If such a data table exists, set max_length_for_sort_data to 2 K or higher.
In this case, you can use explain to find the cause of the error. When this happens, you will find the explicit
Contains the following content:
Extra: Using filesort
Problem 4: The MyISAM engine is used incorrectly, and the query speed is abnormal and slow.
Because the test project only uses the Innodb engine, the configuration related to the MyISAM engine is disabled in the configuration file.
When importing data, the query is slow due to the incorrect use of the Innodb engine.
At the beginning, I didn't know why I used the MyISAM engine. I quickly found
Slow query statement. Then, the error table is found through show create table t_corpcustomize.
For slow query, you can view it through show full processlist. You can see query statements similar to the following:
| Id | User | Host | db | Command | Time | State | Info
| 67 | root | linux70: 55737 | bi | Execute | 0 | Sending data | select whoset, specialphone,
Settype, begintime, endtime from t_corpcustomize where status = 1
Ii. Server Parameter Optimization Configuration
1. Remove all unused features
1. Because query cache is useless, remove the configuration as follows:
Query_cache_type = 0
Query_cache_size = 0
2. Because MyISAM is useless, remove the configuration as follows:
Key_buffer_size = 8 M
Comment out all others
2. Stop all MySQL-related services
During the test, we found that oracle, java, and tomcat processes were running on the MySQL machine.
Although they do not have business, they use a lot of memory. After removal, the performance has been improved.
3. Reasonable Parameter Adjustment
1. Adjust Memory-related configurations
Innodb_file_per_table = true
# Data files must be able to hold your data and indexes
# Set buffer pool size to 50-80% of your computer's memory,
# But make sure on Linux x86 total memory usage is <2 GB
Innodb_buffer_pool_size = 5G
# When innodb_buffer_pool_size> 1G, set this parameter to pide the buffer pool size maybe just
Above 1G
Innodb_buffer_pool_instances = 4
Innodb_additional_mem_pool_size = 20 M
# Set the log file size to AB out 25% of the buffer pool size
Innodb_log_file_size = 2047 M
Innodb_log_files_in_group = 2
Innodb_log_buffer_size = 8 M
Max_connection
*
(
# Used for sequential read, generally not too large
Read_buffer_size = 4 M
# It is used for random read. When there are a large number of order by and group by operations, it is increased.
Read_rnd_buffer_size = 32 M
# Used for sorting. When there are a large number of order by and group by operations, increase
Sort_buffer_size = 64 M
# Generally, the join operation is not too large.
Join_buffer_size = 8 M
# Generally, it does not need to be too large. The default value is enough.
Thread_stack = 192 K
# Configuration when binlog is used, which is usually the default value.
Binlog_cache_size
)
Ii. Adjust configurations related to I/O, file, log cache, etc.
1. Adjust table_open_cache
In general, you can view the values of Open_tables and Opened_tables in phpmyadmin, or execute
1 mysql> show global status like 'open % _ tables ';
View the current open_tables status
To view the values of these two parameters. Open_tables indicates the number of tables currently being opened, and Opened_tables indicates all
The number of opened tables.
If the value of Open_tables is close to that of table_cache, and Opened_tables continues to grow, it indicates
Mysql is releasing the cached table to accommodate the new table. In this case, you may need to increase the value of table_cache. For most
Situation,
Applicable values:
Open_tables/Opened_tables> = 0.85
Open_tables/table_cache <= 0.95
2. I/O Adjustment
# The following two parameters are generally set to the number of CPUs:
Innodb_write_io_threads = 4
Innodb_read_io_threads = 4
# Set the number of CPUs as below * 2
Innodb_thread_concurrency = 8
# Io capacity. This parameter needs to be set based on IOPS. You need to check the hardware manual.
3. log parameter adjustment
# Log flush mode. For details, refer to the reference documentation. It is usually set to O_DIRECT in linux,
Html # sysvar_innodb_flush_log_at_trx_commit
Innodb_flush_method = O_DIRECT
#1 = Ensure transaction consistency. Each time the commit is refreshed to the disk, 0 = cached, the disk is refreshed every second, and 2 = the file is written,
Refresh to the disk every second. The recommended value is 2. When the value is set to 2, the performance will be improved a lot, but it will only happen when the power is down.
Service loss.
Innodb_flush_log_at_trx_commit = 1
3. Adjust the isolation level
Because the default isolation level of oracle is READ-COMMITTED, and mySQL is REPEAT-READ by default. Different
The isolation level varies greatly with performance. Because of its strict degree of concurrency control, in other words, low
Isolation level, requiring applications to do more work to ensure consistency, and high is ensured by the database itself. READCOMMITTED
In fact, there is not much impact, but it will only cause phantom data problems. MySQL READCOMMITTED
The security configuration is as follows.
Transaction_isolation = READ-COMMITTED
Innodb_autoinc_lock_mode = 2
Innodb_locks_unsafe_for_binlog = 1
Innodb_support_xa = 0
4. Store Innodb data, tablespace, and log files on different Disks
After testing, I found that the bottleneck of the system is I/O. The iostat operation shows the io r/s per second during peak hours.
Read 65, w/s write 437.67.
The current position of the system performance cannot be improved because MySQL is always waiting for I/O operations.
Evice: rrqm/s wrqm/s r/s w/s RMB/s wMB/s avgrq-sz avgqu-sz await svctm % util
Sda 0.00 7.00 4.33 4.33 0.16 0.04 48.92 0.16 18.00 4.00 3.47
Sdb 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Sdc 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Sdd 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Sde 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Sdf 0.67 158.67 65.00 437.67 0.68 9.80 42.73 2.11 4.21 0.55 27.73
Sdg 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
VxVM56000 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Dm-0 0.00 0.00 65.67 589.00 0.68 9.80 32.81 3.56 5.44 0.43 27.87
Therefore, we recommend that you divide data file, innodb space file, and innodb log file into three different disks to check whether the performance is improved.
.
Iii. common troubleshooting methods
I. mysql> explain
For details, see the following documents:
Mysql> explain select * FROM students WHERE or sex = 1 \ G
* *************************** 1. row ***************************
Id: 1
Select_type: SIMPLE
Table: students
Type: ALL
Possible_keys: index_name
Key: NULL
Key_len: NULL
Ref: NULL
Rows: 4
Extra: Using where
1 row in set (0.00 sec)
2. mysql> show full processlist
See chapter 1, question 4
3. slow log
1. The configuration is as follows:
Slow_query_log = 1
Slow_query_log_file =/opt/upora/slow. log
# If the SQL write time exceeds long_query_time, the time is in seconds.
Long_query_time = 1
# Writing logs using SQL statements without Indexes
Log-queries-not-using-indexes
2. The content of slow log is as follows:
For example:
Use cust;
SET timestamp = 1356921142;
Select * from cust_contact ct where ct. custid in
(Select c. custid from customer c, cust_group g, (select groupid from groups_defin
E where) gr where c. custid = g. custid and g. groupid = gr. groupid );
3. Use mysqldumpslow to collect statistics
For example:
# Mysqldumpslow slow. log
Count: 1 Time = 1.06 s (1 s) Lock = 0.02 s (0 s) Rows = 7.0 (7), root [root] @ localhost
Select count (*), engine, data_length from information_schema.tables group by engine
4. mysql> show global status
This command produces a large amount of data, which is not intuitive and requires computation before reaching a conclusion.
Some commonly used computing options are listed below.
(1) QPS (queries per second)
QPS = Questions (or Queries)/seconds
Mysql> show global status like 'Question % ';
(2) TPS (transaction volume per second)
TPS = (Com_commit + Com_rollback)/seconds
Mysql> show global status like 'com _ commit ';
Mysql> show global status like 'com _ rollback ';
(3) key Buffer hit rate
Mysql> show global status like 'key % ';
Key_buffer_read_hits = (1-key_reads/key_read_requests) * 100%
Key_buffer_write_hits = (1-key_writes/key_write_requests) * 100%
(4) InnoDB Buffer hit rate
Mysql> show status like 'innodb _ buffer_pool_read % ';
Innodb_buffer_read_hits = (1-innodb_buffer_pool_reads/innodb_buffer_pool_read_requests )*
100%
(5) Query Cache hit rate
Mysql> show status like 'qcache % ';
Query_cache_hits = (Qcahce_hits/(Qcache_hits + Qcache_inserts) * 100%;
(6) Number of Table Cache statuses
Mysql> show global status like 'open % ';
Compare open_tables and opend_tables values
(7) Thread Cache hit rate
Mysql> show global status like 'thread % ';
Mysql> show global status like 'connections ';
Thread_cache_hits = (1-Threads_created/connections) * 100%
(8) Lock status
Mysql> show global status like '% lock % ';
Table_locks_waited/Table_locks_immediate = 0.3%
Severe Blocking
Innodb_row_lock_waits innodb row lock, which is too large and may be caused by a gap lock
V. mysql> show engine innodb status
This command can collect innodb information and locate problems such as deadlocks.
For details, see:
Vi. iostat, top, vmstat
This can be used to view cpu and memory usage. This is also a way to find performance bottlenecks for all databases. Here
No more
This article is from the "MySQL beginner to expert path" blog, please be sure to keep this source