1. 程式人生 > >explain和profiling分析查詢SQL時間

explain和profiling分析查詢SQL時間

ati 打開 fix -c ret read 索引 -- title

mysql可以通過profiling命令查看到執行查詢SQL消耗的時間。

默認情況下,mysql是關閉profiling的,命令:

[sql] view plain copy
  1. select @@profiling;
+-------------------+

| @@profiling |

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

| 0 |

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

說明:

0:表示profiling功能是關閉;

1:表示打開的。

可以通過命令打開/關閉profiling功能。

打開命令:

[sql] view plain copy
  1. set profiling=1;
關閉命令: [sql] view plain copy
  1. set profiling=0;
如查詢命令:

select * from employee limit 1,10;

可以使用profiling命令查看執行這條SQL消耗的時間:

[sql] view plain copy
  1. show profiles;
查詢結果:

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

| Query_ID | Duration | Query |

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

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

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

1 row in set ( 0.00 sec)

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

[sql] view plain copy
  1. mysql> explain select * from user where username = ‘a‘;
  2. +----+-------------+-------+------+---------------+------------+---------+-------+------+-------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+-------+------+---------------+------------+---------+-------+------+-------------+
  5. | 1 | SIMPLE | user | ref | user_index | user_index | 62 | const | 1 | Using where |
  6. +----+-------------+-------+------+---------------+------------+---------+-------+------+-------------+
  7. 1 row in set (0.00 sec)

可以看出已經命中索引user_index

explain和profiling分析查詢SQL時間