历史/审计表的设计建议

发布于 2024-10-06 17:29:11 字数 744 浏览 0 评论 0原文

我需要随时跟踪许多项目及其状态。

示例

ItemId Location  DateTime           State
   1   Mall A    2010-02-03 07:00   on_sale
   1   Mall A    2010-02-20 08:22   sold
   2   Warehouse 2010-02-02 09:00   on_sale
   2   Transit   2010-03-02 16:20   transit
   2   Mall B    2010-03-03 10:10   on_sale
   2   Mall B    2010-03-12 12:11   sold

现在,这是一个巨大的表,我使用 MySQL 的伪排名函数来执行查询。

然而,它非常慢。查询无法使用索引,因为对于任何特定项目,它必须先找到排名 = 1 的项目,然后才能执行任何过滤。

SELECT 
    item_sorted.*, IF(@prev <> item_sorted.item_id, @rownum := 1, @rownum := @rownum+1) AS rank,
    @prev := item_sorted.item_id
FROM ...

您在实现此类功能方面有哪些经验? 您推荐的数据库设计是什么? 像 Oracle 或 T-SQL 这样的数据库会因为支持分区查询而更好吗?

预先感谢您的任何想法!

I need to keep track of many items and their states throughout time.

Example

ItemId Location  DateTime           State
   1   Mall A    2010-02-03 07:00   on_sale
   1   Mall A    2010-02-20 08:22   sold
   2   Warehouse 2010-02-02 09:00   on_sale
   2   Transit   2010-03-02 16:20   transit
   2   Mall B    2010-03-03 10:10   on_sale
   2   Mall B    2010-03-12 12:11   sold

Right now, this is a huge table and I use MySQL's pseudo rank function to perform the queries.

However, it is very slow. The queries are unable to make use of the indexes since for any particular item it will have to find the item with rank = 1 before it can perform any filtering.

SELECT 
    item_sorted.*, IF(@prev <> item_sorted.item_id, @rownum := 1, @rownum := @rownum+1) AS rank,
    @prev := item_sorted.item_id
FROM ...

What are your experiences in implementing such feature?
What is the database design that you would recommend?
Would a database like Oracle or T-SQL capable DBs be a lot better due to their support for partitioned queries?

Thanks in advance for any ideas!

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

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

发布评论

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

评论(1

命硬 2024-10-13 17:29:11

让我从一个问题开始:

这张表的主要目标是什么?向其中插入数据或从中检索数据?

两种可能性:

  • 对于插入:我建议您将其标准化,您将有很多冗余。
  • 对于检索:这样就很好。我会为每组推荐一个代理键。

如果你想比较时间消耗,可以尝试安装PostgreSQL并查看结果。
您不需要使用 Oracle 或 MS-SQL,PostgreSQL 应该足够好。

我知道我的建议与数据分区无关,但是在工作中有一个类似的表,这就是我的做法,而且效果很好。

希望我能帮忙,

Let me start with a question :

What is the main goal of this table ? To insert data in it or to retrieve data from it?

Two possibilities :

  • For inserting : I would recommend you to normalize this, you will have a lot of redundancy.
  • For retrieving : It is good the way it is. I would recommend a surrogate key for each set.

If you want to compare time consumtion, try to install PostgreSQL and see the result.
You should not need to use Oracle or MS-SQL, PostgreSQL should be good enough.

I know that my recommendation has nothing to do with data-partitioning, but having a similar table at work this is how I do it and it works fine.

Hope I could help,

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