Explain the Index Tuning and slow query log

 

Index: essentially a data structure that is simple to understand: sorted quickly find data structure, in the form of index files are stored on disk.
Objective: To improve the efficiency of data queries, query performance optimization, just like the book directory.
Advantage: improve search efficiency, reduce IO costs; sorted table, reduce the consumption of CPU's
disadvantage: the index actually is a table that holds the primary key and index fields, and point to record the entity table space; reduce update table speed (changed data table itself, also need to modify the index); take the time to study the establishment of the best index.

 Index optimization analysis includes several parts, in which Explain to learn more about keywords.

Explain Keyword: Explain the use of keywords can simulate SQL query optimizer to perform, so they know how to deal with your MySQL SQL statements, analyze the performance bottleneck in your query or table structure.

Why can Explain: reading order table, the data reading operation of the operation type, which indexes can be used, which indexes are actually used, the references between tables, each row is the number of query optimizer.

Explain + information on SQL statement execution plan contains: id | select_type | table | patitions | type | possible_keys | key | key_len | ref | rows | filtered | extra |

 

 Second, query interception analysis includes five parts: query optimization, slow query log, batch data script, show profile, global query logs and other information, this is mainly to understand the two parts.

1, the query optimizer : always small table-driven large table; Order by; Group by

2, slow query log

Chart which includes what is, how to play, log analysis tools, from left to right to view it.

 

 

 

Slow query log: Mysql provides a journaling record, running for more than long_querry_time worth SQL, it will be recorded to the slow query log. long_querry_time default equal to 10s.

Slow log specific operation code as follows:

慢日志查询
SHOW VARIABLES LIKE 'long_query_time%';#查看慢查询的时间阈值,默认显示为10S

SHOW VARIABLES LIKE '%slow_query_log%';#查看是否开启

set global slow_query_log=1; #设置为1,开启慢查询日志

set global long_query_time=0.001;#设置慢查询的时间为0.001,超过0.001就显示为慢查询

select * from sleep(1); #睡眠1s

show global status like '%Slow_queries';#查询慢查询的条数,需要优化的条数

mysqldumpslow -s r -t /var/lib/mysql/RMT01-slow.log;#路径就是查询是否开启中show_query_log_file的文件路径

 

 

 

Guess you like

Origin www.cnblogs.com/wendyw/p/11591983.html