Relational vs Non-Relational Data Modeling - what's the difference

前端 未结 3 1055
小蘑菇
小蘑菇 2021-02-01 19:36

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 dat

3条回答
  •  抹茶落季
    2021-02-01 20:30

    Imagine that GAE has two modes for the Datastore: RDMS-mode and non-RDMS-mode. If I take your ReferenceProperty example with the aim of "list all the users and all their zip codes" and write some code to print all of these.

    For the [fictional] RDMS-mode Datastore it might look like:

    for user in User.all().join("location"):
        print("name: %s zip: %s" % (user.name, user.location.zip))
    

    Our RDMS system has handled the de-normalisation of the data behind the senes and done a nice job of returning all the data we needed in one query. This query did have a little bit of overhead as it had to stitch together our two tables.

    For the non-RDMS Datastore our code might look like:

    for user in User.all():
        location = Location.get( user.location )†
        print("name: %s zip: %s" % (user.name, location.zip))
    

    Here the Datastore cannot help us join our data, and we must make an extra query for each and every user entity to fetch the location before we can print it.

    This is in essence why you want to avoid overly normalised data on non-RDMS systems.

    Now, everybody logically normalizes their data to some extent wether they are using RDMS or not, the trick is to find the trade off between convenience and performance for your use case.

    † this is not valid appengine code, I'm just illustrating that user.location would trigger a db query. Also no-one should write code like my extreme example above, you can work around the continued fetching of related entities by say fetching locations in batches upfront.

    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?

    relational-DB's excel at storing thousands-and-millions of rows of complex inter-related models of data, and allowing you to perform incredibly intricate queries to reform and access that data.

    non-RDB's excel at storing billions+ rows of simple data and allowing you to fetch that data with simpler queries.

    The choice should lie with your use-case really. The simpler structure of the non-relational model and design restraints that come with it is one of the main ways that AppEngine is able to promise to scale your app with demand.

提交回复
热议问题