Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can PartitionKey be queried with StartsWith?

In Azure Table Storage, is it possible to query PartitionKey with a StartsWith or some other operator e.g. Contains, etc.

I know I can do this with RowKeys but is it possible to do it with PartitionKeys?

A follow up question is: even if it's doable, is it advisable? Should PartitionKey always be an exact match -- say, for performance reasons?

like image 450
Sam Avatar asked Mar 04 '16 19:03

Sam


2 Answers

Here's an example using the GreaterThanOrEqual and LessThan operators, as an extension on the target column name.

The filter combines two parts:

  • anything greater than or equal to your startsWith prefix,
  • increment the last character of your prefix and find anything less than this.

For example, startsWith prefix "CAR" will prepare something like s >= "CAR" && s < "CAS".

    public static string GetStartsWithFilter(this string columnName, string startsWith)
    {
        var length = startsWith.Length - 1;
        var nextChar = startsWith[length] + 1;

        var startWithEnd = startsWith.Substring(0, length) + (char)nextChar;
        var filter = TableQuery.CombineFilters(
            TableQuery.GenerateFilterCondition(columnName, QueryComparisons.GreaterThanOrEqual, startsWith),
            TableOperators.And,
            TableQuery.GenerateFilterCondition(columnName, QueryComparisons.LessThan, startWithEnd));

        return filter;
    }

Usage:

var query = new TableQuery<MyTableEntity>().Where(myColumnName.GetStartsWithFilter(prefix));

Based on Alexandre B's blog post

like image 122
richaux Avatar answered Nov 11 '22 22:11

richaux


In Azure Table Storage, is it possible to query PartitionKey with a StartsWith or some other operator e.g. Contains, etc.

No, it is not possible to do queries using StartsWith or Contains query operator with Azure Tables. To simulate StartsWith, you would need to use the combination of Greater Than Equal To and Less Than operators. You can't use Contains operator. What you would need to do is first fetch all the data on the client side and then use Contains to filter the data on the client side only.

For a list of supported query operators, please see this link: https://msdn.microsoft.com/en-us/library/azure/dd135725.aspx.

I know I can do this with RowKeys but is it possible to do it with PartitionKeys?

I don't think it is possible. I'm curious to know why you're saying this.

A follow up question is: even if it's doable, is it advisable? Should PartitionKey always be an exact match -- say, for performance reasons?

I would highly recommend reading this excellent guide for this: https://azure.microsoft.com/en-in/documentation/articles/storage-table-design-guide/.

like image 2
Gaurav Mantri Avatar answered Nov 11 '22 22:11

Gaurav Mantri