I want to check as fast as possible if an entity, based on PartitionKey and RowKey, exists in an Azure Table Storage.
I can do a standard TableOperation.Retrieve and check the result, like:
TableOperation retrieveOperation = TableOperation.Retrieve<CustomerEntity>("Smith", "Ben");
TableResult retrievedResult = table.Execute(retrieveOperation);
but that would return the complete entity.
By using a DynamicTableEntity I reduce the amount of data that is returned. More code, but I assume better...
public async Task<bool> EntityExists(string partitionKey, string rowKey)
{
var tableQuery = new TableQuery<DynamicTableEntity>();
tableQuery.FilterString = TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, paritionKey),
TableOperators.And,
TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, rowKey));
var dynamicTableEntities = await CloudTable.ExecuteQuerySegmentedAsync(tableQuery, null, TableRequestOptions, OperationContext);
return dynamicTableEntities.Results.Any();
}
Any suggestions/criticisms to my solution are welcome
2 Answers 2
If I remember correctly, accessing a row by PartitionKey
and RowKey
is built to be the fastest operation you can do on an Azure Table Storage. The performance drawback of using a DynamicTableEntity
probably isn't worth it unless the returned entity is really big.
I feel like you should stick to the Retrieve
, but there's no better way to be sure than to profile both your solutions for performance to see which is better with your data.
Because there's nothing else to say about your code, from what I know, you have the 2 existing solutions in your hand. Figure the best one now :)
You should try solve this using standard TableOperation.Retrieve, but this time specified columns argument to empty list ( new List() ) or a single column ("RowKey"). This will not return the entire entity.
-
1\$\begingroup\$ Welcome to code review. You can probably improve your answers and get points for your answers by reading this help page codereview.stackexchange.com/help/how-to-answer \$\endgroup\$2016年08月22日 11:39:44 +00:00Commented Aug 22, 2016 at 11:39
var dynamicTableEntities =...
because the type if not obvious from the right hand side of that expression. \$\endgroup\$