来自 Azure Table 存储的响应时间缓慢
Slow response time from Azure Table Storage
我在 PHP 应用程序中使用 Azure Table 存储,发现响应时间非常慢。该应用程序每天为每个用户收集报告,并使用分区键 $userID . date("dmY")
将它们放在一个分区中。在负载测试期间,某一天生成了数千份报告,由于 Azure Table 存储查询中的 1,000 个实体限制,需要多次往返才能获取。每次获取 1,000 个实体最多可能需要 2 秒。
代码:
for($i = 0; $i < $daysToGoBack; $i++)
{
$filter = "PartitionKey eq '" . $userId . date("dmY", strtotime("-$i days")) . "'";
$options = new QueryEntitiesOptions();
$options->addSelectField('created');
$options->setFilter(Filter::applyQueryString($filter));
$this->benchmark->mark('main_query');
$result = $this->tableRestProxy->queryEntities('reports', $filter, $options);
$this->benchmark->mark('main_query_end');
echo "Query: " . $this->benchmark->elapsed_time('main_query', 'main_query_end') . "<br/>";
$entities = array_merge($result->getEntities(), $entities);
$nextPartitionKey = $result->getNextPartitionKey();
$nextRowKey = $result->getNextRowKey();
while(!is_null($nextRowKey) && !is_null($nextPartitionKey))
{
$options = new QueryEntitiesOptions();
$options->setNextPartitionKey($nextPartitionKey);
$options->setNextRowKey($nextRowKey);
$options->addSelectField('created');
$options->setFilter(Filter::applyQueryString($filter));
$this->benchmark->mark('sub_query');
$newResult = $this->tableRestProxy->queryEntities('reports', $options);
$this->benchmark->mark('sub_query_end');
echo "Continuation: " . $this->benchmark->elapsed_time('sub_query', 'sub_query_end') . "<br/>";
$newEntities = $newResult->getEntities();
$entities = array_merge($newEntities, $entities);
$nextPartitionKey = $newResult->getNextPartitionKey();
$nextRowKey = $newResult->getNextRowKey();
}
结果:
Query: 1.8183
Continuation: 1.2479
Continuation: 0.2423
Continuation: 0.2619
Continuation: 0.2476
Continuation: 0.2836
Continuation: 0.2345
Continuation: 0.2482
Continuation: 0.2565
Continuation: 0.2187
Continuation: 0.2319
Continuation: 0.2389
Continuation: 0.2221
Query: 0.0320
Query: 0.0338
Query: 0.1038
Query: 0.1263
Query: 0.1841
Query: 0.0547
上面的结果是我能得到的最好结果。第一个查询有将近 13,000 个报告要撤回,初始查询也是如此,然后使用延续令牌进行 12 次回调。大多数情况下,对于完全相同的数据,查询可能需要更长的时间。
Query: 1.8273
Continuation: 1.2592
Continuation: 0.8160
Continuation: 0.8463
Continuation: 0.7474
Continuation: 0.7104
Continuation: 1.3987
Continuation: 1.4321
Continuation: 1.4526
Continuation: 1.3184
Continuation: 0.7390
Continuation: 0.7212
Continuation: 0.2610
Query: 0.0630
Query: 0.1221
Query: 0.0728
Query: 0.1250
Query: 0.1717
Query: 0.0568
这些结果是来自 Azure 的预期结果还是有更有效的方法来查询这些数据?
你看过Azure Table Storage Design guide了吗?这可能会有所帮助。
我在 PHP 应用程序中使用 Azure Table 存储,发现响应时间非常慢。该应用程序每天为每个用户收集报告,并使用分区键 $userID . date("dmY")
将它们放在一个分区中。在负载测试期间,某一天生成了数千份报告,由于 Azure Table 存储查询中的 1,000 个实体限制,需要多次往返才能获取。每次获取 1,000 个实体最多可能需要 2 秒。
代码:
for($i = 0; $i < $daysToGoBack; $i++)
{
$filter = "PartitionKey eq '" . $userId . date("dmY", strtotime("-$i days")) . "'";
$options = new QueryEntitiesOptions();
$options->addSelectField('created');
$options->setFilter(Filter::applyQueryString($filter));
$this->benchmark->mark('main_query');
$result = $this->tableRestProxy->queryEntities('reports', $filter, $options);
$this->benchmark->mark('main_query_end');
echo "Query: " . $this->benchmark->elapsed_time('main_query', 'main_query_end') . "<br/>";
$entities = array_merge($result->getEntities(), $entities);
$nextPartitionKey = $result->getNextPartitionKey();
$nextRowKey = $result->getNextRowKey();
while(!is_null($nextRowKey) && !is_null($nextPartitionKey))
{
$options = new QueryEntitiesOptions();
$options->setNextPartitionKey($nextPartitionKey);
$options->setNextRowKey($nextRowKey);
$options->addSelectField('created');
$options->setFilter(Filter::applyQueryString($filter));
$this->benchmark->mark('sub_query');
$newResult = $this->tableRestProxy->queryEntities('reports', $options);
$this->benchmark->mark('sub_query_end');
echo "Continuation: " . $this->benchmark->elapsed_time('sub_query', 'sub_query_end') . "<br/>";
$newEntities = $newResult->getEntities();
$entities = array_merge($newEntities, $entities);
$nextPartitionKey = $newResult->getNextPartitionKey();
$nextRowKey = $newResult->getNextRowKey();
}
结果:
Query: 1.8183
Continuation: 1.2479
Continuation: 0.2423
Continuation: 0.2619
Continuation: 0.2476
Continuation: 0.2836
Continuation: 0.2345
Continuation: 0.2482
Continuation: 0.2565
Continuation: 0.2187
Continuation: 0.2319
Continuation: 0.2389
Continuation: 0.2221
Query: 0.0320
Query: 0.0338
Query: 0.1038
Query: 0.1263
Query: 0.1841
Query: 0.0547
上面的结果是我能得到的最好结果。第一个查询有将近 13,000 个报告要撤回,初始查询也是如此,然后使用延续令牌进行 12 次回调。大多数情况下,对于完全相同的数据,查询可能需要更长的时间。
Query: 1.8273
Continuation: 1.2592
Continuation: 0.8160
Continuation: 0.8463
Continuation: 0.7474
Continuation: 0.7104
Continuation: 1.3987
Continuation: 1.4321
Continuation: 1.4526
Continuation: 1.3184
Continuation: 0.7390
Continuation: 0.7212
Continuation: 0.2610
Query: 0.0630
Query: 0.1221
Query: 0.0728
Query: 0.1250
Query: 0.1717
Query: 0.0568
这些结果是来自 Azure 的预期结果还是有更有效的方法来查询这些数据?
你看过Azure Table Storage Design guide了吗?这可能会有所帮助。