指向 SQLite 中表的指针

发布于 2024-09-20 00:25:40 字数 266 浏览 8 评论 0原文

假设我有一张苹果表(apple_id)。对于每个苹果,我都有其重量随时间变化的历史记录。如何构建它,以便每次添加苹果时,都会创建一个包含其重量历史记录的关联表?

在查看苹果的重量历史记录时,只有一张所有苹果的重量历史记录表(apple_id、年龄、重量)似乎会消耗性能,因为它必须查看所有其他苹果的重量历史记录。所以看来我需要为每个新苹果创建一个新表(并在每次删除苹果时删除它)。如何做到这一点?

我的想法是使用 apple_id 并将其转换为字符串。这样表的名称是唯一的并且易于生成。

Say I have a table of apples (apple_id). And for each apple I have a history of its weight over time. How to structure it so that every time an apple is added, an associated table is created with its weight history?

Having only one table of weight history for all apples (apple_id,age,weight) seems like a performance drain when looking to view an apple's weight history, since it has to look through the weight history of all other apples. So it seems I need to create a new table for every new apple (and delete it every time an apple is deleted). How to do this?

My idea is to use take the apple_id and convert it to string. That way the table's name is unique and easy to generate.

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

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

发布评论

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

评论(1

∞琼窗梦回ˉ 2024-09-27 00:25:40

您刚才描述的听起来像是一场维护噩梦。这里你看到的是苹果和它的重量随时间变化的一对多关系。您可以这样做:

apples table: apple_id, other apple fields
apple_weights table: aw_id, apple_id, weight, age

然后为给定的苹果拉动所有权重,只要

SELECT weight, age 
FROM apple_weights 
WHERE apple_id=X 
ORDER BY age ASC

正确索引,性能就不会成为问题。关系数据库在执行此类操作时非常高效,因为它们就是为此而设计的。

What you just described sounds like a maintenance nightmare. What you have here is a one-to-many relationship between apple and its weights over time. You can do:

apples table: apple_id, other apple fields
apple_weights table: aw_id, apple_id, weight, age

Then to pull all weights for a given apple, just

SELECT weight, age 
FROM apple_weights 
WHERE apple_id=X 
ORDER BY age ASC

If it's properly indexed, performance won't be a problem. Relational databases are very efficient when doing stuff like this because they were designed for it.

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