explain和profiling分析查询SQL时间

mysql可以通过profiling命令查看到执行查询SQL消耗的时间。

默认情况下,mysql是关闭profiling的,命令:

select @@profiling;
+-------------------+

|    @@profiling    |

+-------------------+

|                     0   |

+-------------------+

说明:

0:表示profiling功能是关闭;

1:表示打开的。

可以通过命令打开/关闭profiling功能。

打开命令:

set profiling=1;
关闭命令:

set profiling=0;
如查询命令:

select * from employee limit 1,10;

可以使用profiling命令查看执行这条SQL消耗的时间:

show profiles;
查询结果:

+----------------+-----------------+-------------------------------------------------------------+

| Query_ID        | Duration         | Query                                                                     |

+----------------+-----------------+--------------------------------------------------------------+

|             1       | 0.00083225      | select * from employee limit 1,10                              |

+----------------+-----------------+--------------------------------------------------------------+

1 row in set ( 0.00 sec)


使用explain来分析是否命中索引

mysql> explain select * from user where username = 'a';
+----+-------------+-------+------+---------------+------------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key        | key_len | ref   | rows | Extra       |
+----+-------------+-------+------+---------------+------------+---------+-------+------+-------------+
|  1 | SIMPLE      | user  | ref  | user_index    | user_index | 62      | const |    1 | Using where |
+----+-------------+-------+------+---------------+------------+---------+-------+------+-------------+
1 row in set (0.00 sec)

可以看出已经命中索引user_index



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值