2014-08-29 84 views
2

我现在有一个queury沿的线条看起来:并行查询Azure存储

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左右分区键。有没有一个好的方法来做到这一点?

+0

看到这个答案http://stackoverflow.com/a/3878378/142904 – 2014-09-23 14:55:42

回答

2

下面的示例代码将同时发出多个分区键查询:

 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 
    } 
    } 

注意,或运算两个分隔键在相同的查询,你上市以上将导致全表扫描。要避免全表扫描,请按照上面的示例代码所示,对每个查询使用一个分区键执行单个查询。

有关查询更多建设详见http://blogs.msdn.com/b/windowsazurestorage/archive/2010/11/06/how-to-get-most-out-of-windows-azure-tables.aspx

0

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