如何获取最新的低于 1000 个 WADLogsTable 条目?

How to fetch latest below 1000 WADLogsTable entry?

我已经编码从 wadlogstable 在 c# 中获取最新的诊断日志 但它正在遍历所有记录,然后提供最新条目 前任。 table 中有 5000 条记录,但我只想要最新或最后的 1000 条记录 但是它给出了所有记录,然后在按查询顺序给出了最后 1000 条记录,所以这非常耗时,它几乎需要 7-8 分钟才能完成 4000-5000 条记录

 CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ATCommon.DiagnosticConfig);
        CloudTableClient cloudTableClient = storageAccount.CreateCloudTableClient();
        TableServiceContext serviceContext = cloudTableClient.GetDataServiceContext();
        IQueryable<WadLogEntity> traceLogsTable = serviceContext.CreateQuery<WadLogEntity>("WADLogsTable");
        var selection = from row in traceLogsTable where row.PartitionKey.CompareTo("0" + DateTime.UtcNow.AddHours(hours).Ticks) >= 0 select row;
        //var selection = from row in traceLogsTable where row.PartitionKey.CompareTo("0" + DateTime.UtcNow.AddMinutes(-5.0).Ticks) >= 0 select row;
        CloudTableQuery<WadLogEntity> query = selection.AsTableServiceQuery<WadLogEntity>();
        IEnumerable<WadLogEntity> output = query.Execute();
return output.OrderByDescending(s => s.Timestamp).ToList();

您可以尝试通过技巧来实现它,使用 DateTime.MaxValue.Ticks – DateTime.UtcNow.Ticks 的 RowKey 值允许您按偏移时间对项目进行排序从最新的项目到旧的项目。通过这样做,您可以按正确顺序或最近添加的项目首先检索结果,然后使用 .Take(1000) 参数将结果限制为最近的 1000 行. 查看 this blog.

中的详细信息

我有超过 5 亿的条目。每一秒它都在增加更多..

string apiLogTableName = "yourtableName";
StorageTable apiLogsTable = new StorageTable(apiLogTableName);

 string filter1 = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, date);   //hear you can check with ticks


 string filter2 = TableQuery.GenerateFilterConditionForInt("yourColumn", QueryComparisons.Equal, yourValue);

            string filter3 = TableQuery.GenerateFilterCondition("YourColumn2", QueryComparisons.NotEqual, "YourValue2");



            TableQuery<ApiCallLogEntity> findapiLogsRecord = new TableQuery<ApiCallLogEntity>().Where(TableQuery.CombineFilters(

                TableQuery.CombineFilters(
                            filter1,
                            TableOperators.And,
                            filter2),
                TableOperators.And, filter3));

//you can use
//var records= apiLogsTable._table.ExecuteQuery(findapiLogsRecord)


//bellow code will get one-one records // time consuming
            foreach (ApiCallLogEntity entity in apiLogsTable._table.ExecuteQuery(findapiLogsRecord))
            {
                Console.WriteLine("{0}, {1}\t{2}\t{3}", entity.PartitionKey, entity.RowKey,
                    entity.Action, entity.RequestBody);


                tw.WriteLine("{0}, {1}\t{2}\t{3}\t{4}", entity.PartitionKey, entity.RowKey,
                    entity.Action, entity.RequestBody, entity.ResponseBody);
            }