按时间戳间隔从 Azure 存储中获取记录的问题

Issues with getting records from azure storage by timestamp interval

我在按时间戳间隔从 Azure 存储中获取记录时遇到问题。这是我的查询。

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("ConnectionString"));

CloudTableClient tableClient = storageAccount.CreateCloudTableClient();
CloudTable table = tableClient.GetTableReference("TableName");

string fromDate = TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.GreaterThanOrEqual, fromDateTime);
string toDate = TableQuery.GenerateFilterConditionForDate("Timestamp", QueryComparisons.LessThanOrEqual, toDateTime);
string anotherValue = TableQuery.GenerateFilterCondition("AnotherValue", QueryComparisons.Equal, "Value");
string timeCombine = TableQuery.CombineFilters(fromDate, TableOperators.And, toDate);

TableQuery<StorageSPS> query = new TableQuery<StorageSPS>().Where(TableQuery.CombineFilters(anotherValue, TableOperators.And, timeCombine));
var data = (await (table.ExecuteQuerySegmentedAsync(query, new TableContinuationToken(), System.Threading.CancellationToken.None))).Results;

执行查询后,我没有得到任何记录。有什么,我做错了吗?有人可以告诉我我做错了什么吗?

非常感谢您的帮助。

ExecuteQuerySegmentedAsync returns a Task of TableQuerySegment, which contains both the results of this call and a continuation token to query the next set of results. By design, a single ExecuteQuerySegmentedAsync call can return 0 entities (see Query Timeout and Pagination),但客户端可以使用提供的延续令牌再次调用它。例如;

int count = 0;
do
{
    TableQuerySegment<DynamicTableEntity> querySegment =
        await currentTable.ExecuteQuerySegmentedAsync(query, token);
    token = querySegment.ContinuationToken;

    foreach (DynamicTableEntity entity in querySegment)
    {
        ++count;
    }
}
while (token != null);

正如Serdar在另一条评论中所说,返回的分段结果可能为空,您需要使用返回的ContinuationToken再次发出查询请求,直到返回的ContinuationToken为null。

我想提的另一件事是,请尽可能避免时间戳间隔查询。这样的查询将导致服务器端 whole table scan。如果您的场景通常需要时间戳间隔查询,请考虑选择时间戳作为您的分区键或行键以优化查询性能。

分区键和行键设计请参考以下帖子table:

How to get most out of Windows Azure Tables

Azure Storage Table Design Guide: Designing Scalable and Performant Tables