关系与非关系数据建模 - 有什么区别 [英] Relational vs Non-Relational Data Modeling - what's the difference

查看:182
本文介绍了关系与非关系数据建模 - 有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是数据库新手,从来没有和任何RDBMS合作过。但是我得到关系数据库的基本思想。至少我认为我是这样做的 - )



假设我有一个用户数据库,每个用户都有以下属性:



  • 用户

    • id

    • 名称

    • 邮编

    • 城市


blockquote>

在一个关系数据库中,我会以一个名为 user



  • 用户

    • id
    • 名称

    • location_id



,并有第二张名为位置



  • 位置

    • id

    • 邮编

    • / li>


location_id 是对位置表中的条目的外键(引用)。如果我理解正确,那么优势就在这里,如果某个城市的邮政编码发生变化,我只需要更改一个条目。



所以,我们来看看非关系型数据库,我开始使用Google App Engine。在这里,我真的会模拟它,就像它在规格中首先写下来一样。我有一种用户

  class User(db.Model) :
name = db.StringProperty()
zip = db.StringProperty()
city = db.StringProperty()

好处是我不需要连接两个表,但缺点是,如果邮政编码发生变化,我必须运行一个脚本用户输入并更新邮政编码,是否正确?



因此,现在Google App Engine中有另一个选项,它使用 ReferenceProperties 。我可以有两种:用户位置



<$ p
$ city $ db









$ db.Model):
name = db.StringProperty()
location = db.ReferenceProperty(Location)

如果我没有错,现在我的模型与上述关系数据库中的模型完全相同。我现在想知道的是,首先,这是错误的,我刚刚做了,并且这会破坏非关系数据库的所有优点。我明白,为了得到zip和城市的价值,我必须运行第二次查询。但在另一种情况下,要改变邮政编码,我必须通过所有现有的用户。



那么这两种建模可能性的含义是什么?非关系型数据库,如Google的数据存储。对于它们两者来说,典型的用例是什么,这意味着什么时候应该使用一个,何时使用另一个。

另外一个问题是,如果在非关系数据库中我可以建模与关系数据库中的模型完全相同,为什么我应该使用关系数据库?



对不起,如果其中一些问题听起来很朴素,但我相信他们会帮助一些对数据库系统不熟悉的人加以理解。

经验,最大的区别在于非关系型数据存储迫使您基于您的查询方式进行建模,因为缺少联接以及您将如何编写,因为交易限制。这当然会导致非常规化的模型。过了一段时间,我开始定义所有的查询第一,以避免以后重新考虑模型。由于关系数据库的灵活性,您可以分别考虑每个数据族,在它们之间建立关系,并在最后查询您希望如何(滥用联接)很多情况下)。

I'm new to databases and I've never worked with any RDBMS. However I get the basic idea of relational databases. At least I think I do ;-)

Let's say I have a user database with the following properties for each user:

  • user
    • id
    • name
    • zip
    • city

In a relational database I would for example model it in a table called user

  • user
    • id
    • name
    • location_id

and have a second table called location

  • location
    • id
    • zip
    • city

And location_id is a foreign key (reference) to an entry in the location table. If I understand it right the advantage is here, if the zip code for a certain city changes I only have to change exactly one entry.

So, let's go to the non-relational database, where I started to play around with Google App Engine. Here I would really model it like it was written down first in the specifications. I have a kind user:

class User(db.Model):
    name = db.StringProperty()
    zip = db.StringProperty()
    city = db.StringProperty()

The advantage is that I don't need to join two "tables", but the disadvantage is, that if the zip code changes I have to run a script that goes through all user entries and updates the zip code, correct?

So, now there is another option in Google App Engine, which is to use ReferenceProperties. I could have two kinds: user and location

class Location(db.Model):
    zip = db.StringProperty()
    city = db.StringProperty()

class User(db.Model):
    name = db.StringProperty()
    location = db.ReferenceProperty(Location)

If I'm not wrong I now have exactly the same model as in the relational database described above. What I'm wondering now is, first of all, is that wrong what I just did and does that destroy all the advantages of a non-relational database. I understand, that in order to get the value of zip and city I have to run I second query. But in the other case, to make a change in the zip code I have to run through all existing users.

So what are the implications of these two modeling possibilities in a non-relational database like Google's datastore. And what are typical use cases for both of them, meaning when should I use one and when the other.

Also as an additional question, if in a non-relation database I can model exactly the same what I can model in a relational database, why should I use a relational database at all?

Sorry if some of these questions sound naive, but I'm sure they will help a couple people, who are new to database systems to get a better understanding.

解决方案

In my experience, the biggest difference is that non-relational datastores force you to model based on how you'll query, because of the lack of joins, and how you'll write, because of the transaction restrictions. This of course results in very denormalized models. After a while, I started to define all the queries first, to avoid having to rethink the models later.

Because of the flexibility of relational db's, you can think about each data family in separate, create relations between them and in the end query how you wish (abusing joins in so many cases).

这篇关于关系与非关系数据建模 - 有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