简体   繁体   中英

which is faster, mysql database with one table or multiple tables?

On my website you can search 'ads' or 'classifieds'. There are different categories.

Would the searches be faster with multiple tables, one for each category, or wouldn't it matter?

We are talking about around 500 thousand ads.

If it won't slow down the search, please explain yourself so that I understand why it won't, because it seems like common sense that the more ads you have, the slower the search!

Thanks

Your question is a little unclear. I'm assuming this scenario:

table: ads
id category ad_text
-- -------- ---------------------------
1  pets     sample text
2  family   sample ad

If you are making one search of ads, then searching multiple tables on each search is slower than searching one table.

HOWEVER, if you're proposing to break "ads" into multiple tables according to "category", leaving you with table names like

  • pets-ads
  • family-ads
  • programmer-ads

And, programatically, you know you're looking for programmer-ads so you can just go search the programmer-ads table, then breaking them out is faster. Barely.

Breaking them out, though, has many drawbacks. You'll need:

  • some cute code to know which table to search.
  • a new table each time you create a new category
  • to rename a table if you decide a category name is wrong

Given the limited info we have, I would strongly advise one table with a category column, then go ahead and normalize that out into its own table. Slap an index on that column. Databases are built to handle tons of rows of data organized correctly, so don't worry about that so much.

Obviously, it will be nominally faster to search a smaller table (one category) than a larger table. The larger table is probably still the correct design, however. Creating multiple identical tables will simply make the developer's and manager's lives miserable. Furthermore, certain kind of searches are more difficult if you segment the data (for instance, searches across two categories).

Properly indexed, the single-table approach will yield results almost as good as the segmented approach while providing the benefits of proper design.

(Of course, when you say "single table", I assume that you mean a single table to hold the core attributes of the Advertistment entities. Presumably there will be other tables as well.)

It depends.

If you've built a single denormalised table containing text, it'll get progressively slower for a number of reasons. Indexes help to a certain point.

If you have a normalised structure with multiple tables, primary and foreign keys, indexes, etc., it can be more robust and scalable.

A database is very well equipped to deal with 500k adds. Add an index on the category, and you should be fine.

If you add the table definition and the distribution of categories to your question, you'd probably get a better answer :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM