通過show status了解SQL執(zhí)行頻率
mysql> show status like 'com_%';
+-----------------------------+-------+
| Variable_name | Value |
+-----------------------------+-------+
| Com_admin_commands | 0 |
| Com_assign_to_keycache | 0 |
| Com_alter_db | 0 |
| Com_alter_db_upgrade | 0 |
| Com_alter_event | 0 |
| Com_alter_function | 0 |
| Com_alter_instance | 0 |
| Com_alter_procedure | 0 |
| Com_alter_server | 0 |
| Com_alter_table | 0 |
| Com_alter_tablespace | 0 |
| Com_alter_user | 0 |
| Com_analyze | 0 |
| Com_begin | 0 |
| Com_binlog | 0 |
| Com_call_procedure | 0 |
| Com_change_db | 1 |
| Com_change_master | 0 |
| Com_change_repl_filter | 0 |
| Com_check | 0 |
| Com_checksum | 0 |
| Com_commit | 0 |
...
Com_xxx表示每個xxx語句執(zhí)行的次數(shù)
參數(shù)說明
- Com_select:執(zhí)行select操作的次數(shù)
- Com_insert:執(zhí)行insert操作的次數(shù)
定位執(zhí)行效率較低的SQL
#在my.cnf中配置
log-slow-queries=/usr/local/mysql/log_slow_queries.log
long_query_time=0.01
-
通過show processlist命令查看的當(dāng)期那MySQL在進(jìn)行的線程
- 線程的狀態(tài)
- 是否鎖表
通過explain 分析低效SQL的執(zhí)行計(jì)劃
本優(yōu)化案例下載地址http://downloads.mysql.com/docs/sakila-db.zip MySQL官方提供的電影出租廳信息管理系統(tǒng)
mysql> explain select sum(amount) from customer a, payment b where 1=1 and a.customer_id = b.customer_id and email='JANE.BENNETT@sakilacustomer.org'\G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: a
partitions: NULL
type: ALL
possible_keys: PRIMARY
key: NULL
key_len: NULL
ref: NULL
rows: 599
filtered: 10.00
Extra: Using where
*************************** 2. row ***************************
id: 1
select_type: SIMPLE
table: b
partitions: NULL
type: ref
possible_keys: idx_fk_customer_id
key: idx_fk_customer_id
key_len: 2
ref: sakila.a.customer_id
rows: 26
filtered: 100.00
Extra: NULL
2 rows in set, 1 warning (0.00 sec)
參數(shù)簡單說明
select_type:select類型
SIMPLE:簡單表挪凑,不使用表連接或者子查詢
PRIMARY:主查詢乘客,即外層查詢
UNION:第二或者后面的查詢語句
SUBQUERY:子查詢中第一個SELECT
table:輸出結(jié)果集的表
type:表示MySQL在表中找到所需行的方式
type=ALL:全表掃描(性能最差)
type=index:索引全掃描局劲,MySQL遍歷整個索引來查詢匹配的行
type=range:索引范圍掃描铭若,常見于<愧膀、<=、>架忌、>=、between等操作符
type=ref:使用非唯一索引掃描或唯一索引的前綴掃描蔬咬,返回匹配某個單獨(dú)值的記錄行
type=eq_ref
type=const/system
type=NULL(性能最好)
案例