I installed the lastest MongoDB 64 bit DB and official C# driver as of 13 Marh 2012. I am getting some unexpected performace results with cursors.
The following code will retrieve and loop through 500,000 records at about 26.8 k / sec on my Core 2 Duo 2 GHz laptop:
var query = Query.EQ("_H._t", "Car");
var cursor = mc.FindAs<RoctObj>(query);
double priceTot = 0d;
foreach (RoctObj item in cursor)
{
Car car = (Car)item._H;
priceTot += car.Price;
}
That seems reasonable. Next, I adjusted the query so that only 721 results are returned. The code takes over 1.1 seconds longer to execute than if the foreach segment is replaced with:
long i = cursor.Count();
Given the speed of the first example, 721 records should only take a fraction of a second to iterate. I know there are some other overheads, but they should be that bad. I don’t understand why I am getting +1.1 seconds.
Any ideas?
EDIT
Here is the alternate query. Note that the query time isn’t the question. It’s the iteration time.
var query = Query.And(
Query.LTE("_H.Price", BsonDouble.Create(80000d)).GTE(BsonDouble.Create(40000d)),
Query.LTE("_H.Cylinders", BsonDouble.Create(8d)).GTE(BsonDouble.Create(4d)),
Query.LTE("_H.Capacity", BsonDouble.Create(3000d)).GTE(BsonDouble.Create(2000d)),
Query.LTE("_H.TopSpeed", BsonDouble.Create(200d)).GTE(BsonDouble.Create(100d))
);
MongoDB does not return all the results at once, it returns a cursor which reads data off the database one record at a time, as your application asks for it (i.e. during your iterations) which may be why it is slower.
Running a
count()simply returns the amount of matches found but without data.