Querying PartitionKey and RowKey syntax in Azure Table Storage service - azure

Meanwhile reading querying tables and entities in Azure Table Storage service on Microsoft docs I found PartitionKey and RowKeys can be filtered in 2 different ways in the myaccount.table.core.windows.net URL as the following:
https://<service-url>/Customers(PartitionKey='MyPartition',RowKey='MyRowKey1')
And using $filter to achieve the same:
https://<service-url>/Customers()?$filter=PartitionKey%20eq%20'MyPartitionKey'%20and%20RowKey%20eq%20'MyRowKey1'
I understand from the documentation PartitionKey and RowKey properties are forming the entity's primary key so the first syntax can be used as well as the Filtering on the PartitionKey and RowKey Properties part states:
Because the PartitionKey and RowKey properties form an entity's primary key, you can use a special syntax to identify the entity.
Questions:
Is there any drawbacks, disadvantages using $filter instead of that special syntax what has been mentioned?
Does it matter which one I use in my solution?
Any clarification is appreciated, thank you!

Interesing question! So I tried to perform both the operations to fetch an entity using Postman and the time it took to get the data was almost identical (between 250ms - 300ms).
So as long as you're using REST API, I don't think it would matter.
When you use the SDK (WindowsAzure.Storage version 9.3.3 for example), there're different methods that uses these features. Take a look at the sample code below:
var account = new CloudStorageAccount(new StorageCredentials(accountName, accountKey), true);
var tableClient = account.CreateCloudTableClient();
var table = tableClient.GetTableReference("TableName");
//This uses https://account.table.core.windows.net/table(PartitionKey='pk', RowKey='rk')
TableOperation op = TableOperation.Retrieve("pk", "rk");
var entity = table.Execute(op).Result as DynamicTableEntity;
//This uses https://account.table.core.windows.net/table?$filter=PartitionKey eq 'pk' and RowKey eq 'rk'
TableQuery query = new TableQuery();
query.FilterString = "PartitionKey eq 'pk' and RowKey eq 'rk'";
var entity = table.ExecuteQuery(query).FirstOrDefault();

Related

How to apply filter in Azure table client

I am working on Azure storage table. Below is the partitionKey and rowkey
partitionKey = Product Type
rowKey = Region
I have used below filter to get the data (see the code below). If rowkey (region) is null then it should get product Type from all region. However, It get empty data. If I pass valid Partitionkey(ProductType) and RowKey (region) it's get the data.
AsyncPageable<T> result =TableClient.QueryAsync<T>(filter: $"PartitionKey eq '{partitionKey}' and RowKey eq '{rowKey}'");
Note: Partition-key is mandatory
Any advice.
Thanks
If rowkey (region) is null then it should get product Type from all
region.
If this is what you want, you would need to change the code to something like following:
AsyncPageable<T> result = TableClient.QueryAsync<T>(filter: $"PartitionKey eq '{partitionKey}'");
UPDATE
What I would like is If user has provided ProductType and Region then
filter using both. In case user only provide ProductType then include
all regions.
In this case, you will need to use two different queries - one when both PartitionKey and RowKey are present and other when only PartitionKey is present.
Your code would be something like:
var query = string.IsNullOrWhiteSpace(rowKey) ?
$"PartitionKey eq '{partitionKey}'" : $"PartitionKey eq '{partitionKey}' and RowKey eq '{rowKey}'";
AsyncPageable<T> result = TableClient.QueryAsync<T>(filter: query);

Azure table query performance with partition key, rowkey and non-index

I have a table query below that search by PK, RK and a non-index key.
Will this slow down database search? Will the search by DOB be faster if it performs on web layer via c#?
var name = "John";
var wins = 20;
var dob = DateTimeOffset.Parse("1999-1-1");
string usernameFilter2 =
TableQuery.CombineFilters(
TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, name),
TableOperators.And,
TableQuery.GenerateFilterConditionForInt("RowKey", QueryComparisons.Equal, wins)),
TableOperators.And,
TableQuery.GenerateFilterConditionForDate("DateOfBirth", QueryComparisons.GreaterThanOrEqual, dob));
Considering there can be a single entity for a PartitionKey/RowKey combination, I believe your query's 3rd filter criteria is redundant (assuming you've meant to include another attribute there instead of RowKey).
As far as the speed of the query goes, again considering you're searching for an entity with matching PartitionKey and RowKey, this query will be the fastest. If you remove RowKey from your query and search on PartitionKey and any other non-indexed attribute, it will be slower as the query will be doing a partition scan to find a matching entity.
You may find this article helpful: https://learn.microsoft.com/en-us/azure/cosmos-db/table-storage-design-guide.

