0

I have 30 tables, each representing a different neighborhood. Each table holds real estate listing with a "Price", "Number Of Rooms", "Square Feet" etc columns.

The end user would be able to choose as many neighborhoods as he likes with the option to screen out results such as "At least 5 Rooms", "Below 250k" etc and sort the results by "Lowest Price", "Time Submitted", you get the point.

Now I'm a programmer not a DBMS guy. I've search the web but feel that trying to build the query one step at a time would be the wrong approach without some guidance on what to avoid.

I would love to hear and learn from the StackOverflow community on best approaches with this one. Please help me sort this up.

EDIT: i'm currently using MyISAM

4
  • Also all the tables are the same Commented Mar 24, 2011 at 21:12
  • Is there any reason why you have 30 tables, which as far as we have been told cary identical types of data? Commented Mar 24, 2011 at 21:13
  • Yes because i set up a cron job to output data from them to an xml every once in a while and i want to scale so that was the best option for me, instead of every time a user visits the neighborhood page to query WHERE neighborhood = 'myhood". Commented Mar 24, 2011 at 21:17
  • 1
    Using indexes will solve the scaling problem for you, even if you listed every property in the world. Commented Mar 25, 2011 at 0:25

3 Answers 3

3

You should not have 30 tables. Normalize your schema:

NEIGHBORHOOD
ID, Name

PROPERTY
ID, NeighborhoodID, Name, Price, Rooms, SquareFeet

Then you can join these together:

SELECT n.Name AS Neighborhood, p.Name AS Property, Price, Rooms, SquareFeet
FROM Property AS p
INNER JOIN Neighborhood AS n ON h.NeighborhoodID = p.ID
WHERE p.NeighborhoodID = X

Then you may need indexes on the tables as the data grows.

Sign up to request clarification or add additional context in comments.

3 Comments

Real-estate, not hotels :). You can put Property for the table name.
what if i want to limit the results each time say to 6? does the LIMIT option helps? or does it render everything and only display 6 results?
The LIMIT option will only return the amount of results specified. The query optimizer will not render then return it will include the limit in is analysis and return the top results in the most efficient manner it can.
0

You should start modifying your database model. Creating 30 tables for storing the same data (real state information) is not adequate. Try to put all the data in a single table adding a column that indicates the neighborhood. This neighborhood could point to another table with the name, description, ... of the neighborhood. Then you can query a single table to search across all neighborhoods and optionally filtrate the neighborhood the user want to search for.

Comments

0

The best way is to change your db model, get rid of 30 tables, and put everything in one table. With your current model, I don't see any other ways but create a huge union (you can put it into a view, and query this view).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.