如果我有多种类型的对象,何时 object.type 应该是字符串,何时应该是外键?
假设我有一些书,可以是浪漫小说、小说或悬疑小说。 我有两个现实的选择来存储这些数据。 一种是在我的书籍表中有一个类型列,它是一个值为“浪漫”、“小说”或“神秘”的字符串。 另一种是创建一个 book_types 表并在其中存储类型。 然后我的书将有一个引用 book_types 表的 type_id 外键。
我的问题是如何选择最好的? 我已经看到 Restful 身份验证 Rails 插件中使用的字符串方法,其中包含有关用户状态的信息 - 'inactive'、'active'、'pending'...
考虑到我将使用查找表方法,是否会对性能造成影响一直在查询这些信息?
谢谢!
Let's say I have books which can be romance, fiction, or mystery. I have 2 realistic options to store this data. One is to have a type column in my books table that is a string with the value "romance", "fiction", or "mystery". The other is to create a book_types table and store the types in there. Then my books would have a type_id foreign key referencing the book_types table.
My question is how do I pick which is best? I have seen the string method used in the Restful authentication Rails plugin which contains information about the users state - 'inactive','active','pending'...
Is there a performance hit for using the lookup table method considering I will be querying for this information all the time?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在大多数情况下,使用单独表的外键的方法是最好的 - 优点:
单独的表为您提供了
验证条目的可扩展方式。 在表定义中放置硬编码检查约束
然后需要 ALTER TABLE 来添加
新类型
如果您出于某种原因需要更改类型文本(例如“浪漫”->“女性小说”作为蹩脚的示例),您只需对查找表进行轻量级更新。
您可能拥有尚无条目的类型,并且单独的表允许您使用外连接将该类型包含在 SQL 结果集中。
从界面角度来看,单独的表可让您轻松生成类型的下拉列表,无需在 UI 中进行硬编码。
从界面角度来看,单独
就性能而言,只要在 FK 上有适当的索引,任何 RDBMS 引擎都会表现良好 - 连接是 RDBMS 的设计目的。
In most cases the approach with a foreign key to a separate table is best - advantages:
The separate table gives you an
extensible way to validate the entry. Putting a hard-coded check constraint in the table definition
then requires an ALTER TABLE to add a
new type
If you ever need to alter the type text for some reason (e.g. "romance"-> "women's fiction" for a lame example), you'll only have a lightweight update to the lookup table.
You could conceivably have types for which there are no entries yet and the separate table lets you use an outer join to include the type in SQL result sets.
From the interface perspective, a separate table lets you easily generate a drop-down list of types that doesn't require hard-coding in the UI.
As far as performance goes, with the proper index on the FK any RDBMS engine will perform well - joins are what an RDBMS is designed for.
我会用fk。
重复信息较少。
编辑:
更好的解决方案:
MySql代码:
I would use fk.
Less information duplicated.
EDIT:
Betther solution:
MySql code:
外键方法会表现得更好。 字符串比较会减慢速度。 比较数字要快得多。
如果您想进一步加快查询速度,请在用于引用外键的列上添加索引。 与主键不同,外键不会自动创建索引。
The foreign key approach will perform better. String comparison will slow things down. It's much faster to compare numbers.
If you want to speed up queries even more, add an index on the column you are using to reference the foreign key. Unlike for primary keys, indexes are not automatically created for foreign keys.
如果不再需要存储更多信息,则字符串通常没问题(尽管这是一个非瞬态值,因此它不是正常形式)。
然而,这似乎是一个很好的表格候选者,因此您可能希望对类别进行更多操作,因此它应该是一个参考表,在我看来。
if there's never more information to be stored against something, string is normally fine (though this is a non-transient value so it's not normal-form).
This seems like a good candidate for a table, however, so you might want to do more with categories so it should be a reference table, imo.