隐藏索引

4.4版本中的新功能

隐藏索引对查询计划程序不可见,并且不能用于支持查询。

通过向规划者隐藏索引,用户可以评估删除索引的潜在影响,而无需实际删除索引。如果影响是负面的,用户可以取消隐藏索引,而不必重新创建已删除的索引。

行为

除了对规划器隐藏之外,隐藏索引的行为与非隐藏索引类似。例如:

  • 如果隐藏索引是唯一索引,则该索引仍将其唯一约束应用于文档。
  • 如果隐藏索引是TTL 索引,则该索引仍然会使文档过期。
  • 隐藏索引包含在listIndexesdb.collection.getIndexes()结果中。
  • 隐藏索引在对集合进行写入操作时更新,并继续消耗磁盘空间和内存。因此,它们包含在各种统计操作中,例如 db.collection.stats()$indexStats
  • 隐藏未隐藏的索引或取消隐藏隐藏的索引会重置其 $indexStats. 隐藏已隐藏的索引或取消隐藏已取消隐藏的索引不会重置$indexStats.

限制

例子

创建隐藏索引

要创建hidden索引,请使用 隐藏选项设置为 的db.collection.createIndex()方法。true

笔记:

要使用该hidden选项 db.collection.createIndex(),您必须将 featureCompatibilityVersion设置为4.4或更大。但是,一旦隐藏,即使在 MongoDB 4.4 二进制文件上将featureCompatibilityVersion设置为,索引仍保持隐藏状态 4.2

例如,以下操作在borough字段上创建隐藏的升序索引:

db.addresses.createIndex(
   { borough: 1 },
   { hidden: true }
);

要验证,请在集合db.collection.getIndexes()上 运行addresses

db.addresses.getIndexes()

该操作返回以下信息:

[
   {
      "v" : 2,
      "key" : {
         "_id" : 1
      },
      "name" : "_id_"
   },
   {
      "v" : 2,
      "key" : {
         "borough" : 1
      },
      "name" : "borough_1",
      "hidden" : true
   }
]

hidden仅当值为 时才返回索引选项true

隐藏现有索引

笔记:

要隐藏现有索引,您可以使用collMod命令或mongosh帮手 db.collection.hideIndex()

例如,创建一个不隐藏的索引:

db.restaurants.createIndex( { borough: 1, ratings: 1 } );

要隐藏索引,您可以指定:

  • db.collection.hideIndex()方法的索引键规范文档:

    db.restaurants.hideIndex( { borough: 1, ratings: 1 } ); // Specify the index key specification document
    
  • 方法的索引名称db.collection.hideIndex()

    db.restaurants.hideIndex( "borough_1_ratings_1" );  // Specify the index name
    

要验证,请在集合db.collection.getIndexes()上 运行restaurants

db.restaurants.getIndexes()

该操作返回以下信息:

[
   {
      "v" : 2,
      "key" : {
         "_id" : 1
      },
      "name" : "_id_"
   },
   {
      "v" : 2,
      "key" : {
         "borough" : 1,
         "ratings" : 1
      },
      "name" : "borough_1_ratings_1",
      "hidden" : true
   }
]

hidden仅当值为 时才返回索引选项true

取消隐藏现有索引

要取消隐藏隐藏索引,您可以使用collMod命令或mongosh帮手 db.collection.unhideIndex()。您可以指定:

  • db.collection.unhideIndex()方法的索引键规范文档:

    db.restaurants.unhideIndex( { borough: 1, city: 1 } );  // Specify the index key specification document
    
  • db.collection.unhideIndex() 方法中的索引名称:

    db.restaurants.unhideIndex( "borough_1_ratings_1" );    // Specify the index name
    

要验证,请在集合db.collection.getIndexes()上 运行restaurants

db.restaurants.getIndexes()

该操作返回以下信息:

[
   {
      "v" : 2,
      "key" : {
         "_id" : 1
      },
      "name" : "_id_"
   },
   {
      "v" : 2,
      "key" : {
         "borough" : 1,
         "ratings" : 1
      },
      "name" : "borough_1_ratings_1"
   }
]

索引选项hidden不再显示为 borough_1_ratings_1索引的一部分,因为仅当值为 时才返回该字段true

由于索引在隐藏时得到完全维护,因此一旦取消隐藏,索引就可以立即使用。

Copyright © 上海锦木信息技术有限公司 all right reserved,powered by Gitbook文件修订时间: 2023-09-01 17:10:26

results matching ""

    No results matching ""