今天學習了mysql的慢sql分析過程,作一下mark。mysql
查詢慢sql的配置:sql
mysql> show variables like '%query%'; +------------------------------+-------------------------------+ | Variable_name | Value | +------------------------------+-------------------------------+ | binlog_rows_query_log_events | OFF | | ft_query_expansion_limit | 20 | | have_query_cache | YES | | long_query_time | 10.000000 | | query_alloc_block_size | 8192 | | query_cache_limit | 33554432 | | query_cache_min_res_unit | 4096 | | query_cache_size | 268435456 | | query_cache_type | OFF | | query_cache_wlock_invalidate | OFF | | query_prealloc_size | 8192 | | slow_query_log | ON | | slow_query_log_file | /var/log/mysql/query_slow.log | +------------------------------+-------------------------------+
qcatch的參數:學習
mysql> SHOW VARIABLES LIKE '%query_cache%'; +------------------------------+-----------+ | Variable_name | Value | +------------------------------+-----------+ | have_query_cache | YES | | query_cache_limit | 33554432 | | query_cache_min_res_unit | 4096 | | query_cache_size | 268435456 | | query_cache_type | OFF | | query_cache_wlock_invalidate | OFF | +------------------------------+-----------+ 6 rows in set (0.00 sec) mysql> SHOW VARIABLES LIKE '%qcache%'; Empty set (0.00 sec) mysql> SHOW STATUS LIKE 'Qcache%'; +-------------------------+------------+ | Variable_name | Value | +-------------------------+------------+ | Qcache_free_blocks | 1 | | Qcache_free_memory | 268417440 | | Qcache_hits | 0 | | Qcache_inserts | 0 | | Qcache_lowmem_prunes | 0 | | Qcache_not_cached | 2211232422 | | Qcache_queries_in_cache | 0 | | Qcache_total_blocks | 1 | +-------------------------+------------+
查看一下,記錄執行的profiling是否是開啓動了,默認是不開啓的優化
mysql> show variables like "%pro%";
spa
+---------------------------+-------+ | Variable_name | Value | +---------------------------+-------+ | have_profiling | YES | | profiling | OFF | | profiling_history_size | 15 | | protocol_version | 10 | | proxy_user | | | slave_compressed_protocol | OFF | | stored_program_cache | 256 | +---------------------------+-------+
開啓profile,操做:code
set profiling = 1;requests
關閉profile,操做:set profiling = 0;it
優化分析,profiles使用:io
mysql> show profiles; +----------+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Query_ID | Duration | Query | +----------+-------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | 1 | 0.00076075 | show variables like "%pro%" | | 2 | 18.95187850 | SELECT fr.id,fr.lendRequest_id,fr.operationType,fr.amount,fr.state,fr.transactionState,fr.payerAccountNumber,fr.payerBankName,fr.payerAccount,fr.repayDay,fr.logId,fr.createTime,log.finished,log.statusCode,lal.id as accountLogId,fr.inAdvanceRepayFlag FROM finance_requests fr LEFT JOIN unionpay_interf | | 3 | 0.01064700 | select * from finance_requests limit 1
查看執行過程:show profile for query 2event
+----------------------+-----------+ | Status | Duration | +----------------------+-----------+ | starting | 0.000161 | | checking permissions | 0.000009 | | checking permissions | 0.000004 | | checking permissions | 0.000007 | | Opening tables | 0.000036 | | init | 0.000073 | | System lock | 0.000015 | | optimizing | 0.000024 | | statistics | 0.000379 | | preparing | 0.000036 | | Sorting result | 0.000007 | | executing | 0.000005 | | Sending data | 0.000024 | | Creating sort index | 25.297675 | | end | 0.000029 | | query end | 0.000009 | | closing tables | 0.000017 | | freeing items | 0.000058 | | logging slow query | 0.000099 | | cleaning up | 0.000094 | +----------------------+-----------+ 20 rows in set, 1 warning (0.00 sec)