db.getCollection("rptDlp").find({ incidentOn: { $gte: new Date(1513641600000), $lt: new Date(1516233600000) } })
.sort({ incidentOn: -1 }).skip(15610600).limit(10)
Hi,
If I execute this query directly against mongo shard server (PRIMARY), it shows result in 14 seconds.
It’s generally not recommended to connect directly to the shards and perform queries there. The result might not be reliable, since it can contain outdated documents that was moved to another shard but not yet deleted. These “orphaned” documents could be updated in the meantime, rendering the query to return incorrect results.
But through mongos, it takes more than 2 minutes
I believe there are multiple causes to this performance issue:
The combination of the three situations above resulted in a slow query, since MongoDB basically cannot use any index to help the query.
Without knowing the details of your use case, if this query is vital and would need to be performed many times a day, then using the incidentOn
field as the shard key would allow the shard key to be used to help the query. However, this comes with the drawback of having a monotonically increasing shard key. If this is not acceptable, then a more selective query may be required, so that the shard key could be less monotonic.
Please see Shard Keys and On Selecting a Shard Key for MongoDB for more shard key selection considerations.
Best regards
Kevin