dtjw6660 2011-05-15 17:59
浏览 24
已采纳

MySQL表有4,000,000条记录?

The website I have to manage is a search engine for worker (yellow page style)

I have created a database like this:

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

So basically the request that I am having slow problem with is to select all the "workers" around a selecty city (select by the user)

The request I have created is fully working but takes 5-6 seconds to return results...

Basically I do a select on the table locations to select all the city in a certain radius and then join to the people table

  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; 

My questions are:

  1. Is my Database nicely designed? I don't know if it's a good idea to have 2 table with 4,000,000 records each. Is it OK to do a select on it?
  2. Is my request badly designed?
  3. How can I speed up the search?
  • 写回答

2条回答 默认 最新

  • drwiupraq047311240 2011-05-15 18:05
    关注
    1. The design looks normalized. This is what I would expect to see in most well designed databases. The amount of data in the tables is important, but secondary. However if there is a 1-to-1 correlation between People and Locations, as appears from your query, I would say the tables should be one table. This will certainly help.

    2. Your SQL looks OK, though adding constraints to reduce the number of rows involved would help.

    3. You need to index your tables. This is what will normally help most with slowness (as most developers don't consider database indexes at all).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?