I have small size Collection with ~3000 records like:{ "_id" : ObjectId( "503aa4deeee88890b069daa4" ),
"active" : "Y",
"name" : "CL",
"pid" : 20,
"sid" : 2448,
"slug" : "acura-cl",
"updated" : Date( 1346020574139 ) }And simple console application :Mongo mongo = new Mongo();
DB db = mongo.getDB("database");
DBCollection collection = db.getCollection("importing.website.models");
Long date1 = Calendar.getInstance().getTimeInMillis();
DBCursor cursor = collection.find();
try {
while(cursor.hasNext()) {
cursor.next();
}
} finally {
cursor.close();
}
Long date2 = Calendar.getInstance().getTimeInMillis();
System.out.println(date1);
System.out.println(date2);
System.out.println(date2 - date1);After starting the application
1346045783973 Date1
1346045784283 Date2
310 msIn Mongo Server log:Mon Aug 27 09:36:24 [conn21] run command admin.$cmd { isMaster: 1 }
Mon Aug 27 09:36:24 [conn21] command admin.$cmd command: { isMaster: 1 } ntoreturn:1 reslen:71 0ms
Mon Aug 27 09:36:24 [conn21] query database.importing.website.models nreturned:101 reslen:11727 0ms
Mon Aug 27 09:36:24 [conn21] getmore database.importing.website.models cursorid:6025217777975209934 nreturned:2624 reslen:307474 1ms
Mon Aug 27 09:36:24 [conn21] end connection 127.0.0.1:52587I run this on macbook pro with corei5, ssd disk.1ms in Mongo Server vs 210-230 ms in java application? This is the normal 200% overhead for Java?In a web application for Spring-Data and Spring-MVC handling similar request reaches 300-350 milliseconds, that worries me.Or am I doing something wrong? Please help me with this issue.
> db.importing.website.models.find().explain()
{
"cursor" : "BasicCursor",
"nscanned" : 2725,
"nscannedObjects" : 2725,
"n" : 2725,
"millis" : 1,
"nYields" : 0,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
}
}
>
for (int c = 1; c <= 10; c++) {
System.out.println("\n");
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("Query = " + c);
Long date1 = Calendar.getInstance().getTimeInMillis();
DBCursor cursor = collection.find().limit(3000);
try {
while (cursor.hasNext()) {
cursor.next();
}
} finally {
cursor.close();
}
Long date2 = Calendar.getInstance().getTimeInMillis();
System.out.println("Time Count = " + (date2 - date1));
try {
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
public class App {
public static void main(String[] args) {
try {
Mongo mongo = new Mongo();
DB db = mongo.getDB("database");
for (int c = 1; c <= 10; c++) {
System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
System.out.println("Query = " + c);
Long date1 = Calendar.getInstance().getTimeInMillis();
DBCollection collection = db.getCollection("importing.website.models");
DBCursor cursor = collection.find().limit(-1).skip( 1 + (int)(Math.random() * ((2500 - 1) + 1)) );
try {
while (cursor.hasNext()) {
cursor.next();
}
} finally {
cursor.close();
}
Long date2 = Calendar.getInstance().getTimeInMillis();
System.out.println("Time Count = " + (date2 - date1));
try {
Thread.currentThread().sleep(1000);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (MongoException e) {
e.printStackTrace();
}
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 1
Time Count = 47
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 2
Time Count = 3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 3
Time Count = 2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 4
Time Count = 3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 5
Time Count = 3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 6
Time Count = 1
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 7
Time Count = 3
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 8
Time Count = 2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 9
Time Count = 2
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 10
Time Count = 2
You will want to do an explain to get how your queries are actually performing, check for proper index use etc. The getmore you see with a 1ms response time in the log is not the query you are running. By default your query will only show up in the log if it is slower than 100ms (the default, you can alter with --slowms on startup or turn on/tweak the settings with the profiler).
The more data, the longer the first one request. Why?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 1
Time Count = 325
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 2
Time Count = 172
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 3
Time Count = 127
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 4
Time Count = 62
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 5
Time Count = 30
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 6
Time Count = 31
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 7
Time Count = 11
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 8
Time Count = 15
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 9
Time Count = 13
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Query = 10
Time Count = 22
$time1 = microtime();
echo $cursor->count() . " document(s) found.\n";
foreach ($cursor as $obj) {
}
$time2 = microtime();
print ($time2 - $time1) * 1000;
bash-3.2$ php54 mongotest.php
2725 document(s) found.
14.795