FK 上独一无二的 MySQL 组合
基于这个问题的问题FK的MySQL复合唯一
DB MySQL,存储引擎:InnoDB。
我有一个表计划:
- IDclubber_id
- (俱乐部表的外键)
- event_id(事件表的外键)
每个俱乐部成员只能为每个活动创建一个计划。 也就是说,clubber_id 和 event_id 理想情况下应该是唯一的复合键。
使用clubber_id 和event_id 创建这样的复合密钥是否有任何性能方面的优势。我已经有了外键,并且在我的业务逻辑中检查了唯一性。
Question based on this question MySQL composite unique on FK's
DB MySQL, storage engine: InnoDB.
I have a table Plan:
- ID
- clubber_id (Foreign key to clubbers table)
- event_id (Foreign key to events table)
Each clubber can only create one plan per event.
That is clubber_id and event_id should ideally be unique composite key.
Is there any point performance-wise to create such a composite key with clubber_id and event_id. I already have foreign keys in place and uniqueness is checked in my business logic.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这并不是真正的性能问题,但在外键上定义唯一的复合键可以确保数据库中的唯一性。如果这样做,您可以跳过对业务逻辑的检查(只要您可以处理尝试添加重复项的失败情况)。一般来说,MySQL中的组合键唯一性验证非常快;在数据库中完成它通常不会出现性能问题,并且它可以通过删除冗余检查来稍微清理您的业务逻辑。本质上,执行这种唯一性检查是数据库领域的事情;为什么不让您的数据库使用唯一性约束进行验证?
It's not really a performance thing, but defining a unique composite key on your foreign keys assures that there's uniqueness in the database. If you do that, you can skip doing the checking in your business logic (as long as you can handle the failure situation of attempting to add a duplicate). In general, the composite key uniqueness validation in MySQL is very fast; having it done in the database is usually not a performance problem, and it can clean up your business logic a bit by removing a redundant check. Essentially, performing this sort of uniqueness check is something that's in the database realm; why not let your database do the validation with the uniqueness constraint?