gpt4 book ai didi

sql - Rails 应用程序 - 最好保留或删除过时的数据

转载 作者:行者123 更新时间:2023-11-29 13:57:32 24 4
gpt4 key购买 nike

tl;dr - At the bottom, I ask a couple general questions about smart database design that don't require reading the whole thing. So feel free to skip and take a look at those. Thanks!

我正在构建一个 Rails 网络应用程序,在用户输入一些内容后,我会生成许多选项,用户可以从中选择一个,然后将其保存并与用户相关联。这种选择可能会在选项生成后很长时间内出现——因此需要保存选项——但一旦做出选择,所有“不正确”的选项都不再相关。

我对数据库的工作原理和减慢速度的原因知之甚少,因此我非常感谢任何关于如何最好地实现这一目标的输入(如下所述),考虑到我在我想到的方法中看到的冲突.

在我看来,我可以采取三种途径:

  1. 让选项只是 Choice 模型的实例,通过 belongs_to/has_many 与用户相关联。当用户选择一个时,它的 id 被声明为用户的 choice_id(并且,其他选项可能与用户解除关联)。

    The main issue with this that I see is the exponential increase in the size of the Choice table which would result. This is a major table in my app, and I would like to keep it as quick as possible.

  2. 在用户输入之后,我生成并保存比方说 5 个Choice 对象,所有这些对象都属于用户,作为选项 .然后,当用户选择时,删除四个,第五个关联为该用户的单个选择

    The downside, as I perceive it, is that I end up with lots of "empty" rows in the database, so that the highest choice_id is significantly greater than the number of choices in the database. A boss I recently worked for gave the advice never to delete data, which obviously clashes with this approach.

  3. 我为 Option 模型创建了一个单独的表,它实际上在所有方面都与 Choice 模型相同,因为选项需要完全丰富的替身供您选择。这些选项 belong_to a user,如上所示,但当我选择毕业成为 choice

    (这有点类似于 this older post's discussion of archiving by moving old data to a new table 。这被认为是一种好的做法吗?对我来说似乎很理想,但我不知道潜在的缺点)。

    As I see it, this option preserves all data while keeping my main Choice table unencumbered by kruft unchosen data. Unfortunately, it adds a major duplication in the database, and a duplication of code as well (though I suppose I could have Option just inherit from Choice, which is weird, but whatever).

但是,所有这些意见都完全不了解数据库架构和效率方面的知识,如果我知道的话,我会觉得能够更好地权衡取舍:

  • 表中已删除的行是否会像现有行一样减慢事务处理速度?较少的?完全没有?什么时候表大小成为 SQL 数据库中的一个严重问题?值得思考吗?

  • 删除数据的最佳做法是什么?这真的是个坏主意吗?

  • 关于重复表的最佳做法是什么? (即具有相同数据结构的表,由于多种原因,与原始表的处理方式不同)。

  • 最广泛、最不恰当且最有帮助的问题:您将如何处理这个问题,优化 Choice 表的效率? (但也要尽可能避免重复并遵循最佳实践)。

谢谢!

最佳答案

一个以“.”为前缀的小文件怎么样?所以它被隐藏(在 unix 中)放置在用户主目录中。如果您以可读的格式输出您的选项,则可以轻松阅读和修改它们。

您只希望在性能或功能合理的情况下使用数据库。如果您要求用户使用必须安装的数据库,而客户可能不喜欢这样做。

关于sql - Rails 应用程序 - 最好保留或删除过时的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28573021/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com