I have a Cosmos DB collection with numerous sections based on device ID. I often have use cases that require getting the most recent document using a specific device identifier. I am currently using the SELECT TOP 1 functions available in the DocumentDB API, as shown below, to do the following:
SELECT TOP 1 * FROM c WHERE c.deviceId = 5 ORDER BY c.timeStamp DESC
This approach leads to an increase in RU / s consumption and a decrease in productivity as the size of the collection and individual sections grows, as one would expect. As a temporary way to fix this problem, I added additional suggestions that limit the scope of the request by timestamp:
SELECT TOP 1 * FROM c WHERE c.deviceId = 5 AND c.timeStamp >= 1506608558
I would like to know if there is a better way to select the last document by section ID, since adding this where clause may lead to unexpected or missing results.
source share