对关联对象模式的扩展详细信息进行高效查询

发布于 2024-10-04 06:48:39 字数 939 浏览 6 评论 0原文

详细信息

  1. 有许多目标
  2. 目标具有有关活动特定详细信息。
  3. Activity 包含通用信息,例如世界纪录。
  4. 可以组织事件来尝试实现目标
  5. Person 通过Invitation 邀请其他Person 观看Event

模式

注意:示例模式图中仅列出了反向引用,用“(fk)”表示。箭头表示正常关系。

图片链接直到我获得 10 点才能使用图片标签

问题

我想要所有邀请的一个人<的大部分事件目标活动详细信息/code> 收到(无论状态如何,但仍然需要状态)立即显示。

在我尝试解决这样的 JOIN 之前,是否有更好的方法来表示问题?我相信 -> Invitation <- Event 是一个关联对象模式,但我不确定如何获得目标并且对于返回的每个邀请,以干净、高效的方式提供活动信息。

奖励:提供示例 SQLAlchemy 查询。

Details

  1. A Person has many Objectives.
  2. Objectives have Person-specific details about Activitys.
  3. An Activity contains generic information such as a world record.
  4. A Person can organize an Event to attempt the Objective.
  5. A Person invites other Persons to watch an Event with an Invitation.

Schema

Note: Only backref's are listed on the example schema diagram, indicated by "(fk)". The arrows imply the normal relationship.

Image Link Until I Get 10 Points To Use Image Tag

Question

I want most Event, Objective, and Activity details for all Invitations one Person received (irregardless of status, but the status is still needed) displayed at once.

Is there a better way to represent the problem before I try tackling a JOIN like this? I believe the Person -> Invitation <- Event is an Association Object pattern, but I am unsure of how to get the Objective and Activity information in a clean, efficient manner for each Invitation returned.

Bonus: Provide sample SQLAlchemy query.

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

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

发布评论

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

评论(1

渡你暖光 2024-10-11 06:48:39

在 SQL 方面,这非常简单。我仅使用一个 ID 号(针对人员)构建了一些用于测试的表;所有其余的键都是自然键。查看此查询的执行计划

select I.*, A.activity_placeholder, E.event_location, O.objective_placeholder
from event_invitations I
inner join activity A 
        on (I.activity_name = A.activity_name)
inner join events E 
        on (I.personal_id = E.personal_id 
        and I.activity_name = E.activity_name
        and I.objective_deadline = E.objective_deadline
        and I.event_time = E.event_time)
inner join personal_objectives O 
        on (I.personal_id = O.personal_id
    and  I.activity_name = O.activity_name
    and  I.objective_deadline = O.objective_deadline)
where I.person_invited_id = 2;

表明,除了对 event_invitations 进行顺序扫描外,dbms (PostgreSQL) 始终使用索引。我确信那是因为我使用的数据非常少,所以所有这些表都可以轻松放入 RAM。 (当表适合 RAM 时,扫描小表通常比使用索引更快。)

优化器估计查询每个部分的成本为 0.00,您无法得到比这更好的结果。实际运行时间不到 0.2 毫秒,但这并没有多大意义。

我相信您可以将其翻译成 SQLAlchemy。如果您希望我发布表格和示例数据,请告诉我。

On the SQL side, this is pretty straightforward. I built some tables for testing using only one id number (for persons); all the rest of the keys were natural keys. Looking at the execution plan for this query

select I.*, A.activity_placeholder, E.event_location, O.objective_placeholder
from event_invitations I
inner join activity A 
        on (I.activity_name = A.activity_name)
inner join events E 
        on (I.personal_id = E.personal_id 
        and I.activity_name = E.activity_name
        and I.objective_deadline = E.objective_deadline
        and I.event_time = E.event_time)
inner join personal_objectives O 
        on (I.personal_id = O.personal_id
    and  I.activity_name = O.activity_name
    and  I.objective_deadline = O.objective_deadline)
where I.person_invited_id = 2;

shows that the dbms (PostgreSQL) is using indexes throughout except for a sequential scan on event_invitations. I'm sure that's because I used very little data, so all these tables easily fit into RAM. (When tables fit in RAM, it's often faster to scan a small table than to use the index.)

The optimizer estimates the cost for each part of the query to be 0.00, and you can't get much better than that. Actual run time was less than 0.2 milliseconds, but that doesn't mean much.

I'm sure you can translate this into SQLAlchemy. Let me know if you want me to post my tables and sample data.

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