Reputation: 18239
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))
);
Upvotes: 2
Views: 3974
Reputation: 12187
Calling cursor.Count() transfers no data from the server to your application. It sends a command to the server and the count is performed on the server, and only a tiny packet comes back from the server containing the numeric result of the count.
Not sure why iterating over the documents is taking that much longer than a simple count. One reason could be that the server is able to compute the count using only an index, but that when you actually iterate over the documents the server would have to fetch every single document from disk if it was not already paged into memory.
It is unlikely to be any bottleneck in the C# driver deserialization code as that is quite fast.
If you can provide a sample program that demonstrates the observed behavior I would be happy to try and reproduce your results.
Upvotes: 3
Reputation: 9210
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.
Upvotes: 1