MySQL 何时使用 MongoDB

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4981753/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:44:11  来源:igfitidea点击:

When to use MongoDB

mysqlmongodbdatabase-designrelational-databasedatabase

提问by dez

I'm writing an application that doesn't necessarily need scaling abilitiesas it won't be collecting large amounts data at the beginning. (However, if I'm lucky, I could down the road potentially.)

我正在编写一个不一定需要扩展能力的应用程序,因为它在开始时不会收集大量数据。(但是,如果我幸运的话,我可能会走上这条路。)

I will be running my web server and database on the same box (for now).

我将在同一个机器上运行我的网络服务器和数据库(现在)。

That being said, I am looking for performance and efficiency.

话虽如此,我正在寻找性能和效率。

The main part of my application will be loading blog articles. Using an RDBMS (MySQL) I will make 6 queries (2 of the queries being joins), just to load a single blog article page.

我的应用程序的主要部分将加载博客文章。使用 RDBMS (MySQL),我将进行 6 个查询(其中 2 个查询是连接),只是为了加载单个博客文章页面。

select blog
select blog_album
select blog_tags
select blog_notes
select blog_comments (join with users)
select blog_author_participants (join with users)

However, with MongoDBI can de-normalize and flatten 6 tables into just 2 tables/collections and minimizes my queries to potentially just one 1 query,

但是,使用MongoDB,我可以将 6 个表反规范化并将其展平为 2 个表/集合,并将我的查询最小化为可能只有一个 1 查询,

users
blogs
    ->blog_album
    ->blog_tags        
    ->blog_notes
    ->blog_comments
    ->blog_author_participants

Now, going with the MongoDB schema, there will be some data redundancy. However, hard drive space is cheaper than CPU/servers.

现在,使用 MongoDB 模式,会有一些数据冗余。但是,硬盘空间比 CPU/服务器便宜。

1.) Would this be a good scenario to use MongoDB?

1.) 这是使用 MongoDB 的好场景吗?

2.) Do you only benefit in performance using MongoDB when scaling beyond a single server?

2.) 当扩展到单个服务器之外时,您是否只使用 MongoDB 来提高性能?

3.) Are there any durability risks using MongoDB? I hear that there is potential for loss of data while performing inserts - as insert are written to memory first, then to the database.

3.) 使用 MongoDB 是否存在任何持久性风险?我听说在执行插入时可能会丢失数据 - 因为插入首先写入内存,然后写入数据库。

4.) Should this stop me from using MongoDB in production?

4.) 这会阻止我在生产中使用 MongoDB 吗?

采纳答案by MDaubs

However, with MongoDB I can de-normalize and flatten 6 tables into just 2 tables/collections and minimizes my queries to potentially just one 1 query

但是,使用 MongoDB,我可以将 6 个表反规范化并将其展平为 2 个表/集合,并将我的查询最小化为可能只有一个 1 查询

But you can easily query MySQL for 6 tables worth of information related to a single blog post with a single properly crafted SQL statement.

但是,您可以使用一个正确编写的 SQL 语句轻松查询 MySQL,以获取与单个博客文章相关的 6 个表的信息。

however hard drive space is cheaper than CPU/servers.

但是硬盘空间比 CPU/服务器便宜。

If performance and scaling is a priority then you are going to be concerned with having enough RAM to fit everything into main memory and enough CPU cores to run queries. An enterprise grade RAID 10 array is a requirement, don't get me wrong, but as soon as your database software (MongoDB or MySQL) needs to scan an index that can't fit into main memory you'll be in for a world of pain assuming a large active database. :)

如果优先考虑性能和扩展性,那么您将关心是否有足够的 RAM 来将所有内容放入主内存中,以及是否有足够的 CPU 内核来运行查询。企业级 RAID 10 阵列是必需的,不要误会我的意思,但是一旦您的数据库软件(MongoDB 或 MySQL)需要扫描无法放入主内存的索引,您就会陷入困境假设一个大型活动数据库的痛苦。:)

I like MongoDB, but it's big strength in my mind is map/reduce and its document-orientation. You require neither of those features. MySQL is time-tested in large scale deployments and supports partitioning (but I would argue that your database would have to be in the order of 50-100 GB before you can realize substantial gain from partitioning vs a single (plus passive backup) server with tons (64 GB+) of RAM. I would also argue that if performance is truly a concern then MySQL would be preferable as you would have supreme control over your indexes.

