2012-06-04 46 views
1

我是sql azure中的新成員。現在我面臨着一個很大的問題,那就是因爲SQL Azure聯合。在我的一個項目中,我想使用聯邦。爲此,我想讀取來自所有聯邦的數據。如何檢查服務器中的聯合總數並從聯合數據中讀取數據?還有我該如何將數據插入聯合會? 目前我使用:聯邦扇出查詢

string strSQL = @"SELECT f.name, fmc.federation_id, fmc.member_id, fmc.range_low, fmc.range_high " + 
                  "FROM sys.federations f " + 
                  "JOIN sys.federation_member_distributions fmc " + 
                  "ON f.federation_id=fmc.federation_id " + 
                  "ORDER BY fmc.federation_id, fmc.range_low, fmc.range_high"; 
     string strTableName = "federation_member_distributions"; 

     try 
     { 
      using (SqlConnection connection = new SqlConnection(csb.ToString())) 
      { 
       connection.Open(); 
       sbResults.AppendFormat("-- Open {0}\r\n\r\n", csb.ToString()); 

       data = new DataSet(); 
       data.Locale = System.Globalization.CultureInfo.InvariantCulture; 

       using (SqlCommand command = connection.CreateCommand()) 
       { 
        // Connect to federation root or federation member 
        command.CommandText = "USE FEDERATION ROOT WITH RESET"; 
        command.ExecuteNonQuery(); 
        sbResults.AppendFormat("{0}\r\nGO\r\n", command.CommandText); 
       } 

       //Retrieve federation root metadata, and populate the data to the DataGridView control 
       SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(strSQL, connection); 
       sqlDataAdapter.Fill(data, strTableName); 
       sbResults.AppendFormat("{0}\r\nGO\r\n", strSQL); 
      } 

     } 
     catch (Exception ex) 
     { 

     } 

,但它不工作

回答

3

爲了您的你真的需要看看這些文章其中談到了如何使用SQL Azure的聯邦上面的問題:

Introduction to Fan-out Queries for Federations in SQL Azure (Part 1): Scalable Queries over Multiple Federation Members, MapReduce Style!

以上你可以找到扇出示例工具它的代碼告訴你如何從聯邦中獲取數據。

本示例的第二部分「Fan-out Querying for Federations in SQL Azure (Part 2): Scalable Fan-out Queries with TOP, ORDER BY, DISTINCT and Other Powerful Aggregates, MapReduce Style!」顯示瞭如何使用特定示例運行扇出查詢。

試試上面的示例,讓我們知道你是否遇到過任何問題。

3

我討論與SQL Azure團隊吉汗Biyikoglu和下面是他的提議:

的想法是,你不必緩存的,其中數據與聯盟「地圖」,所以你不應該不得不;

這裏是僞應用程序代碼和扇出的實際樣本;你也可以在這個UI中嘗試一下代碼。

http://federationsutility-seasia.cloudapp.net/About.aspx

Start at the 
@i=MIN_VALUE (of the data_type or federation key like customerID=-1) 
While (@i not null) 
{ 
Try 
{ 
    -- switch to the next member 
    USE FEDERATION fedname([email protected])... 

    -- Run your transaction here for this member 
    SELECT * FROM table join other_table … WHERE fedkeycolumn >= @i group by … order by … 

    -- grab the next value 
    SELECT @i=range_high FROM sys.federation_member_distributions 
} 
Catch() 
{ 
      If retry_count < total_retries 
          Exit the loop 
} 
} 

如果你仍想找出你總是可以運行在包含聯合會的數據庫查詢中的成員;

Select * from sys.federation_member_distributions fmd join sys.federations f on fmd.federation_id=f.federation_id 

這種方法的問題是,你可能會錯過讀取數據,如果有你讀信息,並完成處理查詢的時間之間的分裂。

沒有緩存'地圖'的上述方法不容易出現這個問題。無論是否進行任何重新分區操作,它都會捕獲所有成員。

+0

偉大而有用的答案! – JYL