MySQL:将一张表拆分为多个表(同一列)以提高性能?

发布于 2024-08-14 04:44:49 字数 888 浏览 6 评论 0原文

这个问题是关于性能的,而不是关于可能的解决方案。

我的系统包含许多不同类别的项目。每个类别都有自己的表,因为每个表都有很多行并且字段不同。

ItemA - id, fld1, fld2
ItemB - id, fld1, fld3, fld4
ItemC - id, fld1, fld3, fld5
....

现在需要管理用户库存,这意味着用户是否拥有商品。一种选择是使用单个表:

Inventory - category_id, item_id, user_id

ItemA、ItemB、...行的category_id 不同,这就是我们区分的方式。

第二个选项是:

InventoryA - item_id, user_id
InventoryB - item_id, user_id
...

第一个选项可能是最容易管理的,但是库存表很大(数量级:所有类别的项目数乘以用户数)并且经常更新和频繁查询。

第二种选择会有点难以管理(因为我们为每个类别创建一个新的库存表),但可能会带来性能增益,因为它可能会防止竞争条件。由于类别是完全隔离的,因此单个查询可能不需要涉及多个库存表。

目前系统使用MySQL和InnoDB引擎。 大约有 10 个类别,但预计在不久的将来会增加到几十个。最大的类别有> 200k 件商品,大多数都有 > 10k 项。 单个库存表有> 1000 万行,并且随着更多用户的加入预计会变得更大。

我知道最好的方法是测试这两种方法的性能并做出决定,但事实是,迁移到多表设计不会那么快速和轻松。

如果您有类似问题的个人经验,请分享。

谢谢

This question is about performance, not about possible solutions.

My system holds many items of different categories. Each category has its own table since each table has many rows AND the fields are different.

ItemA - id, fld1, fld2
ItemB - id, fld1, fld3, fld4
ItemC - id, fld1, fld3, fld5
....

Now there's a need to manage user inventory, meaning the user has an item or not. One option is using a single table:

Inventory - category_id, item_id, user_id

category_id is different for ItemA, ItemB, ... rows and that's how we differentiate.

Second option is to have:

InventoryA - item_id, user_id
InventoryB - item_id, user_id
...

The first option is probably the easiest to manage BUT the inventory table is huge (order of magnitude: number of items on all categories times number of users) and frequently updated and frequently queried.

The second option would be a bit harder to manage (as we create a new inventory table for each category) but may introduce a performance gain as it might prevent race conditions. No single query is likely to require involving more than one of the inventory tables as the categories are quite segregated.

Currently the system uses MySQL and InnoDB engine.
There are ~10 categories but expected to grow to a few dozens in the near future. Biggest category has > 200k items and most have > 10k items.
The single inventory table has > 10M rows and is expected to get MUCH bigger as more users join.

I know the best is to test the performance of both methods and decide but the truth is that it won't be so quick and painless to move to the multiple table design.

If you have personal experience with a similar issue, please share it.

Thanks

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

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

发布评论

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

评论(2

尾戒 2024-08-21 04:44:49

规范化数据库通常可以更好地提高性能和可维护性。

此方法将创建一个与 ItemA、ItemB 等具有 1:1 关系的表 Items。然后,您可以创建一个与基本 Inventory 关系的表 Inventory。代码>项目表。

根据文档,InnoDB支持row级别锁,因此无需使用多个表来防止死锁。

Normalizing the database is normally better for performance and for maintainability.

This approach would create a table Items that has a 1:1 relation with ItemA, ItemB, etc. Then you can create an Inventory table that has a relation with the base Items table.

According to the documentation, InnoDB supports row level locks, so there's no need to use multiple tables to prevent deadlocks.

回忆那么伤 2024-08-21 04:44:49

以上就是我对这个故事的看法,希望对大家有所帮助。

  • 项目表具有所有项目共有的字段。
  • 类别(A、B、C)表具有特定于每个类别的字段。
  • 一个用户拥有多个项目,一个项目可以被多个用户使用。

    inventory_model_01

Here is my take on this story, hope this helps a bit.

  • Item table has fields common to all items.
  • Category (A,B,C) tables have fields specific to each one.
  • One user has many items, one item can be used by many users.

    inventory_model_01

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