我有一个删除每张桌子的功能。属于受影响用户的blob。
CloudTable uploadTable = CloudStorageServices.GetCloudUploadsTable();
TableQuery<UploadEntity> uploadQuery = uploadTable.CreateQuery<UploadEntity>();
List<UploadEntity> uploadEntity = (from e in uploadTable.ExecuteQuery(uploadQuery)
where e.PartitionKey == "uploads" && e.UserName == User.Idendity.Name
select e).ToList();
foreach (UploadEntity uploadTableItem in uploadEntity)
{
//Delete table
TableOperation retrieveOperationUploads = TableOperation.Retrieve<UploadEntity>("uploads", uploadTableItem.RowKey);
TableResult retrievedResultUploads = uploadTable.Execute(retrieveOperationUploads);
UploadEntity deleteEntityUploads = (UploadEntity)retrievedResultUploads.Result;
TableOperation deleteOperationUploads = TableOperation.Delete(deleteEntityUploads);
uploadTable.Execute(deleteOperationUploads);
//Delete blob
CloudBlobContainer blobContainer = CloudStorageServices.GetCloudBlobsContainer();
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(uploadTableItem.BlobName);
blob.Delete();
}
每个表都有自己的blob,因此如果列表中包含3个uploadentities,则会删除3个表和3个blob。
我听说您可以使用表批处理操作来降低成本和负载。我尝试过,但失败了。有兴趣帮助我的人:)?
我猜测tablebatch操作仅适用于表,所以它不适用于blob,对吧?
您如何为此代码添加tablebatchoperations?您是否看到可以做的任何其他改进?
谢谢!
答案 0 :(得分:1)
我想使用批处理操作,但我不知道如何操作。无论如何,经过一些测试我发现了它。
改进了删除多个实体的代码:
CloudTable uploadTable = CloudStorageServices.GetCloudUploadTable();
TableQuery<UserUploadEntity> uploadQuery = uploadTable.CreateQuery<UserUploadEntity>();
List<UserUploadEntity> uploadEntity = (from e in uploadTable.ExecuteQuery(uploadQuery)
where e.PartitionKey == "useruploads" && e.MapName == currentUser
select e).ToList();
var batchOperation = new TableBatchOperation();
foreach (UserUploadEntity uploadTableItem in uploadEntity)
{
//Delete upload entities
batchOperation.Delete(uploadTableItem);
//Delete blobs
CloudBlobContainer blobContainer = CloudStorageServices.GetCloudBlobContainer();
CloudBlockBlob blob = blobContainer.GetBlockBlobReference(uploadTableItem.BlobName);
blob.Delete();
}
uploadTable.ExecuteBatch(batchOperation);
我知道批量操作仅限于100,但在我的情况下,没有什么可担心的。