1

I'm facing an issue with azure table storage. I have hundreds of thousands of data into the storage table that I need to query.

  1. First approach is to retrieve all the data and then queried as per the requirement but its taking too much time.

  2. Second approach is what if I get filtered data from table storage directly using query

So as per my understanding second approach is best but I am not able to query appropriately. How can I retrieve the last entry from Azure table storage, in this I am also trying to MAX and order by function but its not work for me.

Not running code:

var query2 = new TableQuery().Where(
TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, patientId.ToString()),
TableOperators.And,
TableQuery.GenerateFilterConditionForGuid("DeviceID", QueryComparisons.Equal, deviceId))
).OrderByDescending(x=>x.EventDate).Take(1).Select(x=>x.EventDate).ToList();

Running code which taking too much time:

var query = new TableQuery<TherapyEvent>().Where( 
TableQuery.CombineFilters( 
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, patientId.ToString()), 
TableOperators.And, 
TableQuery.GenerateFilterConditionForGuid("DeviceID", QueryComparisons.Equal, deviceId)) ); 

var resp= _table.ExecuteQuery(query).OrderByDescending(x=>x.EventDate).Take(1).Select(x=>x.EventDate).ToList()
James Z
  • 12,209
  • 10
  • 24
  • 44
  • you can try adding TimeStamp as part of your filters too in order to get the last one. – Thiago Custodio Dec 06 '19 at 15:35
  • not possible now add time stamp in row key,because data already exists, and it has large amount data, i can't change existing data because it already use by some software. – Ravinder bachhal Dec 06 '19 at 17:27
  • TimeStamp is a property created automatically whenever you add new data. The data is already there, just consider it to retrieve the latest one – Thiago Custodio Dec 06 '19 at 17:45

1 Answers1

2

As of now there is no direct way of fetching the latest record using the RowKey property. You may, however, shift the RowKey and list it in reverse chronological order and then fetch the top most entry which would be the latest.

You can check this blog for further details:

http://blog.smarx.com/posts/using-numbers-as-keys-in-windows-azure

Additional reference:

How to retrieve latest record using RowKey or Timestamp in Azure Table storage

Note: Using a string date time for the row key is not a good approach as Table Storage stores entities in ascending order based on the Row Key.

Hope it helps.

Mohit Verma
  • 5,140
  • 2
  • 12
  • 27
  • hey mohit, already saw that solution but in my case not possible to change row key as datetime in table storage,because its already have lac's of data, Is there any solution?? – Ravinder bachhal Dec 06 '19 at 17:24