【问题标题】:DocumentDB creating extra indexesDocumentDB 创建额外索引
【发布时间】:2017-05-04 06:48:04
【问题描述】:

我创建了一个分区集合,其中我需要 DateTime 字段上的 Range 索引(如 ISO-8601)和其他几个字段上的 Hash 索引。这些是我将仅过滤的字段,因此我不需要任何其他字段的索引。

我已经这样定义了我的索引(使用扩展方法):

indexingPolicy.IncludedPaths.Add("/playerId/?", new HashIndex(DataType.Number));
indexingPolicy.IncludedPaths.Add("/category/?", new HashIndex(DataType.String));
indexingPolicy.IncludedPaths.Add("/dateTime/?", new RangeIndex(DataType.String));
indexingPolicy.ExcludedPaths.Add(new ExcludedPath { Path = "/*" });

奇怪的是,当我检查索引策略时,我可以看到创建了许多对我来说毫无意义的额外索引。请参阅下面的定义和我的 cmets。

[{
    "path" : "/playerId/?",
    "indexes" : [{
            "kind" : "Hash",
            "dataType" : "Number",
            "precision" : 3
        }, {
            "kind" : "Hash",
            "dataType" : "String", <<< It created an extra String Hash index
            "precision" : 3
        }
    ]
}, {
    "path" : "/category/?",
    "indexes" : [{
            "kind" : "Hash",
            "dataType" : "String",
            "precision" : 3
        }, {
            "kind" : "Range",
            "dataType" : "Number", <<< It created an extra Number Range index, this field is not numeric and I wanted a String Hash index only
            "precision" : -1
        }
    ]
}, {
    "path" : "/dateTime/?",
    "indexes" : [{
            "kind" : "Range",
            "dataType" : "String",
            "precision" : -1
        }, {
            "kind" : "Range",
            "dataType" : "Number", <<< It created an extra Number Range index, this field is not numeric and I wanted a String Range index only
            "precision" : -1
        }
    ]
}, {

它为什么要创建这些索引?

【问题讨论】:

    标签: azure-cosmosdb


    【解决方案1】:

    它为什么要创建这些索引?

    我们可能会从Azure官方document那里得到答案。

    默认情况下,DocumentDB 使用 Hash 索引一致地索引文档中的所有字符串属性,使用 Range 索引来索引数字属性。

    据我了解,如果我们要为属性创建索引,如果不是我们自己创建的,它会确保有一个字符串哈希索引或范围号。

    例如:

    IndexingPolicy = new IndexingPolicy(new RangeIndex(DataType.Number) 
    

    索引将是:

    "indexes" : [{
                "kind" : "Hash",
                "dataType" : "String",
                "precision" : 3
            }, {
                "kind" : "Range",
                "dataType" : "Number",
                "precision" : -1
            }
    

    【讨论】:

    • 我曾希望可以告诉它不要创建我不需要的索引,但不幸的是,情况似乎并非如此。
    • 此外,您似乎无法在同一数据类型上创建 Hash 和 Range 索引。例如。如果您尝试创建哈希字符串 + 范围字符串,它将引发异常。
    • 实际上这个答案并没有回答“为什么”部分。是的,应用了默认值,但不清楚这个从未使用过的索引的目的是什么。
    猜你喜欢
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    相关资源
    最近更新 更多