MongoDB performance is slower with C # driver, not command line

I am researching MongoDB to check if we can / will use it as a noSQL store for our product. Everything seems to be working fine, but it is rather slow in our C # application. Queries take 120 to 140ms to return results, which is unacceptable for our application (more than 10x slower compared to MS SQL). At first I thought it might have something to do with the operator $in

and the fields Guid

that I was filtering, but it doesn't.

At the moment I have created a small benchmark to measure the performance of a specific query. The method looks like this:

public IQueryable<T> QueryTest<T>(string collection, IEnumerable<Guid> shouldBeIn, string fieldName)
{
    var dataModelCollection = MongoDatabase.GetCollection<T>(collection);
    var findResult = dataModelCollection
                            .FindAll()
                            .ToList();  //Doing this on purpose, so the query is executed immediatly. Just for this test!
                                        //This action takes about 110~140ms.
    return findResult.AsQueryable();
}

      

Obviously this is test code as I would not be ToList()

in real code and would return later IQueryable

.

When I run this query in MongoDB console, it returns 0ms result (as explained)

> db.StoreData.find().explain()
{
        "cursor" : "BasicCursor",
        "isMultiKey" : false,
        "n" : 21,
        "nscannedObjects" : 21,
        "nscanned" : 21,
        "nscannedObjectsAllPlans" : 21,
        "nscannedAllPlans" : 21,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 0,
        "server" : "mymongodbserver:27017",
        "filterSet" : false
}

      

There is an index in the field here _id

, and also one in the field StoreId

, because I want to filter later StoreId

.

Both C # and Console requests run on the same machine. I also ran the test several times and ran the command several times .reIndex()

.

Any ideas on how to proceed?

change

It is a very small database with very few test data / records. As requested, there are 21 entries , I think the result of the method explain()

also reports this. I also ran toArray()

. I don't understand how this helps, but this is part of the result, since copying 21 objects here is probably not very useful.

{
        "_id" : ObjectId("542e847e048b8b0f704a7834"),
        "StoreId" : BinData(3,"/FQLn0k/hkSofM9WvEsNKQ=="),
        "Shelves" : [
                {
                        "ShelveId" : BinData(3,"iCDhfhi6z0adh2haWjrzoQ=="),
                        "Name" : "Shelve 1",
                        "Type" : 0
                }
        ],
        "Doors" : [ ]
},
{
        "_id" : ObjectId("542e847e048b8b0f704a7835"),
        "StoreId" : BinData(3,"p7TkqeFrGEOAWtv0RZ4YjQ=="),
        "Shelves" : [ ],
        "Doors" : [ ]
},

      

edit2

I added some more data to the collection StoreData

to see if returning a large number (100,000) of documents is faster or slower. In MongoDB ( mongodb.exe

) client I am getting huge fast results.

> db.StoreData.find().explain()
{
        "cursor" : "BasicCursor",
        "isMultiKey" : false,
        "n" : 100021,
        "nscannedObjects" : 100021,
        "nscanned" : 100021,
        "nscannedObjectsAllPlans" : 100021,
        "nscannedAllPlans" : 100021,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 781,
        "nChunkSkips" : 0,
        "millis" : 53,
        "server" : "cp-crossbario:27017",
        "filterSet" : false
}

      

As you can see, about 100,000 results are returned in 53 milisecconds .

Running the method FindAll().ToList()

with the MongoDB driver results in the entire document in 5075 milliseconds . Quite a difference!

A little more background information on the machines I run on: I created two Azure Standard_A2 machines (2 cores, 3.5GB memory) in the Western Europe region.

One of the machines is running a MongoDB database. I installed this on C:\MongoDB

and the data directory on C:\MongoDB\bin\data

. This is just for testing. No real production system should set there a date on this disc, of course.

The testing database is about 135 MB.

On another machine, I have test clients installed, a console application using the MongoDB C # driver and the MongoDB.exe file. Both connect to the database through the same HTTP endpoint.

Edit3

I just ran the method QueryTest

without <T>

, so now I am returning 'raw' BsonDocument

s. Strange, it's even slower. 100.000 documents now return ~ 8000ms, returning POCOs they return in ~ 5000ms.

+3


source to share


2 answers


The problem with your mapping in the shell is that the shell, when run with .explain (), does not actually discard the data. In fact, you've removed the entire networking stack. So the 53 milligrams you see is the time it takes for the server to process all the data. Since you're on Azure, I'm pretty sure the network will account for most of this overhead. You can prove this by simultaneously running a network capture and looking at how long TCP requests take.



Also, when querying so many results, MongoDB sends the results in batches. This means that it is not just one round trip network. Rather, there are a few of them, each of which adds time overhead.

+4


source


I got 100,000 records in 1411ms, the problem must be something else like crashing or ... If you want to use it in a foreach loop you can avoid the ".ToList ()" code by using the result as MongoCursor



+1


source







All Articles