首页 > 其他 > 详细

索引与性能分析

时间:2014-03-07 02:26:09      阅读:466      评论:0      收藏:0      [点我收藏+]

一、怎么知道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;


三。MYSQL索引建立和使用的基本原则!

     1.合理设计和使用索引。

     2.在关键字段的索引上,建与不建索引,查询速度相差近100倍。

     3.差的索引和没有索引效果一样。

     4.索引并非越多越好,因为维护索引需要成本。

     5.每个表的索引应在5个以下,应合理利用部分索引和联合索引。

     6.不在结果集中的结果单一的列上建索引。比如性别字段只有0和1两种结果,在这个字段上建索引并不会有太多的帮助。

     7.建索引的结果集最好分布均匀,或者符合正态分布。

索引与性能分析,布布扣,bubuko.com

索引与性能分析

原文:http://blog.csdn.net/sangliu/article/details/20638033

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!