简体   繁体   English

哪个更快,mysql数据库有一个表还是多个表?

[英]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. 我们正在讨论大约50万个广告。

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.) (当然,当你说“单表”时,我假设你的意思是一个表来保存Advertistment实体的核心属性。可能还会有其他表。)

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. 数据库非常适合处理500k的增加。 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 :) 如果您将表定义和类别分布添加到您的问题中,您可能会得到更好的答案:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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