mysql - 慢查詢日誌、explain詳解
一 開啟慢查詢日誌
show variables like 'slow_query_log';
show variables like 'slow_query%';
show variables like 'long_query_time';
set global slow_query_log = 'ON';
set global slow_query_log_file = 'C:/Users/Desktop';
set global long_query_time = 1;
二 explain命令詳解(待續。。。)
explain 用於檢視執行計劃的資訊。
explain select username from a, b where a.id = b.id;
explain select username from a, b using(id);
table: 表名
type: 連線的型別(效率遞減)
system:從const中查詢的記錄,有且僅有一行滿足條件。或者說查詢的表中只有一條記錄。
const:由主鍵或者唯一鍵作為查詢條件,由於只有一條,查詢的記錄被優化器視為常量
eq-ref:唯一性索引掃描。
eg. (B.MID 為 主鍵, A.MID 可能為普通欄位) select * from A, B where A.MID = B.MID;
ref:非唯一性索引掃描。索引不是主鍵或者唯一索引。
eg. (A表中可能含有多個相同的sId。sId不具有唯一性。) select * from A where A.sId = 10;
fulltext:全文索引,(優先順序高於普通索引)
eg. alter table A add fulltext(username);
select * from A match(username) against('tom', 'selina');
ref_or_null:在ref的基礎上增加對null的檢測
eg. select * from A where A.sId = 10 or sId is null;
coalesce() ifnull() isnull() nullif()
index_merge:索引合併
eg. (username,password都是索引) select * from user where username = 'tom' and password = 'tom1';
unique_subquery
eg. (id是主鍵) select * from A where id in (select id from A where userId = 10);
index_subquery
eg. (username普通索引) select * from A where username in (select username from A where company = "Oracle");
range: 檢索給定範圍的索引
eg. select * from A where A.age > 20;
index: 先讀索引,再讀實際的行,結果還是全表掃描
eg. select count(*) from A;
all: 全表掃描
eg. select * from A;