Docs 菜单
Docs 主页
/
MongoDB for IntelliJ 插件

缺失索引警告

在此页面上

  • 定义
  • 示例
  • 了解详情

MongoDB for IntelliJ 插件会检查应用程序查询是否使用索引。如果查询未使用索引,则该插件会显示该查询的警告。

要解决此警告,请考虑为查询创建索引。

在添加索引之前,请考虑以下情况:

  • 查询运行的频率足够高,因此有必要降低写入性能以加快读取速度。

  • 您可以更改查询以使用现有索引。

您还可以禁用索引警告。

有关索引的更多信息,请参阅 创建索引。

在以下示例Java代码片段中,查询中使用了 awards文档字段,但该字段未在数据库中建立索引:

client.getDatabase( "sample_mflix" ).getCollection( "movies" ).find(
Filters.ne( "awards", "Comedy" )
)

该插件会显示此警告:

This query will run without an index. If you plan on using this
query heavily in your application, you should create an index that
covers this query.
Implement an Index

要为查询创建索引,请单击插件中显示的带有警告的 Implement an Index 链接。

然后,该插件会显示 Database Explorer Playgrounds 屏幕,其中包含用于创建索引的模板代码。模板代码还包含一条注释,显示要索引的潜在字段。示例,以下代码的第一行表示可以对 awards字段编制索引:

// Potential fields to consider indexing: awards
// Learn about creating an index: https://www.mongodb.com/zh-cn/docs/v7.0/core/data-model-operations/#indexes
db.getSiblingDB("sample_mflix").getCollection("movies").
createIndex({"<your_field_1>": 1})

awards要为<your_field_1> awards字段创建索引,请在示例代码中将createIndex() 设立为 ,然后在Database Explorer Playgrounds 屏幕中运行 方法。示例:

db.getSiblingDB("sample_database").getCollection("movies").
createIndex({"awards": 1})

要在插件中禁用索引警告,请执行以下操作:

  1. 打开 IntelliJ IDEA 系统菜单,然后单击 Settings

  2. 展开Editor

  3. 单击 Inspections(连接)。

  4. 展开MongoDB

  5. 展开Probable bugs

  6. 禁用 Query does not use an index

后退

类型验证