简体   繁体   English

如何从 Azure 表存储中查询最近的 n 条记录?

[英]How to query the most recent n records from Azure Table Storage?

I am using the following query in c# .net:我在 c# .net 中使用以下查询:

TableQuery<MyType> query = new TableQuery<MyType>().Take(50);

This works in grabbing 50 records, but I want to grab the most recent (descending) records from the table.这适用于抓取 50 条记录,但我想从表中抓取最新的(降序)记录。 I have looked at other answers, but none were concise or working.我看过其他答案,但没有一个是简洁的或有效​​的。

What is the most efficient way to grab n most recent records from an Azure Table Storage entity?从 Azure 表存储实体中获取n 个最新记录的最有效方法是什么? Please provide a concise answer with a working code example.请提供带有工作代码示例的简明答案。

Unfortunately there is no easy way to fetch latest records from Azure Table Storage as it does not allow you to sort the records on attributes.不幸的是,没有简单的方法可以从 Azure 表存储中获取最新记录,因为它不允许您根据属性对记录进行排序。 Records in Azure Table Storage are always sorted by PartitionKey and then by RowKey within a partition. Azure 表存储中的记录始终按PartitionKey排序,然后按分区内的RowKey排序。

One way to fix this problem is to specify the PartitionKey value as reverse chronological ticks (something like (DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks).ToString("d20") ) so that whenever you add an entity, it will be prepended (ie added to the top of the table).解决此问题的一种方法是将PartitionKey值指定为反向时间刻度(类似于(DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks).ToString("d20") ),这样无论何时添加实体,它都会放在前面(即添加到表格的顶部)。 Now when you do the query and fetch "x" number of records, you will always the most recent records.现在,当您执行查询并获取“x”条记录时,您将始终是最新的记录。

Other option is to fetch all entities and do the sorting on the client side.另一种选择是获取所有实体并在客户端进行排序。 This is highly inefficient and is generally not recommended.这是非常低效的,通常不推荐。 This solution might work if you have very small number of entities (less than 1000 for example) in your table.如果您的表中的实体数量非常少(例如少于 1000),则此解决方案可能会起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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