0

When I query through mongo shell or robo 3t the query takes about 3ms to 5ms. But the same query from my node(12.18.3) application which uses mongoose(5.5.10) it takes about 6000ms on average. Robo 3t returns 50 documents by default and mongo shell returns 20 I guess. Node application was returning 800 documents. So I made mongo shell return 800 documents by setting DBQuery.shellBatchSize = 300. This query still took very less time than 6000ms. What am I missing here ?

Edit 1: It's a Simple Query. db.gateways.find({org_id:1}) I have an index on org_id. Each Document has about 450 fields and average document size is 6.5kb. Above query returns 800 documents.

soothsayer
  • 21
  • 9
  • 1
    It could be network. Where is your node server running? Where is your database running? Can you show all queries you are running (robo3t, shell and mongoose)? – caffeinated.tech Apr 16 '21 at 08:37
  • Node and Database both run on different ec2 instances. I tried the same query on Robo3t/MongoShell and Node which is `db.gateways.find({ org_id: 1 })`. When I used .explain('executionStats') in node it was relatively faster(76ms from 6000ms), maybe because it didn't return any documents. – soothsayer Apr 16 '21 at 10:03
  • I suspect the query didn't take 6000ms, that was the time to send all those documents via the network, or possibly to render them to a webpage? How are you measuring that time? This `.explain('executionStats')` does execute the query, so I reckon the query is fine and delay is elsewhere. How big are these documents? And does robo3t actually display the documents after 5ms, or does it take it a few seconds to render? Try testing with the same return size and document size on all platforms: `db.gateways.find({ org_id: 1 }, {_id: 0}).limit(10)` – caffeinated.tech Apr 16 '21 at 10:20
  • @soothsayer Can you please update the question with the query you are using? – Avani Khabiya Apr 16 '21 at 10:33
  • Does this answer your question? [Why is my mongodb call so slow?](https://stackoverflow.com/questions/10418558/why-is-my-mongodb-call-so-slow) – Avani Khabiya Apr 16 '21 at 10:36
  • Please check if these helps https://stackoverflow.com/questions/10418558/why-is-my-mongodb-call-so-slow and https://stackoverflow.com/questions/57789926/mongodb-nodejs-native-drivermongodb-vs-mongo-shell-performance – Avani Khabiya Apr 16 '21 at 10:37
  • Thank you @AvaniKhabiya I'll try those methods and update here. – soothsayer Apr 16 '21 at 11:31
  • Yes you are right @caffeinated.tech. I checked mongo logs and the query is not taking 6000ms. I think it's some problem with mongoose or maybe the documents are too large for the network. – soothsayer Apr 16 '21 at 11:31
  • @caffeinated.tech After I applied limit(10) in my node application performance has significantly improved to 50ms. So my network is slow I guess ? – soothsayer Apr 16 '21 at 11:39
  • It looks like large documents over a slow network. You can get an accurate time for the code to execute with https://nodejs.org/docs/v0.8.0/api/all.html#all_process_hrtime – caffeinated.tech Apr 16 '21 at 11:46

0 Answers0