Rails 3 中一个 AR 模型内的多个数据库表

发布于 2024-11-07 04:43:01 字数 770 浏览 1 评论 0原文

我被要求提供某种报告(日志记录)服务。该员工在许多公司本地安装了Web应用程序(只是一些用PHP编写的动态网站)。这个网络应用程序是某种调查。所有数据都保存在本地数据库中,但现在的要求是每次提交表单后,这些数据(调查结果)也将发送到中央服务器。

有四种类型的调查。他们是这样组织的,有很多项目,每个项目每种类型只能有一个调查(这里是 STI?),而调查属于一个项目。每个调查都会收到来自本地应用程序的报告,因此它会有很多报告。记录此报告的 Rails 3 应用程序应该以某种方式模仿此逻辑。第一个问题是:这个 AR 结构对你来说有意义吗?

  Project-1--------1-Survey-1-------*-Report

  Project
    has_one :survey
    has_many :reports, :through => :survey

  Survey
    belongs_to :project
    has_many :reports

  Report
    belongs_to :survey

第二个问题是关于一个 AR 模型有多个表。如果所有数据都存储在reports表中,该表将很快变得巨大,并且在一段时间后有效查询属于特定调查的报告可能会成为问题。也许每个调查都有单独的表格会更好?就像reports_。这可能吗?

另外,我不知何故被迫使用 MySQL,但如果有另一个更好的解决方案,我可以尝试推动它。

如果您还在这里,感谢您阅读本文:)

I was asked to make some kind of reporting (logging) service. The employee has locally installed web application (just some dynamic website, written in PHP) in many companies. This web app is some kind of survey. All data is saved on local database, but now the requirement is that this data (result of survey) will also be sent to central server after every form submit.

There are four types of surveys. They have organised it this way, that there are many Projects, and each Project can have only one Survey of each type (STI here?) and Survey belongs to one Project. Each Survey will receive a report from local app, so it will have many Reports. The Rails 3 app that logs this reports should mimic somehow this logic. First question is: does this AR structure make sense for you?

  Project-1--------1-Survey-1-------*-Report

  Project
    has_one :survey
    has_many :reports, :through => :survey

  Survey
    belongs_to :project
    has_many :reports

  Report
    belongs_to :survey

Second question is about having multiple tables for one AR Model. If all data will be stored in reports table, the table will become huge very quickly, and efficient querying for reports that belong to specific survey might be a problem after some time. Maybe it would be better to have separate tables for each Survey? Like reports_<survey_id>. Is this possible?

Also, I am somehow forced to use MySQL, but if there is another, much better solution for this, I could try to push it through.

If you are still here, thank you for reading this :)

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

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

发布评论

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

评论(2

三生池水覆流年 2024-11-14 04:43:01

第二个问题是关于一个 AR 模型有多个表。如果所有数据都存储在报告表中,该表将很快变得巨大,并且一段时间后,高效查询属于特定调查的报告可能会成为问题。也许每个调查都有单独的表格会更好?喜欢报道_。这可能吗?

是的,这是可能的。

您可以这样做:

class Report < AR::Base
  table_name_suffix = ''
end

Report.table_name_suffix = 'foo'

更新


# simple example of sharding model

class Survey < AR::Base
  has_many :reports

  def shard_reports
    Report.table_name_suffix = "_survey_#{self.id}"
    returning(reports){ Report.table_name_suffix = "" }
  end

end

Survey.first.reports_shard

Second question is about having multiple tables for one AR Model. If all data will be stored in reports table, the table will become huge very quickly, and efficient querying for reports that belong to specific survey might be a problem after some time. Maybe it would be better to have separate tables for each Survey? Like reports_. Is this possible?

Yes, it is possible.

You can do it this way:

class Report < AR::Base
  table_name_suffix = ''
end

Report.table_name_suffix = 'foo'

UPDATE


# simple example of sharding model

class Survey < AR::Base
  has_many :reports

  def shard_reports
    Report.table_name_suffix = "_survey_#{self.id}"
    returning(reports){ Report.table_name_suffix = "" }
  end

end

Survey.first.reports_shard
∝单色的世界 2024-11-14 04:43:01

在每个外键上放置一个索引(例如Reports.survey_id)并休息一下。你现在太担心表现了。在您看到 MySQL 出现任何性能问题之前,您的报告表中至少需要数百万条记录。

Put an index on each of your foreign keys (e.g. Reports.survey_id) and take a breather. You're worrying entirely too much about the performance right now. You will need at least millions of records in your Reports table before you will see any performance problems from MySQL.

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