【发布时间】:2018-10-13 12:35:49
【问题描述】:
当前设计
Table: 1_notes
------------------------------------------
| id | text | created_at |
------------------------------------------
| 1_1 | u1 first note | 2018-01-01 10:00:00 |
| 1_2 | u1 second note | 2018-01-03 10:00:00 |
Table: 1_note_timeline
---------------------------------------------------------------------
| note_id | note_created_at | likes_count | created_at |
---------------------------------------------------------------------
| 1_1 | 2018-01-01 10:00:00 | 10 | 2018-01-01 10:00:00 |
| 1_1 | 2018-01-01 10:00:00 | 20 | 2018-01-02 10:00:00 |
| 1_2 | 2018-01-03 10:00:00 | 10 | 2018-01-03 10:00:00 |
| 1_1 | 2018-01-01 10:00:00 | 15 | 2018-01-03 10:00:00 |
Table: 2_notes
--------------------------------------------
| id | text | created_at |
--------------------------------------------
| 2_1 | u2 first note | 2018-01-01 10:00:00 |
| 2_2 | u2 second note | 2018-01-03 10:00:00 |
Table: 2_note_timeline
---------------------------------------------------------------------
| note_id | note_created_at | likes_count | created_at |
---------------------------------------------------------------------
| 2_1 | 2018-01-01 10:00:00 | 10 | 2018-01-01 10:00:00 |
| 2_1 | 2018-01-01 10:00:00 | 20 | 2018-01-02 10:00:00 |
| 2_2 | 2018-01-03 10:00:00 | 10 | 2018-01-03 10:00:00 |
| 2_1 | 2018-01-01 10:00:00 | 15 | 2018-01-03 10:00:00 |
对于每个用户,他们的笔记数据有 2 个表。
- {{userId}}_notes 表包含 ID 为
userId的用户的注释 - {{userId}}_note_timeline 表每天跟踪笔记 likes_count 数据
要求的结果应该有:
- 点赞最多的两个用户的前 2 个注释
- 点赞数必须是最新的(而不是 MAX_VALUE,因为点赞数会随时间减少)
最终输出
Output
---------------------------------------------------------------
| note_id | note_created_at | likes_count | text |
---------------------------------------------------------------
| 1_1 | 2018-01-01 10:00:00 | 15 | u1 first note |
| 2_1 | 2018-01-01 10:00:00 | 15 | u2 first note |
【问题讨论】:
-
将不同的用户数据存储在不同的表中绝对不是结构化数据的最佳方式。您应该为 所有 个用户提供一组表。
-
@GordonLinoff 这样做会大大增加成本。因为每个用户都有数百万条笔记和数百万条note_timeline。而且我一次只需要查询最多 4 个用户。
-
@GordonLinoff BigQuery 允许按摄取时间或 TIMESTAMP/DATE 列 (cloud.google.com/bigquery/docs/partitioned-tables) 进行分区。因此,根据您的建议,我将无法按用户 ID 对其进行分区。我当前设计的好处是我可以按月对每个用户笔记进行分区。
-
@GordonLinoff 为什么你删除了你的评论?请不要这样做,未来的用户将无法理解上下文。除此之外,BigQuery 还提供了一种自动构建这些相同架构表的方法 (cloud.google.com/bigquery/…)
-
@user2579651 。 . .我认为 BigQuery 已经实现了该功能。在改进的分区发布之前,我的评论只是误导。
标签: sql google-cloud-platform google-bigquery