我编写了从c#中的wadlogstable获取最新诊断日志的代码,但是它遍历了所有记录,然后给出了最新的条目ex。表中有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();发布于 2016-03-15 10:41:31
我有超过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);
}发布于 2016-03-04 07:12:28
您可以尝试通过技巧来实现它,使用RowKey值DateTime.MaxValue.Ticks - DateTime.UtcNow.Ticks,允许您按照从最新项目到旧项目的偏移时间对项目进行排序。通过这样做,您可以按照正确的顺序或最近添加的项检索结果,然后使用.Take( 1000 )参数将结果限制在最近的1000行。查看这个博客中的详细信息。
https://stackoverflow.com/questions/35721724
复制相似问题