Search code examples
azureazure-table-storageazure-tablequery

Azure Table Storage Query For Unique Partition Key


My problem is as follows, I have an Azure Table Storage that is set up like this:

  • Partition Key : string
  • Row Key : number

I want to make a query such that given a number X, I want to return the highest number Row Key that is less than X for each Partition Key. That is kind of confusingly worded, so here is an example:

Partition Key, Row Key

"hello", 7

"hello", 9

"hello", 12

"other", 6

If I queried with the number 10, I want to return a list like [("hello", 9), ("other", 6)]. Note that I want only unique partition keys and the highest number that is less than 10.

I can do this for a particular name with something like (leaving some stuff out for brevity):

    string orderFilter = TableQuery.GenerateFilterConditionForLong("RowKey", "le", 10);
    string nameFilter = TableQuery.GenerateFilterCondition("PartitionKey", "eq", "hello");

    TableQuery query = new TableQuery().Where(TableQuery.CombineFilters(orderFilter, "and", nameFilter));

    IEnumerable<NameEntity> names = await nameTable.ExecuteQuerySegmentedAsync<NameEntity>(query, entityResolver, continuationToken);

    // Get newest name
    NameEntity newestName = names.Last();

But I can't seem to figure out how to do this with all the different Partition Keys. Is this possible in a single query? Thanks for any help!


Solution

  • There is no way to enumerate partition keys (without doing a table scan), so you can either:

    • do a full table scan, keeping track of various partition keys you find along the way, along with highest-value row key
    • keep track of your partition keys as you write them to table storage (e.g. create another table with just your partition keys).

    With the latter, the other answer suggests writing the max-value for that partition key, at the same time. Certainly viable. But you'll need to remember that you need to maintain this bit of metadata.