指向 SQLite 中表的指针
假设我有一张苹果表(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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您刚才描述的听起来像是一场维护噩梦。这里你看到的是苹果和它的重量随时间变化的一对多关系。您可以这样做:
然后为给定的苹果拉动所有权重,只要
正确索引,性能就不会成为问题。关系数据库在执行此类操作时非常高效,因为它们就是为此而设计的。
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:
Then to pull all weights for a given apple, just
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.