網站我要管理是對工人的搜索引擎(黃色頁面樣式)有4,000,000條記錄的MySQL表?
我創建這樣的數據庫:
People: <---- 4,000,000 records
id
name
address
id_activity <--- linked to the activites table
tel
fax
id_region <--- linked to the regions table
activites: <---- 1500 activites
id
name_activity
regions: <--- 95 regions
id
region_name
locations: <---- 4,000,000 records
id_people
lat
lon
所以基本上,我有慢的問題,是請求選擇所有的「打工皇帝」圍繞一個selecty城市(由用戶選擇)
我已經創建了完全工作,但需要5-6秒才能返回結果的請求......
基本上我做了選擇素在桌子上的位置T以一定半徑選擇所有城市,然後加入到百姓餐桌
SELECT people.*,id, lat, lng, poi,
(6371 * acos(cos(radians(plat)) * cos(radians(lat)) * cos(radians(lng) - radians(plon)) + sin(radians(plat)) * sin(radians(lat)))) AS distance
FROM locations,
people
WHERE locations.id = people.id
HAVING distance < dist
ORDER BY distance LIMIT 0 , 20;
我的問題是:
- 是我的數據庫很好的設計?我不知道是不是每個有400萬記錄的2個表是個好主意。可以選擇嗎?
- 我的要求設計得不好嗎?
- 如何加快搜索速度?
你的SQL有一個'HAVING'子句,但沒有'GROUP BY' - 你確定這是對的嗎? – Oded 2011-05-15 18:03:06
每個人是否有eaxactly 1個位置或每個人可以有多個位置?如果沒有其他理由要拆分,將lat + lng表移動到people表中將會有所幫助。 – bot403 2011-05-15 18:04:15
@Oded:是的,我的團隊只是忘了粘貼 – Fazoulez 2011-05-15 18:16:00