1. 程式人生 > >sql 大數據查詢慎用 order by

sql 大數據查詢慎用 order by

images 一個 com style count code 今天 not img

今天在sql 查詢中一個表中查詢花了至少20秒時間,數據為620000行,sql語句如下:

測試一:使用order by

單單只是查詢0,10行數據,耗時27.888s

select a.id,a.county_id,a.county_name,a.town_id,a.town_name,a.village_id,a.village_name,b.province as province_name,b.name as city_name from place a left join city b on a.city_id=b.code where a.id is not null order by a.village_id asc limit 0, 10

技術分享

測試二:不使用order by

select a.id,a.county_id,a.county_name,a.town_id,a.town_name,a.village_id,a.village_name,b.province as province_name,b.name as city_name from place a left join city b on a.city_id=b.code where a.id is not null limit 0, 10

同樣是查詢0,10行數據,耗時0.084s

技術分享

以上僅供參考,數據更大時需要進行更精細的優化

sql 大數據查詢慎用 order by