2016-03-01 98 views
1

我已经编写了从c#中的wadlogstable获取最新的诊断日志,但是它遍历所有记录,然后给出最新的条目 前。表格中有5000条记录,但我只想要最近或最近的1000条记录 ,但它在给出所有记录后,通过查询给出最后的1000条记录,所以非常耗时,fecth需要将近7-8分钟4000-5000记录如何获取最新的低于1000 WADLogsTable条目?

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(); 
+0

什么是您的分区密钥?你可以提供样本数据吗? – Mahesh

+0

@Mahesh分区键是用于过滤目的,以及我在这里共享的整个代码。 –

+0

那就是我想说的。 azure表不支持排序,所有使用分区键您需要编写逻辑以获取最新的100分区键作为索引在您的表中,因此查询将运行速度快。使用分区键过滤器,你需要像先写逻辑只获得最后2小时记录,如果你在这个查询中只有60个,再试一次,然后填写你的清单,然后按它排序,只需100 – Mahesh

回答

1

我有超过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); 
      } 
1

您可以尝试通过招实现它,使用DateTime.MaxValue.Ticks一个RowKey值 - DateTime.UtcNow.Ticks,让您从最新的抵消时间对项目进行排序项目到较旧的项目。通过这样做,您可以按照正确的顺序或最近添加的项目检索结果,然后使用.Take(1000)参数将结果限制为最近的1000行。 查询详情this blog

+0

我已经在使用ticks的东西,但是,如果你在存储表中有12000条记录,首先它将获取所有那么我们可以做任何类型的过滤,而我不想 –

+0

@AshokDhakhada:如果你过滤表,它将只提取过滤数据,而不是所有数据。只是事情是过滤器应该在分区键,所以它不会去扔整个表来搜索正确的数据。它会很快。我张贴我的代码下面 – Mahesh