I'm trying to understand how much time do some basic queries to my MongoDB collections (around 200k documents) take and i don't understand why according to the MongoDB profiler the query takes around 15 milliseconds, while from Python the query is going to take from 1 to 2 seconds.
In order to track the query from Python i did something very basic:
import time
from pymongo import MongoClient
client = MongoClient('')
db = client.my_db
start = time.time()
query = db['my_col'].find({'unix': {'$gte': 'some_timestamp' }})
data = list(query)
end = time.time()
print(end-start)
So here i'm just retrieving all the documents after a specific unix timestamp and then i convert the query to a Python list. The output of this code will range from >>1.01 to >>1.80 seconds on average, while according to the profiler the query takes just some milliseconds.
What am i missing here? Is it because what actually takes time is the loop through the cursor?
listwith all of the query result objects, the network time to send the results from the server to the client and the time to establish a connection. The MongoDB profiler will only include the time to execute the query. This code measures something different than what the profiler measures. If you're concerned about performance, try increasing the cursor batch size and ensure that pymongo is using the C extensions.