简体   繁体   中英

How to retrieve latest record from Azure table storage using C#?

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()

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM