简体   繁体   English

数据库设计,减少join

[英]Database design, reducing join

This is a db for online store for tickets (such as Airbnb experience)这是一个用于门票在线商店的数据库(例如 Airbnb 体验)

For a product (ticket),为商品(票),

there are available days (and times)有可用的日子(和时间)

On an available day,在有空的日子,
- there could be multiple options (such as beginner-class, advanced-class) - 可能有多种选择(例如初级班、高级班)
- there is a quantity that can be sold (shared among multiple options) - 有一个可以出售的数量(在多个选项之间共享)

One way to represent this is表示这一点的一种方法是

Product
  name

Variant (Option)
  product

TimeSlot
  product
  date
  time
  quantity


TimeslotVariant
  variant
  timeslot

Another way would be the following.另一种方法如下。

I see two main differences,我看到两个主要区别,

  • First difference第一个区别

    • Above: you need join on TimeSlot to find what variants are on given day.上图:您需要加入TimeSlot以查找特定日期的变体。
    • Below: you can directly query TimeVariant下图:可以直接查询TimeVariant
  • Second difference第二个区别

    • Above: [{date, time, [variant1, variant2], quantity}] (I think client application would prefer this)上图: [{date, time, [variant1, variant2], quantity}] (我认为客户端应用程序更喜欢这个)
    • Below: [{date, time, variant1}, {date, time, variant2}] + [{date, time, quantity}]下面: [{date, time, variant1}, {date, time, variant2}] + [{date, time, quantity}]

Product
  name

Variant (Option)
  product


TimeSlot
  product
  date
  time
  quantity

TimeVariant
  variant
  date
  time

I think the first option is more intuitive (?) but I also think additional join can be painful to maintain sometimes我认为第一个选项更直观(?)但我也认为额外的连接有时会很痛苦

What questions (criteria) should I ask to myself to decide among the two?我应该问自己什么问题(标准)来决定这两者?

IMHO, The number of joins is probably not the most important question you need to ask yourself when designing a relational database.恕我直言,连接数可能不是您在设计关系数据库时需要问自己的最重要问题。

the most important question is how can you make sure you protect the data integrity.最重要的问题是如何确保保护数据完整性。 The data integrity is best kept with the first option you've presented, so this is the option you should go with.您提供的第一个选项最好保持数据完整性,因此这是您应该使用 go 的选项。

If the joins is what's bothering you, you can always use views to "flatten" the data.如果连接是困扰您的问题,您始终可以使用视图来“扁平化”数据。

But why the first option is better?但为什么第一个选项更好? Because the primary key (or, at least the natural key) of the TimeSlot table must be comprised of product , date and time - and the second option doesn't take the product into consideration in the TimeVariant table.因为TimeSlot表的主键(或至少是自然键)必须由productdatetime组成 - 而第二个选项不会在TimeVariant表中考虑产品。

You could add the product to that table as well, and some DBAs would suggest that as the best option (those would be the DBAs that are opposing to using surrogate keys) - but personally, even though I'm not a DBA myself I think that a surrogate key have it's advantages and one of them is exactly what you have here - you can use a single column instead of three to join two tables - which makes your life much easier (and with enforcing uniqueness of the natural key(s) of the table there's no integrity problem with surrogate keys).您也可以将product添加到该表中,并且一些 DBA 会建议将其作为最佳选择(那些将是反对使用代理键的 DBA) - 但就个人而言,即使我自己不是 DBA,我认为代理键有它的优点,其中一个正是你在这里所拥有的——你可以使用单列而不是三列来连接两个表——这让你的生活更轻松(并强制执行自然键的唯一性)表中没有代理键的完整性问题)。

First you should think do you want to reduce joining overheads?首先,您应该认为您想减少加盟费用吗? If yes then you may use flatten data stored in the views.如果是,那么您可以使用存储在视图中的展平数据。

First Case第一个案例

In the TimeslotVariant you have added variant and timeslot through which only by querying the TimeslotVariant table you will get your intended data.TimeslotVariant中,您添加了变体时间段,只有通过查询 TimeslotVariant 表,您才能获得预期的数据。 In this case your data integrity in okay.在这种情况下,您的数据完整性还可以。

Second Case第二种情况

If you add product key to your TimeslotVariant then only to show the product listing will serve the purpose though time and date are also getting re-entry.如果您将产品密钥添加到您的 TimeslotVariant,那么仅显示产品列表将达到目的,尽管时间和日期也会重新输入。

My suggestion is to keep the first case and store the flatten data in a view.我的建议是保留第一个案例并将展平数据存储在视图中。

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

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