我喜欢 MongoDB,但我认为它的强大之处在于 map/reduce 及其面向文档。您不需要这些功能。MySQL 在大规模部署中经过时间考验并支持分区(但我认为您的数据库必须在 50-100 GB 的数量级才能从分区与单个(加上被动备份)服务器与吨 (64 GB+) RAM。我还认为,如果性能确实是一个问题,那么 MySQL 会更可取,因为您可以对索引拥有至高无上的控制权。

That's not to say that MongoDB isn't high performance, but its place probably isn't serving blogs. Your concern with inserts is valid as well. MongoDB is not an ACIDsystem. Google transactions in both systems and compare.

这并不是说 MongoDB 的性能不高,但它的位置可能不是为博客提供服务。您对插入的关注也是有效的。MongoDB 不是ACID系统。两个系统中的谷歌交易和比较。

回答by Dan Grossman

You would use MongoDB when you have a use case that matches its strengths.

当您有一个与其优势相匹配的用例时,您将使用 MongoDB。

Do you need a schema-less document store? Nope, you have a stable schema.

你需要一个无模式的文档存储吗?不,你有一个稳定的架构。

Do you need automatic sharding? Nope, you don't have extraordinary data needs or budget for horizontally scaling hardware.

你需要自动分片吗?不,您没有用于水平扩展硬件的特殊数据需求或预算。

Do you need map/reduce data processing? Not for something like a blog.

你需要map/reduce数据处理吗?不是为了博客之类的东西。

So why are you even considering it?

那你为什么还要考虑呢?

回答by gusridd

Here is a good explanation: http://mod.erni.st/nosql-if-only-it-was-that-easy/

这是一个很好的解释:http: //mod.erni.st/nosql-if-only-it-was-that-easy/

The last paragraph summarizes it:

最后一段总结了它:

What am I going to build my next app on? Probably Postgres. Will I use NoSQL? Maybe. I might also use Hadoop and Hive. I might keep everything in flat files. Maybe I'll start hacking on Maglev. I'll use whatever is best for the job. If I need reporting, I won't be using any NoSQL. If I need caching, I'll probably use Tokyo Tyrant. If I need ACIDity, I won't use NoSQL. If I need a ton of counters, I'll use Redis. If I need transactions, I'll use Postgres. If I have a ton of a single type of documents, I'll probably use Mongo. If I need to write 1 billion objects a day, I'd probably use Voldemort. If I need full text search, I'd probably use Solr. If I need full text search of volatile data, I'd probably use Sphinx.

我将在什么基础上构建我的下一个应用程序?可能是Postgres。我会使用 NoSQL 吗?也许。我可能还会使用 Hadoop 和 Hive。我可能会将所有内容都保存在平面文件中。也许我会开始对 Maglev 进行黑客攻击。我会使用最适合这份工作的任何东西。如果我需要报告,我不会使用任何 NoSQL。如果我需要缓存,我可能会使用东京暴君。如果我需要 ACIDity,我不会使用 NoSQL。如果我需要大量计数器,我将使用 Redis。如果我需要交易,我会使用 Postgres。如果我有大量单一类型的文档,我可能会使用 Mongo。如果我每天需要写 10 亿个对象,我可能会使用 Voldemort。如果我需要全文搜索,我可能会使用 Solr。如果我需要对易失性数据进行全文搜索,我可能会使用 Sphinx。

回答by Wolph

NoSQL vs. RDBMS: Apples and Oranges?

NoSQL 与 RDBMS:苹果和橙子?

I would advise you to read up a little on what NoSQL is and what it does before you decide whether you can use it. You can't take a normal database and turn it into a NoSQL thing just like that. The way you work with the data is completely different.

在决定是否可以使用 NoSQL 之前,我建议您先阅读一下什么是 NoSQL 以及它的作用。你不能把一个普通的数据库变成一个 NoSQL 的东西。您处理数据的方式完全不同。

NoSQL definitely has its uses. But it's definitely not the answer for everything. The main advantage of NoSQL is the easily changeable data model.

NoSQL 肯定有它的用途。但这绝对不是所有问题的答案。NoSQL 的主要优点是易于更改的数据模型。

回答by Ravindra babu

Advantages of using mongodb( as per Moshe Kaplanpublished in dzonearticle)

使用 mongodb 的优势(根据文章中Moshe Kaplan发布的dzone内容

  1. Schema-less design
  2. Scalability in managing Tera bytes of data
  3. Rapid replicaSet with high availability feature
  4. Sharding enables linear and scale out growth w/o running out of budget
  5. Support high write load
  6. Use of Data locality for query processing
  1. 无模式设计
  2. 管理 Tera 字节数据的可扩展性
  3. 具有高可用特性的快速复制集
  4. 分片可在预算不足的情况下实现线性和横向扩展
  5. 支持高写入负载
  6. 使用数据局部性进行查询处理

MongoDB meets Consistency& Partitioningrequirements in CAP theory ( Consistency, Availability and Partitioning)

MongoDB的满足ConsistencyPartitioning要求,CAP理论(一致性,可用性和分区)

Related SE questions:

相关 SE 问题:

What are the advantages of using a schema-free database like MongoDB compared to a relational database?

与关系数据库相比,使用像 MongoDB 这样的无模式数据库有哪些优势?

When to Redis? When to MongoDB?

什么时候使用Redis?什么时候使用 MongoDB?