2014-08-29 73 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將提供更好的性能