Mongodb 蒙戈“;手册参考”;性能与传统数据库相比’;s&x201C;表连接”;
根据通常首选的“手动引用”操作,那么当我想要使用关系集合查询实体时,我非常关心执行两次查询会带来多大的性能损失,特别是与传统的关系数据库相比——我们可以使用表联接在一次查询中检索预期结果 反规范化示例:Mongodb 蒙戈“;手册参考”;性能与传统数据库相比’;s&x201C;表连接”;,mongodb,Mongodb,根据通常首选的“手动引用”操作,那么当我想要使用关系集合查询实体时,我非常关心执行两次查询会带来多大的性能损失,特别是与传统的关系数据库相比——我们可以使用表联接在一次查询中检索预期结果 反规范化示例: db.blogs.insert({ _id: 1, title: "Investigation on MongoDB", content: "some investigation contents", post_date: Date.now(), permalink: "ht
db.blogs.insert({
_id: 1,
title: "Investigation on MongoDB",
content: "some investigation contents",
post_date: Date.now(),
permalink: "http://foo.bar/investigation_on_mongodb",
comments: [
{ content: "Gorgeous post!!!", nickname: "Scott", email: "foo@bar.org", timestamp: "1377742184305" },
{ content: "Splendid article!!!", nickname: "Guthrie", email: "foo@bar.org", timestamp: "1377742184305" }
]}
)
db.books.insert({
_id: 1,
name: "MongoDB Applied Design Patterns",
price: 35,
rate: 5,
author: "Rick Copeland",
ISBN: "1449340040",
publisher_id: 1,
reviews: [
{ isUseful: true, content: "Cool book!", reviewer: "Dick", timestamp: "1377742184305" },
{ isUseful: true, content: "Cool book!", reviewer: "Xiaoshen", timestamp: "1377742184305" }
]
}
);
db.publishers.insert({
_id: 1,
name: "Packtpub INC",
address: "2nd Floor, Livery Place 35 Livery Street Birmingham",
telephone: "+44 0121 265 6484",
}
);
我们可以简单地使用:db.blogs.find()获得我们想要的一切:带有评论的博客文章属于它们
规范化示例:
db.blogs.insert({
_id: 1,
title: "Investigation on MongoDB",
content: "some investigation contents",
post_date: Date.now(),
permalink: "http://foo.bar/investigation_on_mongodb",
comments: [
{ content: "Gorgeous post!!!", nickname: "Scott", email: "foo@bar.org", timestamp: "1377742184305" },
{ content: "Splendid article!!!", nickname: "Guthrie", email: "foo@bar.org", timestamp: "1377742184305" }
]}
)
db.books.insert({
_id: 1,
name: "MongoDB Applied Design Patterns",
price: 35,
rate: 5,
author: "Rick Copeland",
ISBN: "1449340040",
publisher_id: 1,
reviews: [
{ isUseful: true, content: "Cool book!", reviewer: "Dick", timestamp: "1377742184305" },
{ isUseful: true, content: "Cool book!", reviewer: "Xiaoshen", timestamp: "1377742184305" }
]
}
);
db.publishers.insert({
_id: 1,
name: "Packtpub INC",
address: "2nd Floor, Livery Place 35 Livery Street Birmingham",
telephone: "+44 0121 265 6484",
}
);
现在,如果我想获得关于一本书的完整信息,我必须手动查询两次,如下所示:
> var book = db.books.find({ "name": { $regex: 'mongo*', $options: 'i' } })
> db.publishers.find({ _id: book.publisher_id })
我所知道的是:优先操作将由Mongo“在内存中处理,但我将有以下总结问题:
简而言之:面向文档的数据库提倡“非规范化”数据以在一次查询中检索结果,但是,当我们必须存储关系数据时,它“建议”您使用“手动引用”,这意味着两次查询,而在关系数据库中,使用“表连接”只能进行一次查询
这对我来说毫无意义:)关系数据库还通过查询两个表来执行连接。但它的优点是,它可以在内部完成这项工作,而不必与客户机进行通信。它可以立即查询第二个表 MongoDB首先需要将第一个查询的结果发送到客户端,然后应用程序才能制定并将第二个查询发送回数据库。由此损失的时间为: