Docs 菜单
Docs 主页
/ / /
Mongoid
/

使用索引优化查询

在此页面上

  • Overview
  • 声明和创建索引
  • 别名和声明索引
  • 在嵌入式文档字段上创建索引
  • 创建复合索引
  • 创建地理空间索引
  • 创建稀疏索引
  • 创建多个索引
  • dropIndexes
  • Atlas Search 索引
  • 删除Atlas Search索引
  • 列出Atlas Search索引
  • API 文档

在本指南中,您可以学习;了解如何在 Mongoid 中使用索引。索引可以通过限制MongoDB需要扫描的文档数量来提高查询效率。如果应用应用程序对某些字段重复运行查询,则可以对这些字段创建索引以提高查询性能。

本指南的以下部分介绍如何使用 Mongoid 声明和创建不同类型的索引。这些示例使用Restaurant 模型,该模型映射到restaurants sample_restaurants数据库中的 集合。要学习;了解如何使用 Mongoid 连接到此数据库和集合,请参阅 快速入门 - Ruby on Rails 或快速入门 - Sinatra 指南。

使用 Mongoid 时,可以使用 index 宏索引,然后使用 create_indexes 命令创建索引。

以下代码示例演示如何在 Restaurant 类中的 cuisine字段上声明并创建名为 cuisine_index 的升序索引:

class Restaurant
include Mongoid::Document
field :name, type: String
field :cuisine, type: String
field :borough, type: String
index({ cuisine: 1}, { name: "cuisine_index", unique: false })
end
Restaurant.create_indexes

index 宏定义要创建的索引,而 create_indexes 命令会在 restaurants集合中创建该索引。

定义索引时,第一个哈希对象包含要索引的字段及其方向。1 表示升序索引,-1 表示降序索引。第二个哈希对象包含索引选项。要学习;了解有关索引选项的更多信息,请参阅 API文档部分。

您可以在索引定义中使用带别名的字段名称。示例,以下代码在 b字段上创建索引,该字段是 borough字段的别名:

class Restaurant
include Mongoid::Document
field :borough, as: :b
index({ b: 1}, { name: "borough_index" })
end

您可以在嵌入式文档字段上定义索引。以下代码示例演示如何对 street字段索引升序索引,该字段嵌入 Restaurant 模型中的 address字段中。

class Address
include Mongoid::Document
field :street, type: String
end
class Restaurant
include Mongoid::Document
embeds_many :addresses
index({"addresses.street": 1})
end

您可以在多个字段上定义复合索引。以下代码示例演示如何声明对 borough字段为升序且对 name字段为降序的复合索引。

class Restaurant
include Mongoid::Document
field :name, type: String
field :borough, type: String
index({borough: 1, name: -1}, { name: "compound_index"})
end

您可以在包含GeoJSON对象或坐标对的字段上定义 2dsphere索引。以下示例在包含GeoJSON对象的字段上定义了 2dsphere索引:

class Restaurant
include Mongoid::Document
field :location, type: Array
index({location: "2dsphere"}, { name: "location_index"})
end

有关2 dsphere 索引的更多信息,请参阅MongoDB Server手册中的2 dsphere指南。

有关GeoJSON类型的更多信息,请参阅MongoDB Server手册中的GeoJSON对象指南。

您可以对并非所有文档中都存在的字段定义稀疏索引。以下代码示例在 borough字段上定义了一个稀疏索引:

class Restaurant
include Mongoid::Document
field :name, type: String
field :cuisine, type: String
field :borough, type: String
index({ borough: 1}, { sparse: true })
end

有关稀疏索引的更多信息,请参阅MongoDB Server手册中的稀疏索引指南。

您可以在模型中定义多个索引,并使用单个 create_indexes 调用来创建它们。以下示例展示了如何同时创建多个索引:

class Restaurant
include Mongoid::Document
field :name, type: String
field :cuisine, type: String
field :borough, type: String
index({ name: 1})
index({ cuisine: -1})
end
Restaurant.create_indexes

您可以删除集合中的所有索引。以下示例删除 Restaurant 模型中的所有索引:

Restaurant.remove_indexes

注意

Default Index

MongoDB在创建集合期间会对 _id字段创建默认索引。该索引可防止客户端插入两个具有相同 _id字段值的文档。您不能删除此索引。

您可以使用 Mongoid 声明和管理Atlas Search索引。

要声明搜索索引,请在模型中使用 search_index 宏。要创建模型中声明的搜索索引,请使用 create_search_indexes 命令。以下代码示例演示如何声明和创建名为 my_search_index 的Atlas Search索引。索引位于 namecuisine 字段上,并且是动态的。

class Restaurant
include Mongoid::Document
field :name, type: String
field :cuisine, type: String
field :borough, type: String
search_index :my_search_index,
mappings: {
fields: {
name: {
type: "string"
},
cuisine: {
type: "string"
}
},
dynamic: true
}
end
Restaurant.create_search_indexes

要学习;了解有关创建Atlas Search索引的语法的更多信息,请参阅MongoDB Atlas文档中的创建Atlas Search索引指南。

要删除Atlas Search索引,请使用 remove_search_indexes 命令。以下代码示例展示了如何从 restaurants集合中删除Atlas Search索引:

Restaurant.remove_search_indexes

您可以使用 search_indexes 命令枚举集合中的所有Atlas Search索引。以下示例枚举 restaurants集合中的所有Atlas Search索引并打印出其信息:

Restaurant.search_indexes.each { |index| puts index }

要学习;了解有关在 Mongoid 中使用索引的更多信息,请参阅 Mongoid::Indexable::ClassMethods 文档。

要学习;了解有关索引选项的更多信息,请参阅 Mongoid::Indexable::Validator::Options 文档。

要学习;了解有关在 Mongoid 中使用Atlas Search索引的更多信息,请参阅 Mongoid::SearchIndexable::ClassMethods 文档。

后退

数据关联