C# mongodb driver execution of UpdateOneAsync takes a long time

Hello,

I’m having a long execution time on UpdateOneAsync in c# driver. From data analytics I can check that the execution in the mongo db itself only took 100ms, but when I stopwatch the UpdateOneAsync in the code it took 3.5 secs!

I wonder what’s wrong?

here’s the code

Thanks!

Hi, @Alvi_Andi,

Welcome to the MongoDB Community Forums. I understand that you are trying to understand why an update on the server only takes 100ms but wall time is closer to 3.5 seconds.

The server time only includes the time required to lookup the matching document and perform the write. The wall time will potentially include:

  • creation of MongoClient, connection pools, and monitoring connections
  • TCP connection establishment including DNS lookups including SRV, TXT, and A record lookups
  • TLS handshake
  • authentication
  • server selection
  • serialization of the dictionary (could be significant if it contains a large number of keys/values or is deeply nested)
  • possibly a retry of the write if the primary changed during the test

The first 4 items are amortized if you reuse your {{MongoClient}}. As a first step, I would suggest running the test method a few times within the same timing run to see if calls after the first are faster. It would also be useful to profile the method call to determine where the time is being spent.

Hope that helps!

Sincerely,
James

Hi,

Thank you for the answer. Yes, I am reusing the Mongo Client. The serialization itself is not deep and not large. Or is my method of how replacing one array of data wrong?

I will try multiple runs in my local too.

Thanks!