为什么MongoDB比SQL DB快得多的任何详细和特定原因? [英] Any detailed and specific reasons for Why MongoDB is much faster than SQL DBs?
问题描述
好吧,关于为什么MongoDB这么快
我很感谢那些答案,但是它们很笼统.是的,我知道:
I appreciate those answers, however, they are quite general. Yes, I know:
- MongoDB是基于文档的,那么为什么基于文档可以导致 更高的速度?
- MongoDB是noSQL,但是为什么noSQL意味着更高的性能?
- 在一致性,ACID等方面,SQL比MongoDB要做的更多,但是我相信MongoDB还在做类似的事情来保持数据安全,维护索引等,对吧?
- MongoDB is document-based, then why being document-based can lead to much higher speed?
- MongoDB is noSQL, but why noSQL means higher performance?
- SQL does a lot more than MongoDB for consistency, ACID, etc, but I believe MongoDB is also doing something similar to keep data safe, maintain indexing, etc, right?
好吧,我写这个问题只是为了找出
Ok, I write this question just in order to find out
- MongoDB高性能的详细原因是什么?
- 确实是什么SQL,但是MongoDB没有,因此它获得了很高的性能?
- 如果访问员(MongoDB和SQL专家)问您
"Why MongoDB is so fast"
,您将如何回答?显然,仅回答:"because MongoDB is noSQL"
是不够的.
- what are the detailed and specific reasons for MongoDB's high performance?
- What exactly SQL does, but MongoDB does not do, so it gains very high performance?
- If an interviewer (a MongoDB and SQL expert) asks you
"Why MongoDB is so fast"
, how would you answer? Obviously just answering:"because MongoDB is noSQL"
is not enough.
谢谢
推荐答案
首先,让我们将苹果与苹果进行比较:使用MongoDB进行的读取和写入就像通过主键在没有非聚集表的表上进行的单一读取和写入一样.在RDBMS中建立索引.
First, let's compare apples with apples: Reads and writes with MongoDB are like single reads and writes by primary key on a table with no non-clustered indexes in an RDBMS.
因此,让我们精确地进行基准测试: http: //mysqlha.blogspot.de/2010/09/mysql-versus-mongodb-yet-another-silly.html
So lets benchmark exactly that: http://mysqlha.blogspot.de/2010/09/mysql-versus-mongodb-yet-another-silly.html
事实证明,在完全相同的原始操作的公平比较中,速度差异并不大. 实际上,MySQL的速度稍快.我想说的是,它们是等效的.
And it turns out, the speed difference in a fair comparison of exactly the same primitive operation is not big. In fact, MySQL is slightly faster. I'd say, they are equivalent.
为什么?因为实际上,在该特定基准测试中,两个系统都在做类似的事情.通过主键搜索返回单行实际上并没有那么多工作.这是一个非常快的操作.我怀疑跨进程通信开销是其中很大的一部分.
Why? Because actually, both systems are doing similar things in this particular benchmark. Returning a single row, searched by primary key, is actually not that much work. It is a very fast operation. I suspect that cross-process communication overheads are a big part of it.
我的猜测是,MySQL中经过更优化的代码要比MongoDB的系统开销少一些(没有逻辑锁,可能还有其他一些小东西).
My guess is, that the more tuned code in MySQL outweighs the slightly less systematic overheads of MongoDB (no logical locks and probably some other small things).
这会得出一个有趣的结论:您可以将MySQL用作文档数据库,并从中获得出色的性能.
This leads to an interesting conclusion: You can use MySQL like a document database and get excellent performance out of it.
如果面试官说:我们不在乎文档或样式,我们只需要一个更快的数据库,您认为我们应该使用MySQL还是MongoDB?",我会怎么回答?
If the interviewer said: "We don't care about documents or styles, we just need a much faster database, do you think we should use MySQL or MongoDB?", what would I answer?
我建议暂时不考虑性能,并查看两个系统的相对强度.对于MongoDB,诸如扩展(扩展)和复制之类的事情已经浮现在脑海.对于MySQL,还有很多功能,例如丰富的查询,并发模型,更好的工具和成熟度等等.
I'd recommend to disregard performance for a moment and look at the relative strength of the two systems. Things like scaling (way up) and replication come to mind for MongoDB. For MySQL, there are a lot more features like rich queries, concurrency models, better tooling and maturity and lots more.
基本上,您可以以功能换取性能.愿意这样做吗?这是通常无法做出的选择.如果您不惜一切代价选择性能,请在添加其他技术之前先考虑对MySQL进行调整.
Basically, you can trade features for performance. Are willing to do that? That is a choice that cannot be made generally. If you opt for performance at any cost, consider tuning MySQL first before adding another technology.
当客户端通过主键检索单个行/文档时,会发生这种情况.我将注释这两个系统之间的差异:
Here is what happens when a client retrieves a single row/document by primary key. I'll annotate the differences between both systems:
- 客户端构建一个二进制命令(相同)
- 客户端通过TCP发送(相同)
- 服务器解析命令(相同)
- 服务器从缓存访问查询计划(仅SQL,不是MongoDB,不是HandlerSocket)
- 服务器要求B-Tree组件访问该行(相同)
- 服务器在通往该行(相同)的B-Tree路径上采用物理只读锁
- 服务器对行进行逻辑锁定(仅SQL,不是MongoDB,不是HandlerSocket)
- 服务器对行进行序列化并通过TCP(相同)发送
- 客户端反序列化(相同)
对于典型的基于SQL的RDBMS,仅需执行两个附加步骤. 这就是为什么没有真正区别的原因.
There are only two additional steps for typical SQL-bases RDBMS'es. That's why there isn't really a difference.
这篇关于为什么MongoDB比SQL DB快得多的任何详细和特定原因?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!