cosmos db document query takes long time

i am new to cosmos-db and facing issues in querying the collection, i have a partitioned collection with 100000 RU/s(unlimited storage capacity). the partition is based on '/Bid' which a GUID. i am querying the collection based on the partition key value which has 10,000 records (the collection has more than 28,942,445 documents for different partitions). i am using the following query to get the documents but it takes around 50 seconds to execute the query which is not feasible.
object partitionkey = new object();
partitionkey = "2359c59a-f730-40df-865c-d4e161189f5b";
// Now execute the same query via direct SQL
var DistinctBColumn = this.client.CreateDocumentQuery<BColumn>(BordereauxColumnCollection.SelfLink, "SELECT * FROM BColumn_UL c WHERE c.BId = '2359c59a-f730-40df-865c-d4e161189f5b'",new FeedOptions { EnableCrossPartitionQuery=true, PartitionKey= new PartitionKey("2359c59a-f730-40df-865c-d4e161189f5b") }, partitionkey).ToList();
also tried with other querying options which too resulted in talking along 50 seconds.
But it takes less than a second for the same query on azure portal.
kindly help to optimize the query and correct me if i am wrong. Many Thanks.

azure table storage partitionkey/row key background?

I'm reviewing Azure tables in an existing implementation. Here's an example of data from 1 row:
partitionkey (string):
6b348096-e6cb-4126-ba3c-cd0c9e8ba9c9
rowkey (string):
02519452888782521547_c1a98e0f-1b25-4d38-bd96-d72b30a97bf0
Obviously, rowkey does not have a proper guid and both column names are native to Azure and required per entity. There does not appear to be an identity or default insert for these columns. Can someone please provide context around these columns and the implementation style differences and considerations between these Azure columns vs a SQL Server style implementation?
I have no idea about how the partition key and row key are constructed in your table, please turn to someone who created the table. :)
About the considerations on the Azure Table design, you can refer to this post (which is very complete and helpful).
Partitionkey and Rowkey are just two properties of entitis in Azure table. Rowkey is the "primary key" within one partition. Within one PartitionKey, you can only have unique RowKeys. If you use multiple partitions, the same RowKey can be reused in every partition. PartitionKey + RowKey form the unique identifier(Primary key) for an entity.
In your table, the partitionkey and rowkey are just assigned with a random string. I'm not sure whether you designed this table or somebody else, but these two properties can be assigned with other values through Azure Storage .NET client libary and Rest API. As the example below, you can design the rowkey and partitionkey, and assign whatever valid value you want, here lastname for Partitionkey and firstname for rowkey:
public class CustomerEntity : TableEntity
{
public CustomerEntity(string lastName, string firstName)
{
this.PartitionKey = lastName;
this.RowKey = firstName;
}
public CustomerEntity() { }
public string Email { get; set; }
public string PhoneNumber { get; set; }
}
It’s better to think about both properties and your partitioning strategy. Don’t just assign them a guid or a random string as it does matter for performance. I recommend you go through Designing a Scalable Partitioning Strategy for Azure Table Storage, the most commom used is Range Partitions, but you can choose whatever you want.
This is a great blog help you understand how partitionkey and rowkey work http://blog.maartenballiauw.be/post/2012/10/08/What-PartitionKey-and-RowKey-are-for-in-Windows-Azure-Table-Storage.aspx

How to Compare only date in Linq to Entity with Dynamic Query API?

I have downloaded Microsoft Dynamic Query API. And using the dynamic query to filter the data using dates. I have written following query :-
Entities db = new Entities();
DateTime d = new DateTime(2014, 1, 17);
var lst = db.MSTPriorityS.Where("ModifiedOn == #0", d.Date.ToString()).ToList();
The result count, i am getting is 0. While there is data in the database table.
Please advise what i am doing wrong?
i think the problem is where you cast DateTime to String probably,
you can create your query step by step, and type safe, follow 'Creating dynamic queries with entity framework'
you can use lambda expression instead: var lst = db.MSTPriorityS.Where(u => u.ModifiedOn == System.Data.Objects.EntityFunctions.TruncateTime(d))

Resources