缺失索引警告
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
创建索引
要为查询创建索引,请单击插件中显示的与警告一起出现的实现索引链接。
插件随后会显示创建索引的模板代码的数据库资源管理器沙盒屏幕。模板代码还包括显示潜在可索引字段的注释。例如,以下代码的第一行指示awards
字段可以索引
// Potential fields to consider indexing: awards // Learn about creating an index: https://mongodb.ac.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()
方法。例如
db.getSiblingDB("sample_database").getCollection("movies"). createIndex({"awards": 1})
禁用索引警告
要在插件中禁用索引警告
打开IntelliJ IDEA系统菜单,然后单击设置。
展开编辑器。
点击 检查。
展开 MongoDB。
展开 可能出现的错误。
禁用 查询未使用索引。