看看這個sqlfiddle我做了http://sqlfiddle.com/#!9/4b903/2/0。這是我的數據庫的簡化版本。我在歷史上有100萬條記錄,問題是查詢似乎太慢。有時需要一分鐘才能得到結果。我在這個SQL的東西不太好。我想有一些列需要索引,但我不知道那些是什麼。需要幫助來提高SQL查詢的性能
更新:
我試圖解釋SQL
id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra
1 | PRIMARY | t1 | range | created_reason,created_reason_2 | created_reason_2 | 6 | NULL | 91136 | Using where; Using temporary; Using filesort
2 | DEPENDENT SUBQUERY | t2 | ref | history_table1_id_foreign,table1_id,table1_id_2 | table1_id_2 | 4 | t1.table1_id | 11 | Using where; Using index; Using filesort
創建索引。 https://dev.mysql.com/doc/refman/5.6/en/optimization-indexes.html – hjpotter92
順便說一句錢通常是DECIMAL,而不是FLOAT – Strawberry
另外,您正在使用相關的子查詢。一個非編碼的幾乎總是更快。 – Strawberry