首页 > 解决方案 > Cosmos DB 高额查询费用

问题描述

我在 Autopilot 模式下使用 Cosmos DB,集合中的最大 RU/s 设置为 20K RU/s。我正在使用 SQL API。我在这个特定分区中有几十万条记录。我有默认的索引策略,即所有内容都被索引。我已进入 Azure 门户中的数据资源管理器并运行了以下查询:

SELECT count(1) FROM c where c.partitionKey = "12140" and c.uniqueId = "20cdb686-6959-4ed6-ac31-c7f414238f68" and c.timestampAsString = null

查询统计显示 RU/s 费用为 3.46。这很棒!

然后我运行一个非常相似的查询,但我正在测试 c.timestampAsString 是否不为空:

SELECT count(1) FROM c where c.partitionKey = "12140" and c.uniqueId = "20cdb686-6959-4ed6-ac31-c7f414238f68" and c.timestampAsString != null

上述查询的总 RU/s 费用为 2989.73 RU/s。这是相当高的一点。我期待与原始查询类似的东西。

谁能解释为什么这些查询费用如此不同?

标签: azureazure-cosmosdb

解决方案


RU 中这种巨大差异的原因是我们如何/是否将过滤器下推到索引中。对于像 c.timestampAsString = null 这样的查询,我们确实将其推送到索引。对于您进行聚合和过滤的查询,例如 c.timestampAsString != null 我们不这样做。

但是,您可以做一些事情来优化此查询。如果您创建名为“istimestampAsStringNull”的第二个属性并将其设置为 true 到 false,这将使您能够执行以下查询,并且您应该获得大致相同的 RU/s 成本。

SELECT count(1) FROM c where c.partitionKey = "12140" and c.uniqueId = "20cdb686-6959-4ed6-ac31-c7f414238f68" and c. isTimestampAsStringNull == false

我们正在考虑未来的优化,但目前上述解决方法应该可行。

希望这会有所帮助。


推荐阅读