
一、怎么知道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()
一、怎么知道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.建索引的結果集最好分布均勻,或者符合正態分布。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com