Rails:针对一组具有可变长度的独特项目的数据库设计

发布于 2024-12-21 23:27:45 字数 295 浏览 3 评论 0原文

我有一个项目表。该表中可能会有大约 5 个项目,我预计它不会增长太多。我有另一个名为 Orders 的表,其中包含数十万行。每个订单需要引用 5 个项目的某种组合(可以是 0 个项目(空),可以是全部 5 个项目或介于两者之间)。任何项目都不能计算两次,并且项目的顺序无关。这是一个多对多的关系。

我想利用项目组合是有限的这一事实。因此,如果一个订单已经引用了特定的项目组合,则第二个订单可以引用同一组项目,而不是复制连接表中的多对多关联。对于如此多的订单,items_orders 连接表将会很大,我认为这是不必要的。

你会如何解决这个问题?

I have a table of Items. There will likely be around 5 items in this table and I don't expect it to grow much beyond that. I have another table called Orders with hundreds of thousands of rows. Each order needs to reference some combination of the 5 items (could be 0 Items (null), could be all 5 or somewhere in between). No item can be counted twice and the order of items is irrelevant. This is a many-to-many relationship.

I want to capitalize on the fact that the combination of Items are finite. So if an Order has already referenced a particular combination of items, a second Order can reference the same set of items instead of duplicating the many-to-many associations in the join table. With so many Orders the items_orders join table would be huge and unnecessarily I believe.

How would you solve this problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

输什么也不输骨气 2024-12-28 23:27:45

数据库表可以处理数百万甚至数十亿条记录。我认为@coreyward 的意思是你不应该过早地优化代码。以适合您的方式构建它并解决出现的性能问题。另外,当您需要解决性能问题时,您总是有可能想要更改应用程序或数据库。通过不试图预先压缩最后一点性能而节省的时间可以用来解决真正的问题。

Database tables can handle millions, if not billions of records. I think what @coreyward was getting at is that you shouldn't prematurely optimize code. Build it in a way that feels right to you and address performance issues as they arise. Plus, there's always a chance that by the time you need to address the performance, you'll want to change your application or database anyway. The time you saved by not trying to squeeze every last bit of performance out up front can then be used to fix the real issues.

只怪假的太真实 2024-12-28 23:27:45

计算组合数:

  • 5 个项目的 1 个组合
  • 4 个项目的
  • 5 个组合 3 个项目的 10 个组合
  • 2 个项目的 10 个组合
  • 1 个项目的 5 个组合

因此,您想要修复一个包含 5 个项目的 31 种不同组合的表格。然后你就会有一张桌子

class Combo < ActiveRecord::Base
   has_many :items
   has_many :orders
end
class Item < ActiveRecord::Base
   belongs_to :item
end
class Order < ActiveRecord::Base
   belongs_to :combo
end

,这看起来不错,因为你的桌子又漂亮又紧凑。 Item 表限制为 5 个项目,Combo 表限制为 31 个项目,Order 表变化为 1000000 个条目。

您可以进一步简化并完全摆脱 Item 和 Combo 表,然后对它们进行硬编码。毕竟,它们的数量都非常小。打个比方,如果您有一个应用程序需要执行高达 100 的阶乘计算,那么只需使用硬编码的查找表即可使其速度非常快。

该解决方案有几个问题。您的订单配置软件必须了解所有组合并确定用户选择了哪一种。所以它只是一个查找表或具有 31 种变化的链接条件语句。可能,但容易出错。另一个

问题是,如果您需要更改组合或拥有可以选择多个项目的组合,那么您将需要编写更多代码,并且数量会随着允许的每个附加组合而组合增加。而且你必须对它们全部进行硬编码。

您需要确定编写代码和修复错误的时间比仅仅为与订单关联的每个项目支付连接表条目的费用更便宜还是更昂贵。在我的情况下,编码要昂贵得多,因此,我只会选择如下所示的结构。

class ItemOrder < ActiveRecord::Base
   belongs_to :item
   belongs_to :order
end
class Item < ActiveRecord::Base
   has_many :item_combo
end
class Order < ActiveRecord::Base
   has_many :item_combo
end

To tally up the combinations:

  • 1 combination of 5 items
  • 5 combinations of 4 items
  • 10 combinations of 3 items
  • 10 combinations of 2 items
  • 5 combinations of 1 item

So you would like to fix a table of 31 different combinations of the 5 items. Then you would have a table

class Combo < ActiveRecord::Base
   has_many :items
   has_many :orders
end
class Item < ActiveRecord::Base
   belongs_to :item
end
class Order < ActiveRecord::Base
   belongs_to :combo
end

And this seems good because your tables are nice and compact. The Item table is bounded at 5 items, the Combo table is bounded at 31 items, and the Order table varies to 1000000s of entries.

You could simplify further and get rid of the Item and Combo tables altogether and just hard-code them. After all, they are both bounded at a pretty small number. An analogy is if you have an application which needs to do factorial calculations up to 100, you can make it very fast by just having a hard-coded lookup table.

This solution has a couple of problems. Your order provisioning software must be aware of all of the combinations and determine which one the user has chosen. So it is just a lookup table or a chained condition statement with 31 variations. Possible, but error-prone. Y

The other problem is that if you ever need to change combinations or have combinations where you can pick more than one of any item, then you will experience the need to write more code, and the number increases combinatorically with each additional combination allowed. And you have to hard-code them all.

You need to determine if your time writing code and fixing bugs is cheaper or more expensive than merely paying for a join table entry for each item associated with an order. In my situation, coding is much more expensive, so, I'd just choose a structure which looks like this.

class ItemOrder < ActiveRecord::Base
   belongs_to :item
   belongs_to :order
end
class Item < ActiveRecord::Base
   has_many :item_combo
end
class Order < ActiveRecord::Base
   has_many :item_combo
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文