简体   繁体   English

数据库:多个表还是只有一个表?

[英]Database: Multiple tables or just one table?

For example I have photos and videos tables, I can comment on these, but when I send it to database which way is better? For example我有photosvideos表,我可以对这些进行评论,但是当我将它发送到数据库哪种方式更好?

  1. To have 2 tables for comments: photo_comments and video_comments 要有2个评论表: photo_commentsvideo_comments

  2. Or to have 1 table comments and create a row inside the table like type and put there if it's a photo_comment or video_comment 或有1篇comments和创建像表内的行type ,并把那里,如果它是一个photo_commentvideo_comment

I think the 1 is faster because I have less data when I need to query the table but maybe the 2 is easier to use. 我认为1更快,因为当我需要查询表时我的数据更少但是2更容易使用。

Please let me know what's the best way, speed is very important for me. 请让我知道什么是最好的方式,速度对我来说非常重要。

I'm talking about a very big system with millions of data, millions of comments, so I want the fastest way to get the results, for me doesn't matter if I need to code more or need to keep in mind something in plus, results are much more important! 我说的是一个拥有数百万条数据,数百万条评论的非常庞大的系统,所以我想要以最快的方式获得结果,对我来说无关紧要,如果我需要更多代码或者需要记住一些东西加上,结果更重要!

If you really have two separate data tables photos and videos , I would always choose to use two separate comments tables, too. 如果你真的有两个单独的数据表photosvideos ,我总是会选择使用两个单独的评论表。

Why? 为什么?

If you put all your comments into a single comments table, but that references media from two separate data tables, there's no way you can easily set up a referential integrity between your comments table and the two data tables. 如果将所有注释放在单个comments表中,但引用来自两个单独数据表的媒体,则无法在注释表和两个数据表之间轻松设置参照完整性。 There are some workarounds (like having two separate reference fields, one for each), but none are really very compelling. 有一些解决方法(比如有两个单独的引用字段,每个引用字段一个),但没有一个真正非常引人注目。 Not having a referential integrity will ultimately lead to "zombie" data that doesn't belong to any existing media entry. 没有参照完整性将最终导致不属于任何现有媒体条目的“僵尸”数据。

Having two comments tables allows each comment table to properly reference its associated data table, thus your data integrity in the database will be better. 有两个注释表允许每个注释表正确引用其关联的数据表,因此数据库中的数据完整性将更好。

For that reason, if you have two separate data tables, I would always choose to use two separate comments tables as well. 因此,如果您有两个单独的数据表,我总是会选择使用两个单独的注释表。

It depends a bit more on how photos and videos are structured. 这取决于照片和视频的结构。 Consider the following DB Design: 请考虑以下数据库设计:

MediaType
----------
ID *
Name

Media
----------
ID *
TypeID
OwnerName
Name
Size
Path

Photo
----------
MediaID *
MediaTypeID (constraint, always set to the photo type)
Height
Width

Video
---------
MediaID *
MediaTypeID (constraint, always set to the video type)
Rating

If Photo and Video both had a FK to MediaType and to Media, I would make Comments relate to the Media table instead of either one, and not to the Photos or Videos table directly. 如果照片和视频都有一个FK到MediaType和媒体,我会使评论与媒体表而不是任何一个相关,而不是直接与照片或视频表。 This is often the type of design I use when Photo and Video have a lot of common properties. 这通常是我在Photo和Video具有许多常见属性时使用的设计类型。 It's especially useful when you want to do things like security because you aren't boxed into repeating the same visibility and ownership constructs on each type of media you're dealing with. 当您想要执行安全性操作时,它尤其有用,因为您没有在您正在处理的每种类型的媒体上重复相同的可见性和所有权构造。 It's also quite fast to query because many queries often look only for common properties, or just type-specific rows, so some tables don't need to be included. 它的查询速度也非常快,因为许多查询通常只查找公共属性,或者只查找特定于类型的行,因此不需要包含某些表。 Designing the database by modeling these IS-A relationships also keeps your indexes highly selective, which means speed . 通过对这些IS-A关系建模来设计数据库也可以使您的索引具有高选择性,这意味着速度

If you're locked into your design and Videos and Photos have no commmon "base table", then I would make a separate comments table for each. 如果你被锁定在你的设计中,而视频和照片没有通用的“基表”,那么我会为每个人制作一个单独的评论表。

Splitting up the tables would be better performance-wise, since you wouldn't have to query on an extra "comment type" column. 拆分表格会更好,因为您不必查询额外的“评论类型”列。 The downside of doing things this way is not reusing code (possibly in the future, if you add comments to other things). 以这种方式做事的缺点是不重用代码(如果你将评论添加到其他东西,可能在未来)。 But it doesn't sound like you're concerned with that. 但这听起来并不像你那么担心。

Why not having only one comment table? 为什么不只有一个评论表? Is there any diffrence between a comment on a video or a photo? 视频评论或照片之间是否存在差异? If not you should only have a column that holds the foreign key for the video/photo the comment is poiting to and an additional column with the type ENUM that holds the information of the type of resource the comment is ment for. 如果不是,您应该只有一个列,其中包含注释所针对的视频/照片的外键以及一个类型为ENUM的附加列,该列包含注释所针对的资源类型的信息。

Using an ENUM will keep your queries very fast (as it is saved as a number) and makes it easy to use string in your query. 使用ENUM可以非常快速地保存查询(因为它保存为数字),并且可以在查询中轻松使用字符串。

I don't think that the choice of whether to have 1 or 2 tables for comments is going to have any appreciable impact on the performance of your application. 我不认为选择是否有1或2个表用于评论会对应用程序的性能产生任何明显的影响。

You should choose whichever one makes more sense in the context of your application. 您应该选择在您的应用程序环境中更有意义的任何一个。

For example, if comments on photos and comments on videos are both going to act in the same way then you should have one table, if however (for example) comments on videos are allowed to be twice as long as comments on photos, or comments on photos have an additional "ranking" field or something, then 2 tables would make more sense. 例如,如果对照片的评论和对视频的评论都以相同的方式行事,那么您应该有一个表格,但是如果(例如)对视频的评论被允许的时间是照片评论或评论的两倍长在照片上有一个额外的“排名”字段或其他东西,那么2个表格会更有意义。

your queries will either look like 你的查询看起来像

select * from comments where linked_id = 555

or 要么

select * from comments where linked_id = 555 and comment_type = 1

(with comment type=1 meaning it's a video). (注释类型= 1意味着它是一个视频)。

As long as comment type as an index, they will basically be just as fast. 只要注释类型作为索引,它们基本上就会一样快。

Only thing I would consider, is columns. 只有我会考虑的是列。 If video comments has a different set of comments from picture comments, split em up. 如果视频评论与图片评论有不同的评论集,请将其拆分。 If everything is the same, keep em together. 如果一切都是一样的话,请将它们放在一起。

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

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