【问题标题】:Parallel Querying Azure Storage并行查询 Azure 存储
【发布时间】:2017-06-27 21:58:59
【问题描述】:

我目前有一个查询如下:

TableQuery<CloudTableEntity> query = new TableQuery<CloudTableEntity().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, PK));

foreach (CloudTableEntity entity in table.ExecuteQuery(query))
{
    //Logic
}

我一直在研究并行,但是,我找不到任何关于如何使用它的好的代码示例。我希望能够查询数以千计的分区键,例如

CloudTableEntity().Where(PartitionKey == "11" || PartitionKey == "22")

我可以拥有大约 40000 个分区键。有什么好办法吗?

【问题讨论】:

标签: c# azure azure-storage azure-table-storage


【解决方案1】:

以下示例代码将并行发出多个分区键查询:

     CloudTable table = tableClient.GetTableReference("xyztable");
     List<string> pkList = new List<string>(); // Partition keys to query
     pkList.Add("1");
     pkList.Add("2");
     pkList.Add("3");
     Parallel.ForEach(
        pkList,
        //new ParallelOptions { MaxDegreeOfParallelism = 128 }, // optional: limit threads
        pk => { ProcessQuery(table, pk); }
     );

其中 ProcessQuery 定义为:

  static void ProcessQuery(CloudTable table, string pk)
  {
     string pkFilter = TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, pk);
     TableQuery<TableEntity> query = new TableQuery<TableEntity>().Where(pkFilter);
     var list = table.ExecuteQuery(query).ToList();
     foreach (TableEntity entity in list)
     {
        // Process Entities
     }
  }

请注意,在上面列出的同一查询中对两个分区键进行 ORing 将导致全表扫描。如上面的示例代码所示,为避免全表扫描,每个查询使用一个分区键执行单个查询。

关于查询构造的更多详细信息,请参阅http://blogs.msdn.com/b/windowsazurestorage/archive/2010/11/06/how-to-get-most-out-of-windows-azure-tables.aspx

【讨论】:

    【解决方案2】:

    使用 table.ExecuteQuerySegmentedAsync 将提供更好的性能

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-07
      • 1970-01-01
      • 2016-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-18
      • 2021-09-07
      相关资源
      最近更新 更多