So I had this issue today where my MongoDB queries where extremely slow and timing out. I posted this question - MongoDB too many records? and his suggestion was right wherein I had to ensureIndex and remove case-insensitivity. I tried it in Mongo shell, it worked perfectly.
However, when I ran it via PHP, it was still the same :( I then realized the query had a sort on "id" (not _id) field and when I removed that, things were blazing fast. But with the sort, it was REALLY slow. I already had an index on the id field. This is the query btw :
db.tweet_data.find({
... $or:
... [
... { in_reply_to_screen_name: /^kunalnayyar$/, handle: /^kaleycuoco$/, id: { $gt: 0 } },
... { in_reply_to_screen_name: /^kaleycuoco$/, handle: /^kunalnayyar$/, id: { $gt: 0 } }
... ],
... in_reply_to_status_id_str: { $ne: null }
...
... } ).sort({id:-1})explain()
So my indexes are : (not composite) { {id:-1} , {handle:1}, {in_reply_to_screen_name:1} }
After some reading I realized it should have been a composite index and I tried two variations to no success :
1. {handle:1, in_reply_to_screen_name:1, id:-1}
2. {id:-1,handle:1, in_reply_to_screen_name:1}
I am not sure where I am going wrong, but I am pretty sure the issue is indexing here. I am just too buzzed and can't understand the order and the fields to index
id: { $gt: 0 }
in the query, or has that been removed? If that is actually a default ObjectId you shouldn't need to assert it is greater than 0. Similarly, you may want to includein_reply_to_status_id_str
in the$or
queries/index rather than separately. I would try explaining the individual queries first before combining in an$or
. – StennienScanned
numbers in the explain() versus then
. – Stennie