mysql中如何查看sql语句是否⽤到索引
1、操作步骤
1.1 使⽤explain ,放在sql前⾯
2、解释
我们只需要注意⼀个最重要的type 的信息很明显的提现是否⽤到索引:
type结果值从好到坏依次是:
system > const > eq_ref > ref > fulltext > ref_or_null > index_merge > unique_subquery > index_subquery > range > index > ALL
⼀般来说,得保证查询⾄少达到range级别,最好能达到ref,否则就可能会出现性能问题。
possible_keys:sql所⽤到的索引
key:显⽰MySQL实际决定使⽤的键(索引)。如果没有选择索引,键是NULL
rows: 显⽰MySQL认为它执⾏查询时必须检查的⾏数
3、profiling分析
想要优化⼀条query sql ,就要清楚这条query的性能瓶颈在哪⾥,mysql的profiler是⼀个⾮常⽅便的查询诊断分析⼯具,通过该⼯具可以获取⼀条查询在整个执⾏过程中多种资源的消耗情况,例如内存消耗、I/O消耗和CPU消耗
profile的语法结构:
show profile [type [,type] …]
[for query n]
[limit row_count [offset offset]]
其中type参数可选含义如下:
all:显⽰所有信息
block io:显⽰输⼊输出操作阻塞的数量
context switches:显⽰⾃动或⾮⾃动context switches的数量
cpu:显⽰系统和⽤户CPU使⽤的时间
ipc:显⽰信息发送和接受的数量
memory:内存的信息
page faults:显⽰主要的page faults数量
查看mysql索引source:显⽰函数的名称,并且是那些函数所在⽂件的名字和⾏数
swaps:显⽰swap数量
开启profile
set profiling = 1;
开启query profiler功能之后,MySQL就会⾃动记录所有执⾏的query的profile信息
select count(*) from customers1;
通过执⾏show profiles 命令获取当前系统中保存的多个query的profile的概要信息
针对单个query获取详细的profile信息(根据概要信息中的query_id来获取)show profile for query 5;
转载:blog.csdn/weixin_45310179/article/details/99591496