Home > database >  Directing index sort field is very slow
Directing index sort field is very slow

Time:09-27

You greatly, it is such a, such as the following query:
 & gt; Db.com ment. Find ({" topicId ":" XXXX "}), limit (10), sort ({" createTime ": 1}) 

Now there are two situations:
In the table 1. In the comment, topicId and createTime have been set to ascending index, and according to condition {" topicId ":" XXXX "} is query, no data at this point, the query time take ,
In the table 2. In the comment, topicId is set to ascending index, createTime not indexed field, and according to condition {" topicId ":" XXXX "} is query, no data at this point, the query time take ,

According to the explain check found:
The first case was carried out a full table scan,
The second case without a full table scan, but found another problem: join in {" topicId ":" XXXX "} article data for 8000, but according to explain:
 "nscannedObjects:" 186657, "nscanned" : 186657 
, incredibly scanned the 18 w multiple objects,

Now my question is:
1. In the query result sets of empty (no results returned), and do the sorting, if can do it faster?
Article 2. Clearly conforms to query data number more than 8000 only, explain why the results of the 18 w multiple documents to be scanned?
Mongo newbie guide,,,

CodePudding user response:

No one answer,,,,

CodePudding user response:

1 plus topicId and createTime joint index db.com ment. CreateIndex ({1} topicId: 1, createTime:)
2 scan 180000 documents to filter out the 8000

CodePudding user response:

Could you tell me your specific index is how?
Looks like the index is not completely the fetch,

CodePudding user response:

Thank you for the guidance of the great god @ rucypli, added joint after the index, problem solved
  • Related