2

According to the official document: "manual reference" operation is usually preferred, experienced guy even suggest never use DBref, then I am seriously concerning how much of the performance penalty to do twice query when I want to query entities with relational collection, especially comparing with the traditional relational DB - we can retrieve the expected result within one query using table joins.

Denormalize example:

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" }
  ]}
               )

We can simply use: db.blogs.find() to get everything we want: blog posts with comments belong to them.

Normalize example:

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",
  } 
);

Now if I want to get the complete information about a single book I have to manually query twice, similar to below:

> var book =  db.books.find({ "name": { $regex: 'mongo*', $options: 'i' } })
> db.publishers.find({ _id: book.publisher_id })

Things I know is: the precedence operations will be process by Mongo "in memory", but I will have the summarized question below:

In simply words: document oriented database advocates "denormalize" data to retrieve result within one query, however, when we have to store relational data, it "suggest" you to use "manual reference", which means twice query, while in relational DB there will be only one time query by using "table joining".

This makes no sense for me:)

4

1 回答 1

1

关系数据库还通过查询两个表来执行 JOIN。但它的优点是它可以在内部执行此操作,并且不必与客户端进行通信即可执行此操作。它可以立即查询第二张表。

MongoDB 首先需要将第一个查询的结果发送给客户端,然后应用程序才能制定并将第二个查询发送回数据库。损失的时间是:

  1. 数据库服务器和应用程序服务器之间的网络延迟(几毫秒)
  2. 解释应用服务器上的响应并从中生成一个$in查询(几微秒)
  3. 应用服务器和数据库服务器之间的网络延迟(几毫秒)

根据应用程序服务器和数据库服务器的互连程度,我们在这里讨论几毫秒的惩罚。

于 2013-08-29T11:07:27.870 回答