且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

索引为>的搜索时间没有索引

更新时间:2023-11-26 23:14:52

在这种情况下,索引没有帮助,因为您匹配的结果集几乎包含整个集合.这意味着它必须加载到RAM中并遍历大部分索引,还必须加载到RAM中并遍历文档本身.

In this case, the index doesn't help because your matching result set consists of almost the entire collection. That means it has to load into RAM and traverse most of the index, as well as load into RAM and traverse the documents themselves.

没有索引,它将只进行表扫描,检查每个文档,如果匹配则返回.

Without the index, it would just do a table scan, inspecting each document and returning if matched.

在这种情况下,查询将返回几乎整个集合,索引可能无济于事.

In cases like this where a query is going to return almost an entire collection, an index may not be helpful.

添加.limit()将加快查询速度.您还可以强制查询优化器不要在.hint()中使用索引:

Adding a .limit() will speed the query up. You can also force the query optimizer to not use the index with .hint():

db.collection.find().hint({$natural:1})

您还可以通过将所选字段限制为仅已索引的字段来强制查询直接从索引本身提供结果值.这样可以避免在执行索引扫描后加载任何文档.

You could also force the query to provide the result values directly from the index itself by limiting the selected fields to only the ones you've indexed. This allows it to avoid the need to load any documents after doing the index scan.

尝试一下,看看说明输出是否指示"indexOnly":true

Try this and see if the explain output indicates "indexOnly":true

db.numbers.find({number: {$gt: 10000}}, {number:1}).explain()

详细信息在这里:

http://www.mongodb. org/display/DOCS/Retrieving + a + Subset + of + Fields#RetrievingaSubsetofFields-CoveredIndexes