日期:2014-05-16  浏览次数:20543 次

索引与性能分析

一、怎么知道sql执行效率的高低呢???

1.set @@profiling =1;

2.执行语句。

3.show proliles;

mysql> show profiles;

Empty set (0.00 sec)


mysql> set @@profiling=1;
Query OK, 0 rows affected (0.00 sec)


mysql> select * from wtdoctor order by rand() limit 2 \g
+-------------+---------------+-------------------------------------------------
+-------------------+---------------+---------------+------------------+--------
------------+-----------+
| wmxDoctorID | wmxDoctorName | wmxDoctorPhoto
| wmxProfessionalID | wmxAcademicID | wmxHospitalID | wmxDepartmentsID | wmxDise
asesClassID | inputtime |
+-------------+---------------+-------------------------------------------------
+-------------------+---------------+---------------+------------------+--------
------------+-----------+
|           5 | 琉璃          | ./default/Tpl/image/2014/03/04/139391281120.jpg
| 技术              | 医生          | 不知道        | 都带点           | 都带点
            |         0 |
|           3 | 地点          | ./default/Tpl/image/2014/02/24/139322651638.jpg
|  地点             | 的d           | 地点          |                  |
            |         0 |
+-------------+---------------+-------------------------------------------------
+-------------------+---------------+---------------+------------------+--------
------------+-----------+
2 rows in set (0.00 sec)


mysql> show profiles;
+----------+------------+------------------------------------------------+
| Query_ID | Duration   | Query                                          |
+----------+------------+------------------------------------------------+
|        1 | 0.00058350 | select * from wtdoctor order by rand() limit 2 |
+----------+------------+------------------------------------------------+

1 row in set (0.00 sec)


想要查看语句执行的细节:

mysql>show proliles for query 1;


二、mysql执行计划。

     就是在select语句前面放上关键字explain(说明),mysql解释它将如何处理select,提供有关表如何联合和以什么次序联合的信息。

     我们能做什么?

     1.什么时候我们必须为表加入索引,以得到一个使用索引找到记录的更快的select方法。

     2.优化器是否以一个最佳次序联结表。

    mysql>explain[extended] select * from t;

    对比较复杂的查询进行计划分析时,可能会得到多条执行计划。

    例如:

    mysql>explain select * from t;


<