16
votes

I have an azure table with about 5 million+ rows, each partition about 100k rows. The RowKey is a sortable timestamp. I need to be able to get the latest record that was inserted in the table.

Since Linq Last, Max, OrderBy etc are not supported, how do I efficiently get the most recent table record?

3

3 Answers

15
votes

There's no efficient way to do this. But if it's an option, consider flipping your row key so that the newest entries are on the top (reverse chronological order). Retrieving the top n entries is easy and efficient.

2
votes

You can query for TimeStamp smaller than MaxDate and limit the result for 1. Haven't tested it yet but it should work. Depends on how is your time stamp formatted.

0
votes

Log tail pattern section of Microsoft's Table design patterns specifically addresses this:

Context and problem

A common requirement is been able to retrieve the most recently created entities, for example the 10 most recent expense claims submitted by an employee. Table queries support a $top query operation to return the first n entities from a set: there is no equivalent query operation to return the last n entities in a set.

(...)

The following C# code sample shows one way to create a suitable "inverted ticks" value for a RowKey that sorts from the most recent to the oldest:

string invertedTicks = string.Format("{0:D19}", DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks);

(...)