首页 > 解决方案 > Microsoft.Azure.Cosmos.Table 和 Microsoft.WindowsAzure.Storage 之间的 ExecuteQuerySegmentedAsync 性能显着下降

问题描述

我一直在研究从存储帐户表存储迁移到 CosmosDB 表存储。目前我正在使用 WindowsAzure.Storage (9.3.3) 库来查询 .net core 3.1 应用程序中的数据。作为此迁移的一部分,我已切换到 Microsoft.Azure.Cosmos.Table 1.0.7 库。我在下面编写了 LinqPad 基准测试来比较两者在进行全表扫描时的性能。

async Task Main()
{
    var timer = Stopwatch.StartNew();
    await QueryCosmosDb().ConfigureAwait(false);
    timer.Stop();
    var cosmosExecutionTime = timer.Elapsed;

    timer = Stopwatch.StartNew();
    await QueryTableStorage().ConfigureAwait(false);
    timer.Stop();
    var tableExecutionTime = timer.Elapsed;
    
    cosmosExecutionTime.Dump();
    tableExecutionTime.Dump();
}

public async Task QueryCosmosDb()
{
    var cosmosTableEndpoint = new Uri($"https://***.table.cosmos.azure.com:443/");
    var storageAccount = new Microsoft.Azure.Cosmos.Table.CloudStorageAccount(new Microsoft.Azure.Cosmos.Table.StorageCredentials("***", "****"), cosmosTableEndpoint);
    var client = storageAccount.CreateCloudTableClient();
    var table = client.GetTableReference("tablename");
    var query = new Microsoft.Azure.Cosmos.Table.TableQuery();
    Microsoft.Azure.Cosmos.Table.TableContinuationToken token = null;
    do
    {
        var segment = await table.ExecuteQuerySegmentedAsync(query, token).ConfigureAwait(false);
        token = segment.ContinuationToken.Dump();
    }
    while (token != null);
}

public async Task QueryTableStorage()
{
    var storageAccount = new Microsoft.WindowsAzure.Storage.CloudStorageAccount(new Microsoft.WindowsAzure.Storage.Auth.StorageCredentials("***", "****"), true);
    var client = storageAccount.CreateCloudTableClient();
    var table = client.GetTableReference("tablename");
    var query = new Microsoft.WindowsAzure.Storage.Table.TableQuery();
    Microsoft.WindowsAzure.Storage.Table.TableContinuationToken token = null;
    do
    {
        var segment = await table.ExecuteQuerySegmentedAsync(query, token).ConfigureAwait(false);
        token = segment.ContinuationToken;
    }
    while (token != null);
}

存储帐户表和 CosmosDb 表具有大约 200k 个实体的相同数据集。

Cosmos 表帐户的共享供应吞吐量为 2200 RU。

将 Cosmos Executor 与 Microsoft.Azure.Cosmos.Table 库一起使用时,执行时间约为 3 小时。带有 Microsoft.WindowsAzure.Storage 库的存储帐户表大约需要 2 分钟。如果我将 Microsoft.Azure.Cosmos.Table 库切换为使用 Cloud Table Client 中的其余执行程序,我会得到约 3 分钟的执行时间。

有没有人遇到过类似的行为或意识到空表查询的问题?

还在azure-cosmos-table-dotnet中添加了 Github 问题的票

标签: c#azure-sdk-.netazure-cosmosdb-tables

解决方案


它是导致时差的 ExecuteQuery 方法的内部实现,因此除非 Microsoft 在即将发布的版本中注意到并修复该问题,否则我们不可能解决该问题,无论如何,既然他们已经弃用并使用Microsoft.Azure.Cosmos它必须使用的公共库解决了问题,希望对你有帮助


推荐阅读