Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does collections.find({}) takes over 9 secs for 250 objects (MongoMapper)

I am running the following query and it is take on average 9 seconds to return the results. There are no filters on it, so I am not sure an index would help. Why is this running so slowly? There are only 250 objects in there, and only 4 fields (all text).

Country.collection.find({},:fields => ['country_name', 'country_code']).to_json

"cursor":"BasicCursor",
"nscanned":247,
"nscannedObjects":247,
"n":247,
"millis":0,
"nYields":0,
"nChunkSkips":0,
"isMultiKey":false,
"indexOnly":false,
"indexBounds":{},
"allPlans":[{"cursor":"BasicCursor","indexBounds":{}}]

The cpu, memory and disk on the machine do not even notice the query run. Any help would be appreciated.

like image 453
ABrowne Avatar asked Oct 22 '22 09:10

ABrowne


1 Answers

Create indexes on the 'country_name' fiels using :

db.countries.ensureIndex({country_name:1});

That will speed your query enormously You can learn more about indexes here

PS- you can type 'it' to display more when you see the 'has more' phrase, or you can display all the result without the 'has more' by using:

db.countries.find({}, {'country_name' : 1, 'country_code' : 1}).forEach(printjson)

and you can always set the profiler by using:

>use databaseName;
> db.setProfilingLevel(2); // 2 tell the profiler to catch everything happened inside the DB

You can learn more about profiler here

and you can display the data inside the profiler using

> db.system.profile.find()

This method will give you more info about your database and what's going on inside.

like image 133
mongotop Avatar answered Nov 15 '22 05:11

mongotop