如何在数据库级别设计定价模块
我想在数据库中模拟以下场景:
我有一个包,由独立的 Activities
和 Hotel
组成:
包 P:
- 活动 A1、A2、A3< /strong>
- 酒店H
活动
和酒店
是独立的实体,在定义时定义了自己的价格的实体。
当实体添加到套餐时,其价格可以更改(仅特定于套餐
)。因此,每个套餐的活动/酒店都有独特的价格。
例如 (定义活动和酒店)
:
A1 - 10$
A2 - 20$
H1 - 100$
(将活动和酒店添加到包中)
:
Package p;
p.addActivity("A1", 15);
p.addActivity("A2", 25);
p.addHotel("H1", 50);
yields =>
p
- A1 - 15
- A2 - 25
- H1 - 50
数据库端:
定义:
活动< /代码>:
活动 ID、开始日期、结束日期、价格
酒店
:酒店 ID、开始日期、结束日期、价格
添加到套餐后
套餐表
:
套餐 ID、酒店 ID、活动 ID
套餐-活动价格表
:Package-id, Activity-id, Price //package-id和activity-id作为唯一键
套餐-酒店价格表
:Package-id, Hotel-id, Price //package-id 和 hotel-id 作为唯一键
我需要一些关于我的设计的反馈。 我把这件事搞得太复杂了吗?有没有更简单/更好的方法来做到这一点? 另外,当我写这篇文章时,我认为一个套餐可以包含许多酒店和活动,因此需要在这里考虑这一点。 因为对于实体的每次价格查找我都将执行连接,所以我是否过于分散了?
编辑
找到了有关连接性能的相关链接:何时为什么数据库连接很昂贵?
I want to model the following scenario in the database:
I have a Package, comprising of independent Activities
and Hotel
:
Package P:
- Activities A1, A2, A3
- Hotel H
Activities
and Hotel
are independent entities which have their own price defined at the time of definition of entity.
When an entity is added to the package, it's price can be changed (only specific to the package
). Hence, every package would have a unique price for the activities/hotels.
E.g.(Defining activities and hotel)
:
A1 - 10$
A2 - 20$
H1 - 100$
(Adding activities and hotel to package)
:
Package p;
p.addActivity("A1", 15);
p.addActivity("A2", 25);
p.addHotel("H1", 50);
yields =>
p
- A1 - 15
- A2 - 25
- H1 - 50
Database Side:
Definition:
Activity
:Activity-id, StartDate, EndDate, Price
Hotel
:Hotel-id, StartDate, EndDate, Price
After adding to package
Package table
:
Package-id, Hotel-id, Activity-id
Package-Activity price table
:Package-id, Activity-id, Price //package-id and activity-id serve as unique key
Package-Hotel price table
:Package-id, Hotel-id, Price //package-id and hotel-id serve as unique key
I need some feedback on the design that I've come up with.
Have I over complicated this ? Is there a simpler/better way to do this ?
Also, as I was writing this, I figured a package can have many Hotels and Activities, so will need to factor that in here.
Have I fragmented this too much since, for each price lookup of an entity I will be performing a join ?
EDIT
Found a relevant link on performance with Joins: When and why are database joins expensive?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上你这里确实需要五张桌子。
您有三个实体:活动、酒店和套餐(假设每个套餐都有一些与酒店或活动无关的数据)。您有两个多对多关系,即套餐-酒店和套餐-活动。每个关系行都标有特定的价格。
五张桌子是正确的选择。不,必须进行联接不是问题:它比拥有非正常数据要好。您可以稍后如有必要对性能进行非规范化。
You actually do need five tables here.
You have three entities: activities, hotels, and packages (assuming each package has some data which is not hotel- or activity-related). You have two relationships, both many-to-many, namely package-hotel and package-activity. Each relationship row is annotated with a particular price.
Five tables is the way to go. No, having to do a join is not a problem: it's better than having non-normal data. You can denormalize for performance later if necessary.