我正在學習如何優化SQL語句,我想知道是否有可能通過查看執行來估計什麼可能會使查詢變慢計劃。只要看一眼的執行計劃是否有可能通過查看執行計劃來了解爲什麼我的查詢速度太慢
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: <derived2>
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 382856
Extra: Using where; Using temporary; Using filesort
*************************** 2. row ***************************
id: 1
select_type: PRIMARY
table: rf
type: ref
possible_keys: rec_id
key: rec_id
key_len: 4
ref: rs.id
rows: 7
Extra: Using index condition
*************************** 3. row ***************************
id: 2
select_type: DERIVED
table: f
type: range
possible_keys: facet_name_and_value,rec_id
key: facet_name_and_value
key_len: 309
ref: NULL
rows: 382856
Extra: Using index condition; Using where; Using temporary; Using filesort
*************************** 4. row ***************************
id: 2
select_type: DERIVED
table: r
type: ref
possible_keys: record_id
key: record_id
key_len: 9
ref: sqlse_test_crescentbconflate.f.rec_id
rows: 1
Extra: Using where; Using index
我可以看到,我使用了太多的連接和數據量太大,因爲SQL是使用文件排序,但我可能是錯的。
只是要注意,這不是萬能的詳盡清單。這裏的建議在「SELECT」語句的性能方面進行了相當多的調查。但是這個答案並沒有涉及很多其他的性能調優主題:例如sql結果緩存,存儲引擎(MyISAM,InnoDB等)的差異,以及調整mysql實例本身(例如分配給InnoDB緩衝池的內存等) – spencer7593