1. 首先拿到执行的sql语句,使用myslq自带的explain命令,使用explain 查看执行计划,
explain select * from cc_ci where id = '200000000140094'
或 以json格式显示
explain format= json select * from cc_ci where id = '200000000140094'
{
"query_block" : {
"select_id" : 1 ,
"cost_info" : {
"query_cost" : "1.00"
} ,
"table" : {
"table_name" : "cc_ci" ,
"access_type" : "const" ,
"possible_keys" : [
"PRIMARY"
] ,
"key" : "PRIMARY" ,
"used_key_parts" : [
"ID"
] ,
"key_length" : "8" ,
"ref" : [
"const"
] ,
"rows_examined_per_scan" : 1 ,
"rows_produced_per_join" : 1 ,
"filtered" : "100.00" ,
"cost_info" : {
"read_cost" : "0.00" ,
"eval_cost" : "0.20" ,
"prefix_cost" : "0.00" ,
"data_read_per_join" : "5K"
} ,
"used_columns" : [
"ID" ,
"CI_CODE" ,
"CI_DESC" ,
"CLASS_ID" ,
"SOURCE_ID" ,
"OWNER_ID" ,
"ORG_ID" ,
"SUB_CLASS" ,
"CI_VERSION" ,
"HASH_CODE" ,
"CI_PRIMARY_KEY" ,
"CUSTOM_1" ,
"CUSTOM_2" ,
"CUSTOM_3" ,
"CUSTOM_4" ,
"CUSTOM_5" ,
"CUSTOM_6" ,
"ATTRS_STR" ,
"DOMAIN_ID" ,
"DATA_STATUS" ,
"CREATOR" ,
"MODIFIER" ,
"CREATE_TIME" ,
"MODIFY_TIME"
]
}
}
}
2. 使用profiling详细的列出在每一个步骤消耗的时间,前提是先执行一遍语句。
SET profiling = 1;
SHOW VARIABLES LIKE '%profiling%' ;
show profiles;
show profile block io,cpu for query 3;
3. Optimizer trace是MySQL5.6添加的新功能,可以看到大量的内部查询计划产生的信息, 先打开设置,然后执行一次sql,最后查看information_schema
.OPTIMIZER_TRACE
的内容
SET optimizer_trace= 'enabled=on' ;
SET OPTIMIZER_TRACE_MAX_MEM_SIZE= 1000000;
SET END_MARKERS_IN_JSON= ON;
SET optimizer_trace_limit = 1;
SHOW VARIABLES LIKE '%optimizer_trace%' ;
select * from cc_ci where id = '200000000140094'
SELECT * FROM ` information_schema` . ` OPTIMIZER_TRACE` ;