gpt4 book ai didi

azure - CosmosDB 中的索引数组

转载 作者:行者123 更新时间:2023-12-04 12:16:24 24 4
gpt4 key购买 nike

为什么 CosmosDB 默认情况下不索引数组?默认索引路径是

"path": "/*"

这不是意味着“索引所有内容”吗?不是“索引除数组之外的所有内容”。

如果我将数组字段添加到索引中,如下所示:

"path": "/tags/[]/?"

它将工作并开始索引该特定数组字段。

但我的问题是为什么“索引所有内容”不索引所有内容?

编辑:这是一篇描述我所看到的行为的博客文章。 http://www.devwithadam.com/2017/08/querying-for-items-in-array-in-cosmosdb.html Array_Contains查询非常慢,显然没有使用索引。如果您将相关字段显式添加到索引中,则查询速度会很快(显然它们开始使用索引)。

最佳答案

"new"索引布局

Index Types 中所述

Azure Cosmos containers support a new index layout that no longer usesthe Hash index kind. If you specify a Hash index kind on the indexingpolicy, the CRUD requests on the container will silently ignore theindex kind and the response from the container only contains the Rangeindex kind. All new Cosmos containers use the new index layout bydefault.

以下问题不适用于新的索引布局。默认索引策略运行良好(并以 36.55 RU 提供结果)。然而,现有的集合可能仍在使用旧的布局。

“旧”索引布局

我能够使用 ARRAY_CONTAINS 重现您所询问的问题。

设置一个包含来自 SO 数据转储的 100,000 个帖子的 CosmosDB 集合(例如,这个问题将如下所示)

{
"id": "50614926",
"title": "Indexing arrays in CosmosDB",
/*Other irrelevant properties omitted */
"tags": [
"azure",
"azure-cosmosdb"
]
}

然后执行以下查询

SELECT COUNT(1)
FROM t IN c.tags
WHERE t = 'sql-server'

该查询使用默认索引策略占用了 2,000 多个 RU,添加了以下内容则占用了 93 个 RU(如链接文章中所示)

{
"path": "/tags/[]/?",
"indexes": [
{
"kind": "Hash",
"dataType": "String",
"precision": -1
}
]
}

但是,您在这里看到的不是默认情况下不对数组值建立索引。只是默认范围索引对您的查询没有用。

范围索引使用基于部分前向路径的键。因此将包含如下路径。

  • 标签/0/azure
  • 标签/0/c#
  • 标签/0/oracle
  • 标签/0/sql-server
  • tags/1/azure-cosmosdb
  • 标签/1/c#
  • tags/1/sql-server

使用此索引结构,它从 tags/0/sql-server 开始,然后读取所有剩余的 tags/0/ 条目以及整个条目tags/n/ 其中 n 是大于 0 的整数。需要检索和评估映射到其中任何一个的每个不同文档。

相比之下,哈希索引使用反向路径 ( more details - PDF )

StackOverflow 理论上允许 UI 为每个问题添加最多 5 个标签,因此在这种情况下(忽略一些问题通过网站管理事件拥有更多标签的事实),感兴趣的反向路径是

  • sql-server/0/tags
  • sql-server/1/tags
  • sql-server/2/tags
  • sql-server/3/tags
  • sql-server/4/tags

通过反向路径结构,查找叶节点值为 sql-server 的所有路径是直接的。

在此特定用例中,由于数组最多有 5 个可能值,因此还可以通过仅查看这些特定路径来有效地使用原始范围索引。

以下查询在我的测试集合中使用默认索引策略占用了 97 个 RU。

SELECT COUNT(1)
FROM c
WHERE 'sql-server' IN (c.tags[0], c.tags[1], c.tags[2], c.tags[3], c.tags[4])

关于azure - CosmosDB 中的索引数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50614926/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com