Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs 菜单
Docs 主页
/
IntelliJ Plugin

索引性能警告

MongoDB for IntelliJ 插件会检查应用程序查询是否使用索引。如果查询不使用索引或仅部分被索引覆盖,则该插件会显示该查询的警告。

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

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

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

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

您还可以禁用索引警告。

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

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

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

侧面板在 Performance Warnings 下显示以下警告:

IntelliJ 插件中的索引警告。

要为查询创建索引,请执行以下操作:

1

该插件显示 Database Explorer Playgrounds 屏幕,其中包含用于创建索引的模板代码:

// region Queries covered by this index
// alt.mongodb.javadriver.JavaDriverRepository#getRatings at line 32
// endregion
// 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({ "awards": 1 })
2

在示例代码中将 <your_field_1> 设置为 awards,然后在 Database Explorer Playgrounds 屏幕中运行createIndex() 方法。示例:

db.getSiblingDB("sample_database").getCollection("movies").
createIndex({"awards": 1})
  • 禁用警告

  • 索引

  • 运营因素和数据模型

后退

类型验证

在此页面上