【问题标题】:Azure Table Storage: Efficient way to query multiple PK-RK pairsAzure 表存储:查询多个 PK-RK 对的有效方法
【发布时间】:2017-05-16 19:21:02
【问题描述】:

我正在使用此过滤器进行查询: (PartitionKey eq 'A' or PartionKey eq 'B' or ...) 和 RowKey eq 'RK'

我意识到这种具有 20 到 100 个 PK 的查询需要 3 到 5 秒。桌子上的物品总数不多(大约100万)

我认为正在执行部分扫描查询。我以为它会做几个 puntual 查询,但似乎不是这样。

我的另一个选择是进行独立的并行查询,然后合并结果。

  • 这是 100 件商品的好选择吗?
  • 我不会遇到网络连接问题吗? (我用 ServicePointManager.DefaultConnectionLimit 增加它们)

注意:并非所有 PK/RK 对都会检索记录。

【问题讨论】:

    标签: azure azure-table-storage


    【解决方案1】:

    我的另一个选择是进行独立的并行查询,然后合并结果。

    它会节省 Azure Storage 上的查询时间,但会花更多时间在查询请求和结果响应上。我有一个包含 160K 实体的表。我编写了两个示例代码来测试从一个查询和多个查询中查询实体的总次数。这是我的测试结果。

    以下是我的示例代码。

    从一个查询中查询实体。

    int entitesCount = 20;
    TableQuery<CustomerEntity> customerQuery = new TableQuery<CustomerEntity>();
    string filter = "(";
    for (int i = 0; i < entitesCount; i++)
    {
        filter = filter + "PartitionKey eq '" + i + "'";
        if (i < entitesCount - 1)
        {
            filter = filter + " or ";
        }
    }
    filter = filter + ") and RowKey eq '42'";
    customerQuery.FilterString = filter;
    
    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();
    
    var customers = table.ExecuteQuery(customerQuery);
    Console.WriteLine(customers.Count().ToString());
    
    stopWatch.Stop();
    TimeSpan ts = stopWatch.Elapsed;
    Console.WriteLine(ts.ToString());
    

    从并行多查询中查询实体。

    ServicePointManager.DefaultConnectionLimit = 100;
    
    Stopwatch stopWatch = new Stopwatch();
    stopWatch.Start();
    
    int entitesCount = 20;
    List<CustomerEntity> customers = new List<CustomerEntity>();
    var result = Parallel.For(0, entitesCount, new Action<int>(i =>
    {
        TableQuery<CustomerEntity> customerQuery = new TableQuery<CustomerEntity>();
        customerQuery.FilterString = "PartitionKey eq '" + i.ToString() + "' and RowKey eq '88'";
    
        var cs = table.ExecuteQuery(customerQuery);
        foreach (var c in cs)
        {
            customers.Add(c);
        }
    }));
    while (!result.IsCompleted) { }
    
    Console.WriteLine(customers.Count.ToString());
    stopWatch.Stop();
    
    TimeSpan ts = stopWatch.Elapsed;
    Console.WriteLine(ts.ToString());
    

    Azure 表存储:查询多个 PK-RK 对的有效方法

    我建议你自己测试一下,然后确定哪种方式好。

    【讨论】:

    • 感谢 Amor 的回答。我会按照你的建议做一个测试。
    • 经过一些测试后,我得出结论,在我的特定情况下,在 IN 语句中对 30 个项目进行分组的并行查询是性能最高的。
    • 减少或增加 IN 项会增加获得结果所需的总时间
    • 另外我发现在分组之前对项目进行排序也提高了性能。
    猜你喜欢
    • 2016-09-29
    • 1970-01-01
    • 2012-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多