2013-01-08 73 views
1

我正在使用Windows Azure表存儲。我的問題是,從表中訪問給定用戶的實體需要很長時間。我用來訪問表的代碼如下:Windows Azure表訪問時間過長

public CloudTableQuery<T> GetEntites(string username) 
    { 
     try 
     { 
      CloudTableQuery<T> entries = 
       (from e in ServiceContext.CreateQuery<T>(TableName) 
       where e.PartitionKey == username 
       select e).AsTableServiceQuery(); 

      return entries; 
     } 
     catch (Exception) 
     { return null; } 
    } 

表中的實體總數目前只有100左右。例如:查詢似乎需要長達40秒才能爲給定用戶返回25個實體。請建議代碼中是否有改進的地方以提高性能?

+0

public class UserEntity { public string UserName { get; set; } public string Name { get; set; } public string Email { get; set; } } 

然後用類似定義你的表存儲實體從處所運行代碼?你的互聯網連接如何?無論如何,表格本身是如此之小以至於無論查詢有多糟(不是什麼),都不應該有任何性能問題。這聽起來像是存儲或連接問題 – Igorek

+0

當您使用Dev Storage在本地運行項目時,是否遇到相同的延遲? –

回答

1

這裏真正的問題是您正在返回查詢而不是實體,並且您可能會爲每個返回的實體(查詢)再次執行查詢。喜歡的東西定義你的實體:

public class StoredUserEntity : TableServiceEntity 
{ 
    public StoredUserEntity (string username) 
    { 
     this.PartitionKey = username; 
     this.RowKey = Guid.NewGuid().ToString(); 
     this.Timestamp = DateTime.Now; 
    } 

    public string Email { get; set; } 
    public string Name { get; set; } 
} 

然後將查詢實際上應該返回UserEntities列表:

public List<UserEntity> GetUserData(string username) 
    { 
     var q = _tableContext.CreateQuery<StoredUserEntity>(_tableName); 
     return 
      q.ToList().Select(x => new UserEntity {UserName = x.PartitionKey, Email = x.Email, Name = x.Name}).Where(x => x.UserName == username).ToList(); 
    } 
0

嘗試使用Fiddler來查看發生了什麼。這可能是因爲你正在經歷一些可能會減慢速度的重試。 API不會告訴你,但是用Fiddler你可以看到正在做出的請求,任何響應(以及錯誤)以及它們中的多少。