First, compare apples to apples: Reading and writing with MongoDB is like reading and writing by primary key once in a table without non-clustered indexes in an RDBMS.
So let's compare this: http://mysqlha.blogspot.de/2010/09/mysql-versus-mongodb-yet-another-silly.html
And it turns out that the difference in speed in a fair comparison of the exact same primitive operation is small. MySQL is actually a little faster. I would say that they are equivalent.
Why? Because in fact, both systems do similar things in this particular test. Returning a single row, searching by primary key, actually doesnโt work that much. This is a very fast operation. I suspect that the overhead of interprocess communication is a significant part of it.
I assume that the more customizable code in MySQL outweighs the slightly less systematic overhead of MongoDB (without logical locks and possibly some other small things).
This leads to an interesting conclusion: You can use MySQL as a document database and get excellent performance.
If the interviewer said: โWe donโt need documents or styles, we just need a much faster database, do you think we should use MySQL or MongoDB?โ, What would I answer?
I would recommend briefly ignoring performance and looking at the relative strength of the two systems. Things like scaling (way up) and replication are reminiscent of MongoDB. For MySQL, there are many more features, such as rich queries, concurrency models, advanced snap-in and maturity, and much more.
Basically, you can trade features for performance. Are you ready to do this? This is a choice that cannot be made at all. If you choose performance at all costs, consider tuning MySQL before adding another technology.
This is what happens when a client retrieves a single line / document using a primary key. I will comment on the differences between both systems:
- Client creates a binary command (same)
- The client sends it over TCP (same)
- The server parses the command (same)
- The server accesses the query plan from the cache (only SQL, not MongoDB, not HandlerSocket)
- The server requests a B-Tree component to access the row (same)
- Server accepts readonly physical lock on B-Tree path leading to row (same)
- The server accepts a logical lock in the string (only SQL, not MongoDB, not HandlerSocket)
- The server serializes the string and sends it over TCP (same)
- Client deserializes it (same)
There are only two additional steps for typical RDBMS SQL databases. That is why there really is no difference.