为每个 "Group" 选择最新文档
Selecting the latest document for each "Group"
我正在使用 Azure Cosmos DB SQL API 来尝试实现以下目标;
我们将设备数据存储在一个集合中,希望能够有效地检索每个设备序列号的最新事件数据,而无需执行N个分别查询每个设备。
SELECT *
FROM c
WHERE c.serial IN ('V55555555','synap-aim-g1') ORDER BY c.EventEnqueuedUtcTime DESC
我假设我需要使用分组依据 - https://docs.microsoft.com/en-us/azure/cosmos-db/sql-query-group-by
如有任何帮助,我们将不胜感激
粗略的数据示例:
[
{
"temperature": 25.22063251827873,
"humidity": 71.54208429695204,
"serial": "V55555555",
"testid": 1,
"location": {
"type": "Point",
"coordinates": [
30.843687,
-29.789895
]
},
"EventProcessedUtcTime": "2020-09-07T12:04:34.5861918Z",
"PartitionId": 0,
"EventEnqueuedUtcTime": "2020-09-07T12:04:34.4700000Z",
"IoTHub": {
"MessageId": null,
"CorrelationId": null,
"ConnectionDeviceId": "V55555555",
"ConnectionDeviceGenerationId": "637323979596346475",
"EnqueuedTime": "2020-09-07T12:04:34.0000000"
},
"Name": "admin",
"id": "6dac491e-1f28-450d-bf97-3a15a0efaad8",
"_rid": "i2UhAI7ofAo3AQAAAAAAAA==",
"_self": "dbs/i2UhAA==/colls/i2UhAI7ofAo=/docs/i2UhAI7ofAo3AQAAAAAAAA==/",
"_etag": "\"430131c1-0000-0100-0000-5f5621d80000\"",
"_attachments": "attachments/",
"_ts": 1599480280
}
]
更新:
因此,执行以下 return 是正确的数据,但遗憾的是,您只能 return 分组内的数据或聚合函数(即不能执行 select *)
SELECT c.serial, MAX(c.EventProcessedUtcTime)
FROM c
WHERE c.serial IN ('V55555555','synap-aim-g1')
GROUP BY c.serial
[
{
"serial": "synap-aim-g1",
"": "2020-09-09T06:29:42.6812629Z"
},
{
"serial": "V55555555",
"": "2020-09-07T12:04:34.5861918Z"
}
]
感谢@AnuragSharma-MSFT的帮助:
- 恐怕没有直接的方法可以使用查询来实现它
宇宙数据库但是,对于同一主题,您可以参考下面的 link。如果
您正在使用任何 sdk,这将有助于实现所需的
功能:https://docs.microsoft.com/en-us/answers/questions/38454/index.html
我们很高兴您以这种方式解决了问题,感谢您分享更新:
因此,执行以下 return 是正确的数据,但遗憾的是,您只能 return 组内的数据或聚合函数(即不能 select *)
SELECT c.serial, MAX(c.EventProcessedUtcTime)
FROM c
WHERE c.serial IN ('V55555555','synap-aim-g1')
GROUP BY c.serial
[
{
"serial": "synap-aim-g1",
"": "2020-09-09T06:29:42.6812629Z"
},
{
"serial": "V55555555",
"": "2020-09-07T12:04:34.5861918Z"
}
]
如果问题真的是关于这种特定查询场景的有效方法,我们可以在查询语言本身不能提供有效解决方案的情况下考虑非规范化。 partitioning and modeling 上的本指南有一个关于获取 Feed 中的最新项目的相关部分。
We just need to get the 100 most recent posts, without the need to
paginate through the entire data set.
So to optimize this last request, we introduce a third container to
our design, entirely dedicated to serving this request. We denormalize
our posts to that new feed container.
按照这种方法,您可以创建一个专用于“最新”查询的“Feed”或“LatestEvent”容器,它使用设备序列号 id
并具有单个分区键以保证每个设备只有一个(最新的)事件项,它可以通过设备序列号获取或使用简单查询以尽可能低的成本列出:
SELECT *
FROM c
WHERE c.serial IN ('V55555555','synap-aim-g1')
更改提要可用于 upsert 最新事件,这样最新事件是 created/overwritten 在“LatestEvent”容器中,因为它的源项目是在主容器中创建的。
我正在使用 Azure Cosmos DB SQL API 来尝试实现以下目标;
我们将设备数据存储在一个集合中,希望能够有效地检索每个设备序列号的最新事件数据,而无需执行N个分别查询每个设备。
SELECT *
FROM c
WHERE c.serial IN ('V55555555','synap-aim-g1') ORDER BY c.EventEnqueuedUtcTime DESC
我假设我需要使用分组依据 - https://docs.microsoft.com/en-us/azure/cosmos-db/sql-query-group-by
如有任何帮助,我们将不胜感激
粗略的数据示例:
[
{
"temperature": 25.22063251827873,
"humidity": 71.54208429695204,
"serial": "V55555555",
"testid": 1,
"location": {
"type": "Point",
"coordinates": [
30.843687,
-29.789895
]
},
"EventProcessedUtcTime": "2020-09-07T12:04:34.5861918Z",
"PartitionId": 0,
"EventEnqueuedUtcTime": "2020-09-07T12:04:34.4700000Z",
"IoTHub": {
"MessageId": null,
"CorrelationId": null,
"ConnectionDeviceId": "V55555555",
"ConnectionDeviceGenerationId": "637323979596346475",
"EnqueuedTime": "2020-09-07T12:04:34.0000000"
},
"Name": "admin",
"id": "6dac491e-1f28-450d-bf97-3a15a0efaad8",
"_rid": "i2UhAI7ofAo3AQAAAAAAAA==",
"_self": "dbs/i2UhAA==/colls/i2UhAI7ofAo=/docs/i2UhAI7ofAo3AQAAAAAAAA==/",
"_etag": "\"430131c1-0000-0100-0000-5f5621d80000\"",
"_attachments": "attachments/",
"_ts": 1599480280
}
]
更新: 因此,执行以下 return 是正确的数据,但遗憾的是,您只能 return 分组内的数据或聚合函数(即不能执行 select *)
SELECT c.serial, MAX(c.EventProcessedUtcTime)
FROM c
WHERE c.serial IN ('V55555555','synap-aim-g1')
GROUP BY c.serial
[
{
"serial": "synap-aim-g1",
"": "2020-09-09T06:29:42.6812629Z"
},
{
"serial": "V55555555",
"": "2020-09-07T12:04:34.5861918Z"
}
]
感谢@AnuragSharma-MSFT的帮助:
- 恐怕没有直接的方法可以使用查询来实现它 宇宙数据库但是,对于同一主题,您可以参考下面的 link。如果 您正在使用任何 sdk,这将有助于实现所需的 功能:https://docs.microsoft.com/en-us/answers/questions/38454/index.html
我们很高兴您以这种方式解决了问题,感谢您分享更新:
因此,执行以下 return 是正确的数据,但遗憾的是,您只能 return 组内的数据或聚合函数(即不能 select *)
SELECT c.serial, MAX(c.EventProcessedUtcTime)
FROM c
WHERE c.serial IN ('V55555555','synap-aim-g1')
GROUP BY c.serial
[
{
"serial": "synap-aim-g1",
"": "2020-09-09T06:29:42.6812629Z"
},
{
"serial": "V55555555",
"": "2020-09-07T12:04:34.5861918Z"
}
]
如果问题真的是关于这种特定查询场景的有效方法,我们可以在查询语言本身不能提供有效解决方案的情况下考虑非规范化。 partitioning and modeling 上的本指南有一个关于获取 Feed 中的最新项目的相关部分。
We just need to get the 100 most recent posts, without the need to paginate through the entire data set.
So to optimize this last request, we introduce a third container to our design, entirely dedicated to serving this request. We denormalize our posts to that new feed container.
按照这种方法,您可以创建一个专用于“最新”查询的“Feed”或“LatestEvent”容器,它使用设备序列号 id
并具有单个分区键以保证每个设备只有一个(最新的)事件项,它可以通过设备序列号获取或使用简单查询以尽可能低的成本列出:
SELECT *
FROM c
WHERE c.serial IN ('V55555555','synap-aim-g1')
更改提要可用于 upsert 最新事件,这样最新事件是 created/overwritten 在“LatestEvent”容器中,因为它的源项目是在主容器中创建的。