我與我的查詢花了17秒的執行問題(350K行):查詢性能;不知道發生了什麼
SELECT idgps_unit, MAX(dt)
FROM gps_unit_location
GROUP BY 1
解釋
1 SIMPLE gps_unit_location index fk_gps2 5 422633
擺弄它後,我想出了這個解決方案,將1秒:
Select idgps_unit, MAX(dt) from (
SELECT idgps_unit, dt
FROM gps_unit_location
) d1
Group by 1
解釋:
1 PRIMARY <derived2> ALL 423344 Using temporary; Using filesort
2 DERIVED gps_unit_location index gps_unit_location_dt_gpsid 10 422617 Using index
現在我很困惑 - 爲什麼查詢#2是快速的,而查詢#1似乎是相同的查詢,似乎寫得更有效。
指數1:DT,索引2:idgps_unit,INDEX3:idgps_unit + DT
執行時間是一致的;查詢#1總是需要17-19秒;而#1 < 1秒。
我使用Godaddy的VPS Windows Server 2008的經濟
表例如:
id | idgps_unit | dt | location
1 | 1 | 2012-01-01 | 1
2 | 1 | 2012-01-02 | 2
3 | 2 | 2012-01-03 | 3
4 | 2 | 2012-01-04 | 4
5 | 3 | 2012-01-05 | 5
mysql或tsql?!? – 2013-02-19 17:06:14
這兩個查詢的執行時間是否一致?因爲當第一個查詢被執行並被第二個查詢使用時,結果可能會被緩存。 – Slowcoder 2013-02-19 17:08:20
如果您可以在這些查詢上發佈運行'EXPLAIN'的結果,可能會有所幫助。 – 2013-02-19 17:14:19