SlideShare a Scribd company logo
Modelling data in a
schema-free world?
          FrOSCon
         2012-08-26

   Jan Steemann (triAGENS)




              © 2012 triAGENS GmbH | 2012-08-26   1
About me

   Professional database developer @ triAGENS GmbH, Cologne,
    Germany
   Interested in all kind of databases
   Currently working on an open source mixed-model nosql database




                                      © 2012 triAGENS GmbH | 2012-08-26   2
Schemas in relational databases

   Relational databases are around for a long time already
   In most relational databases we can deal with these schema elements:
       tables                           relationships
       columns / fields                 views
       data types                       triggers
       indexes                          procedures / functions
   Schema elements are auxiliary means to organise and validate the
    actual data!




                                    © 2012 triAGENS GmbH | 2012-08-26   3
Normalisation in relational databases

   Relational databases like your data to be in a highly normalized and
    de-duplicated form
   Put simple, normalisation means
       dividing big entities (tables, fields) into smaller ones
       defining relationships between them
   Example data, not normalised:
        user_id   user_name group_name         status
           1          foo       Power user     Active
           2          bar         Rookie      Inactive
           3          baz         Rookie       Active




                                                 © 2012 triAGENS GmbH | 2012-08-26   4
Normalisation in relational databases

   Example data, normalised (relationships not depicted):
                Table „users“                Table „groups“                      Table „status“
          id    name    group    status       id      name                        id     name
          1      foo      1        1          1       Power user                 1       Active
          2      bar      2        2          2         Rookie                   2      Inactive
          3      baz      2        1

   Normalisation helps to
       reduce redundancies and storage requirements
       increase consistency and integrity
   Normalised data is re-arranged on retrieval using joins, projections etc.
   Due to normalisation, data may be fetched from several tables

                                             © 2012 triAGENS GmbH | 2012-08-26                     5
Issues with relational databases

   Issues of relational databases:
       scaling is hard:
        This is due to the ACID (atomicity, consistency, isolation, durability) guarantees
        they usually provide.
       rigid database schemas can obstruct rapid application development:
        Initial and ongoing database schema development and evolution takes time,
        but you must do it in order to have your database accept any data at all,
        designing good schemas is hard
       fixed database schemas block deployment of changes:
        When deploying application changes, the database schema needs to stay in sync
        with the application. Deploying application changes may even cause downtime due
        to long-running database migrations (e.g. ALTER TABLE statements)




                                                © 2012 triAGENS GmbH | 2012-08-26            6
Issues with relational databases

   Some more issues:
       static schemas do not play well with dynamically typed languages:
        many languages support dynamic keys for their container types, dynamic
        variables, run-time typing etc. To use this with a relational database, one needs
        workarounds (e.g. entity-attribute value modelling, auxilliary tables) that increase
        complexity and make it slow
       saving multi-valued data is „forbidden“:
        Mutli-value containers (e.g. hashes, dictionaries, lists, arrays) are used
        frequently in programming but must not be saved in the database as such
        Complex workarounds are necessary to handle that
       saving objects can be expensive:
        saving one object in a relational way may create a lot of noise in relational
        databases (though this may be obscured by frameworks)




                                                © 2012 triAGENS GmbH | 2012-08-26         7
NoSQL databases – why?

   In the past 5 years, many new database products have emerged
   The common term for these new products is „NoSQL“ databases,
    mainly interpreted as „not only SQL“
   „non-relational“ is also a good name for them because almost no
    NoSQL database follows the relational model
   Instead, most NoSQL databases overcome specific issues that
    relational databases have, especially
       scaling
       schema rigidity




                                    © 2012 triAGENS GmbH | 2012-08-26   8
NoSQL databases – scaling

   Scaling problems of relational databases are addressed in most
    NoSQL databases by reducing ACID guarantees and removing
    relational features:
       Atomicity is guaranteed only for some operations, not all
       Isolation is only guaranteed for some operations
       Strict consistency is relaxed to eventual consistency
       Strict durability is normally turned off (but is configurable by admin)
       Don't offer referential integrity or do not enforce it
       Complex data operations as joins etc. are highly restricted or completely
        disallowed
   These measures help to reduce locking and synchronisation
    overhead and make scaling possible at all

                                                  © 2012 triAGENS GmbH | 2012-08-26   9
NoSQL databases – schema rigidity

   Most NoSQL databases lift the requirement of having to define
    explicit database schemas:
       Some NoSQL databases don't use schemas at all, they are fully schema-free
       Some require the user to only predefine highest level schema elements
        (e.g. databases, collections, column families) upfront (and some allow it as you go)
       Some use schemas implicitly by analysing incoming data, using dynamic schemas
        for each data element inserted instead of predefined schemas
   Benefits:
       Less time spent on upfront schema development and ongoing schema evolution,
        more time can be used for actual application development
       Can store and retrieve programming language objects more easily
       No need to sync application changes deployment with database schema changes,
        no database downtime required for schema modifications

                                              © 2012 triAGENS GmbH | 2012-08-26        10
NoSQL databases – overview

   There is a multitude of different NoSQL databases around
   http://nosql-databases.org/ currently lists 122 NoSQL databases
   Some of the most popular ones:
       MongoDB                       Riak
       CouchDB                       Redis
       RavenDB                       Tokyo Cabinet
       Hadoop                        Voldemort
       Hypertable                    Neo4j
       Cassandra                     OrientDB
       SimpleDB                      InfiniteGraph
   They are all different, but a basic categorisation follows

                                         © 2012 triAGENS GmbH | 2012-08-26   11
NoSQL databases – categorisation

   Key-value stores:
                                                                       key             value
       Map value data to unique string keys (identifiers)
                                                                      obj3         „{ a: 1, b: 1 }“
       Allow access to data by key only
                                                                      obj1         „{ a: 2, b: 1 }“
       Treat data as opaque (data has no schema)
                                                                      obj2         „{ a: 1, b: 2 }“
       Do not save keys in predicatable order
                                                                  numUsers              „25“
       Sometimes disallow key enumeration
        because it is expensive                                     userIds         „1, 2, 3, 4“
       May have one or multiple key spaces to logically similar group keys
       Can implement scaling and partitioning easily due to simplistic data model




                                               © 2012 triAGENS GmbH | 2012-08-26                      12
NoSQL databases – categorisation

   Key-value store extensions (containers, counters, expiration):
       may allow querying on value data, too, by offering secondary indexes.
        secondary index values must be provided by application because store
        still treats data as opaque (data has no schema)
       may offer atomic container operations, counters (e.g. inc / dec) or value
        expiration features that allow broader queries and use cases

          key          value          index on „a“        atomic operations
          obj3     „{ a: 1, b: 1 }“        1
          obj1     „{ a: 2, b: 1 }“        1
          obj2     „{ a: 1, b: 2 }“        2
        numUsers        „25“                                    increment
         userIds     „1, 2, 3, 4“                             add 5 to set


                                                © 2012 triAGENS GmbH | 2012-08-26   13
NoSQL databases – categorisation

   Ordered key-value stores:
       Co-locate values with adjacent keys so keys are stored sorted
       Also allow range queries on keys
       Still treat value data as opaque (data has no schema)
            key           value
          prj1_cnt         „25“
         prj1_user1   „{ a: 2, b: 1 }“
         prj1_user2   „{ a: 1, b: 2 }“
          prj2_cnt         „42“
         prj2_user1   „{ a: 5, b: 1 }“
         prj2_user2   „{ a: 1, b: 3 }“



                                              © 2012 triAGENS GmbH | 2012-08-26   14
NoSQL databases – categorisation

   Wide column stores:
       Save values in an n-level map-of-maps substructure,
        e.g. column families, supercolumns and columns with value & timestamp
       Allow querying specific attributes for keys (at least at the top levels)
           key                           columns
          user1        name: „foo“       city: „bar“
          user2        name: „bar“       city: „baz“          work: „foo“
          user3        name: „baz“

            key       supercolumns       columns
          user1          address         city: „bar“         street: „foo“
                          name           first: „foo“         last: „bar“



                                                   © 2012 triAGENS GmbH | 2012-08-26   15
NoSQL databases – categorisation

   Document stores:
       Normally based on key-value stores (each document still has a unique key)
       Allow to save documents with logical similarity in „databases“ or „collections“
       Treat data records as attribute-structured documents (data is no more opaque)
       Use inherent schemas of documents to categorise attributes and sub-attributes
       Introduce data types (primitive types, compound types)
       Allow arbitrary nesting and document complexity using lists, arrays etc.
       Allow individual documents to have different attributes, even if in same collection
       Often allow querying and indexing document attributes




                                               © 2012 triAGENS GmbH | 2012-08-26          16
NoSQL databases – categorisation

   Example documents in a document store:
        key                    attributes and sub-attributes
       user1     address       city: „bar“        street: „foo“
                name: „foo“
                 birthday      month: 12              day: 1                year: 1983
                   likes        „skiing“            „fishing“
               avgScore: 3.5

       user2     address       city: „baz“      street: „foobar“
                name: „bar“

      user42   banana: true




                                        © 2012 triAGENS GmbH | 2012-08-26                17
NoSQL databases – categorisation

   Graph databases:
       Often based on key-value or document stores
       Additionally save relationships (edges) between documents (vertices)
       Allow querying relationships between documents
       Relationships can have attributes as well

          user1       name: „foo“                                      user2       name: „bar“
                        age: 32                                                     age: 35

                                       rated           value: 3

                                       rated           value: 5




                                               © 2012 triAGENS GmbH | 2012-08-26                 18
Caveat emptor

   Things you should be aware of when using a NoSQL database:
       multi-query, multi-document or multi-key „transactions“ may not be atomic
        and not be isolated
       the application may need to handle inconsistent / stale reads
       the application may need to resolve version conflicts or handle multiple schema
        versions of documents
       referential integrity needs to be enforced by the application
       if the datastore treats data as opaque, it will not at all validate it
       by design, the database may not be able to answer some classes of queries,
        e.g. joins need to be performed by the application, not by the database
   Put simple: database will do less, application must do more!



                                                  © 2012 triAGENS GmbH | 2012-08-26   19
Which datastore to use?

   Relational databases are still a good fit for many problems,
    but not for all
   The same is true for NoSQL databases
   Each category of NoSQL databases and each individual product
    has its own sweet spots (and downsides)
   Check what you want to achieve first, then pick the right tool(s)
    for the job
   It may make sense to use different databases in parallel




                                      © 2012 triAGENS GmbH | 2012-08-26   20
Why care about data modelling in NoSQL?

   Even in NoSQL databases, your data will have some structure
   This effective data model determines
       the consistency level you're able to achieve
       the retrieval and update performance
       the storage space required for saving the data
       the types of queries you will be able to run on the data
   So even if you don't define explicit schemas, you still need to care
    about structuring your data properly




                                               © 2012 triAGENS GmbH | 2012-08-26   21
Some NoSQL data modelling considerations

   Data modelling for schema-less or dynamic schema datastores
    is different than modelling for relational databases
   Different NoSQL databases require different modelling
   The techniques to apply are not revolutionary, but in the relational
    world they are unnecessary or even anti-patterns
   A few things to look at:
       Proper key and attribute naming
       Building „indexes“ by hand for aggregation
       Data denormalisation and duplication




                                               © 2012 triAGENS GmbH | 2012-08-26   22
Picking „good“ keys – key length

   In plain key-value stores, keys are the only thing that you can query
   Keys are also present in other categories of NoSQL databases.
    Data elements are still identified using unique keys
   Keys should be picked so that they are sensible and legible
   Keys are normally stored just once, but...
   ...in some NoSQL databases, key values may be duplicated a lot of
    times in secondary indexes, append-only log structures etc.
   In these situations, keys should also be as short as can be tolerated
    to not waste disk space / RAM




                                      © 2012 triAGENS GmbH | 2012-08-26   23
Key length – CouchDB example

   Case: insert 1000 documents with unique keys into vanilla CouchDB
   Key prefix is „very-long-id“
   For each document, we also store an attribute named „very-long-value“
   Setup:
    for i in `seq 1000 1999`
     do
      curl -X POST                              
          --header "Content-Type: application/json" 
          --data "{ "_id": "very-long-id-$i", 
                  "very-long-value": "$i" }" 
          http://127.0.0.1:5984/keytest
     done
                                      © 2012 triAGENS GmbH | 2012-08-26   24
Key length – CouchDB example

   Now check how often the key prefix occurs in the datafile:
    strings /db/keytest.couch | grep -c "very-long-id"
    strings /db/keytest.couch | grep -c "very-long-value"
   Results:
       the non-key attribute name „very-long-value“ is saved 1,000 times
       the key prefix is stored 42,492 times instead of the expected 1,000 times !!
   This is due to MVCC that organises data in copy-on-write B+ trees




                                               © 2012 triAGENS GmbH | 2012-08-26       25
Picking „good“ keys – hierarchical keys

   If in a key-value store only one global keyspace is available, use
    „hierarchical keys“ to distinguish keys from multiple applications or
    multiple users. This helps preventing unintended key collisions.
    Avoid these keys:            Instead, use such keys:
       user1                       app1_user1
       profile42                   client6_profile42
       item23                      app1_client6_item23
   If your datastore allows efficient key enumeration with prefixes,
    this also allows hierarchical operations by prefix, e.g.
    DELETE client6_*, GET app1_client6_item*
   Don't need this in document stores due to collections

                                        © 2012 triAGENS GmbH | 2012-08-26   26
Attribute names in value data

   The value data you save is often a combination of attribute names and
    values, e.g. { „age“: 35, „gender“: „m“, „name“: „foo“ }
   In a fully schema-free datastore (e.g. plain key-value store), the store
    treats data as opaque and cannot tell attribute names and values in the
    value data apart
   In document stores, data is no more opaque. However, there are no
    fixed schemas. Each document, even if in the same collection, could
    have different attributes
   This means non-key attribute names are saved redundantly for EACH
    record/document
   Result: using attribute name „f“ will normally result in much lower disk
    footprint than using attribute name „firstName“.
   Check these MongoDB-specific links:
                                           © 2012 triAGENS GmbH | 2012-08-26   27
       http://christophermaier.name/blog/2011/05/22/MongoDB-key-names
Aggregation – home-made indexes

   NoSQL stores normally don't support aggregation queries, but you
    can workaround that by creating „index“ keys on your own.
    Initial data:
        key                  value
       user1       „{ state: „CA“, age: 35 }“

       user2       „{ state: „CA“, age: 26 }“

       user3       „{ state: „FL“, age: 21 }“

       user42      „{ state: „FL“, age: 39 }“

   From that you can derive „index“ keys that provide users by state.
    You can now run count queries easily:
      users_CA          „user1, user2“
      users_FL         „user3, user42“


                                                © 2012 triAGENS GmbH | 2012-08-26   28
Aggregation – home-made indexes

   „Index“ keys that provides users by age:
         key               value
      users_39            „user42“
      users_35             „user1“
      users_21             „user3“
      users_26             „user2“

   Can also directly aggregate categories in „index“ keys if there are
    (too) many distinct category values:
     users_20_29       „user3, user2“
     users_30_39       „user1, user42“

    (I know, I should have used better key names)

                                         © 2012 triAGENS GmbH | 2012-08-26   29
Aggregation – home-made indexes

   Home-made „indexes“ provide about the same functionality as
    materialised views do in relational databases
   And as in materialised views, this means data duplication!
   The database won't maintain the „indexes“ for you, so you have to
    maintain them yourself (from out of the application)
   Keeping the „indexes“ up-to-date may require data updates
    in several places
   Can easily get ouf of sync or provide an inconsistent view of the data
   Accuracy may still be good enough, but this depends on the use case




                                     © 2012 triAGENS GmbH | 2012-08-26   30
Aggregation – hierarchical keys

   If your datastore allows efficient key range enumeration using leftmost
    prefix queries, using hierarchical „index“ keys can also be used for
    multi-dimensional aggregation
                 key                              query
        Americas_CA_Toronto
        Americas_US_New York
           Asia_CN_Beijing               Query: GET Asia_*
           Asia_TW_Taipeh
           key
          Europe_DE_Berlin             Query: GET Europe_*
         Europe_DE_Cologne

          Europe_FR_Paris            Query: GET Europe_FR_*

         Europe_FR_Toulose


                                     © 2012 triAGENS GmbH | 2012-08-26   31
Aggregation – key buckets

   If the datastore does not allow querying attribute values, buckets
    with predictable key names can be used to support aggegation
   Example case: users online on a website during the last x minutes
   Key name convention: „usersOnline_${slot}“, with ${slot} being
    slot = (time() / 60).floor() * 60
    (the current Unix timestamp rounded to a 60 second slot)
   This will produce predictable key names such as
       usersOnline_1345732920
       usersOnline_1345732980
       UsersOnline_1345733040
   You can now easily save and query which users were online in
    which time slot and create reports
                                      © 2012 triAGENS GmbH | 2012-08-26   32
Denormalisation – nesting sub elements

   It may be beneficial to all nest sub elements of a logical unit in the
    unit itself instead of saving the sub elements separately
   Document stores are built for exactly this case (nesting attributes),
    but this pattern can be used in key-value stores as well
   Instead of saving all sub elements individually
         key                          value
     user1_name                         ...
    user1_address                       ...
     user1_likes                        ...

    Can store all sub elements of a user in one key / document only:
        user1         { name: ..., address: ..., likes: .... }


                                              © 2012 triAGENS GmbH | 2012-08-26   33
Denormalisation – nesting sub elements

   Benefit: nesting sub elements reduces the number of elements that
    keep data for a logical unit (here: „user“)
   This reduces the number of read / write operations for each unit
   Accessing data of a single key is normally an atomic operation, so
    nesting prevents sub elements from getting out sync, avoiding
    inconsistent reads and writes of logical units
   In NoSQL databases, nesting is also a natural way to avoid joins,
    which are not offered by most NoSQL products




                                     © 2012 triAGENS GmbH | 2012-08-26   34
Denormalisation – nesting sub elements

   Downside: nesting will increase the data volume stored for each key,
    increasing the overhead for each read and write in both the datastore
    and the application
   If you overdo nesting, you might end up having the same data stored
    in multiple places, and no clear responsibility for it
   This will waste space and cause consistency issues
   For example, a change to user2's „likes“ attribute requires 3 writes:
        key                                                    value
        user1      { likes: [ ... ], friends: [ { user2: { likes: [ ... ] } }, user15: { likes: [ ... ] } }, ... ] }

        user2      { likes: [ ... ], friends: [ { user67: { likes: [ ... ] } }, user1: { likes: [ ... ] } }, ... ] }

       user15      { likes: [ ... ], friends: [ { user2: { likes: [ ... ] } }, user99: { likes: [ ... ] } }, ... ] }



                                                   © 2012 triAGENS GmbH | 2012-08-26                         35
Denormalisation – nesting sub elements

   Instead of nesting such elements fully, prefer referencing them by
    key / id:
        key                             value
        user1        { likes: [ ... ], friends: [ user2, user15 ] }

        user2        { likes: [ ... ], friends: [ user67, user1 ] }

       user15        { likes: [ ... ], friends: [ user2, user99 ] }
   This will allow you to update data in only one place,
    saving storage space and improving consistency
   The price you pay: to retrieve all details for the referenced items, you
    now need additional read operations




                                                © 2012 triAGENS GmbH | 2012-08-26   36
Summary

   Data modelling for NoSQL databases is different than for relational
    databases:
       Instead of normalising your schema, you'd rather denormalise your data
       Data duplication is common to support analysis and aggregation queries, and for
        fast retrieval
   Different NoSQL databases have different features and to be used
    effectively,
    may require different data models
   To make best use of a specific store, you should have some
    knowledge about how it organises data internally and what guarantees
    it provides:
       Atomic operations should be exploited where possible to improve consistency
       Cross-record/document consistency is mostly not guaranteed.
        If your store at all offers it, it will be slowed down by it
                                             © 2012 triAGENS GmbH | 2012-08-26        37
   In general logic is moved from the database to the application
Misc

   NoSQL user group Cologne:
    every 1st Wednesday / month
    http://www.nosql-cologne.org/
   NoSQL matters conference Barcelona:
    October 6th, 2012
    http://www.nosql-matters.org/
   ArangoDB 1.0 release:
    September 2012
    http://www.arangodb.org/




                                    © 2012 triAGENS GmbH | 2012-08-26   38

More Related Content

What's hot (20)

PDF
Deep dive into the native multi model database ArangoDB
ArangoDB Database
 
DOC
Assignment_4
Kirti J
 
PDF
Processing large-scale graphs with Google Pregel
Max Neunhöffer
 
PPSX
A Seminar on NoSQL Databases.
Navdeep Charan
 
PPTX
NoSQL and MapReduce
J Singh
 
PDF
Multi model-databases
ArangoDB Database
 
PDF
Introduction to NoSQL
Dimitar Danailov
 
PDF
NOSQL- Presentation on NoSQL
Ramakant Soni
 
PPTX
Nosql databases
ateeq ateeq
 
PPT
NoSQL databases
Meshal Albeedhani
 
PPTX
Nosql seminar
Shreyashkumar Nangnurwar
 
PPTX
Introduction to NoSQL
balwinders
 
PDF
Big Challenges in Data Modeling: NoSQL and Data Modeling
DATAVERSITY
 
PPTX
Why no sql ? Why Couchbase ?
Ahmed Rashwan
 
PPT
Mule database-connectors
Phaniu
 
PPTX
NOSQL vs SQL
Mohammed Fazuluddin
 
PPTX
No sq lv2
Nusrat Sharmin
 
PPTX
Appache Cassandra
nehabsairam
 
PDF
Vskills Apache Cassandra sample material
Vskills
 
PDF
SQL or NoSQL, is this the question? - George Grammatikos
George Grammatikos
 
Deep dive into the native multi model database ArangoDB
ArangoDB Database
 
Assignment_4
Kirti J
 
Processing large-scale graphs with Google Pregel
Max Neunhöffer
 
A Seminar on NoSQL Databases.
Navdeep Charan
 
NoSQL and MapReduce
J Singh
 
Multi model-databases
ArangoDB Database
 
Introduction to NoSQL
Dimitar Danailov
 
NOSQL- Presentation on NoSQL
Ramakant Soni
 
Nosql databases
ateeq ateeq
 
NoSQL databases
Meshal Albeedhani
 
Introduction to NoSQL
balwinders
 
Big Challenges in Data Modeling: NoSQL and Data Modeling
DATAVERSITY
 
Why no sql ? Why Couchbase ?
Ahmed Rashwan
 
Mule database-connectors
Phaniu
 
NOSQL vs SQL
Mohammed Fazuluddin
 
No sq lv2
Nusrat Sharmin
 
Appache Cassandra
nehabsairam
 
Vskills Apache Cassandra sample material
Vskills
 
SQL or NoSQL, is this the question? - George Grammatikos
George Grammatikos
 

Viewers also liked (6)

PDF
UNIFAL - MySQL 5.6 - Replicação
Wagner Bianchi
 
ODP
No sql std
Arthur Azevedo
 
PDF
Technical Introduction to PostgreSQL and PPAS
Ashnikbiz
 
PPTX
MySQL Multi Master Replication
Moshe Kaplan
 
PDF
Processing large-scale graphs with Google(TM) Pregel
ArangoDB Database
 
KEY
NoSQL databases and managing big data
Steven Francia
 
UNIFAL - MySQL 5.6 - Replicação
Wagner Bianchi
 
No sql std
Arthur Azevedo
 
Technical Introduction to PostgreSQL and PPAS
Ashnikbiz
 
MySQL Multi Master Replication
Moshe Kaplan
 
Processing large-scale graphs with Google(TM) Pregel
ArangoDB Database
 
NoSQL databases and managing big data
Steven Francia
 
Ad

Similar to Jan Steemann: Modelling data in a schema free world (Talk held at Froscon, 2012-08-26) (20)

PDF
NoSQL BIg Data Analytics Mongo DB and Cassandra .pdf
Sharmila Chidaravalli
 
PPTX
Non relational databases-no sql
Ram kumar
 
PPTX
NOSQL IN BIGDATA FOR PG STUDENTS FOR COL
DharaniMani4
 
PPTX
Introduction to No SQL - Learn nosql databases
Sharon Manmothe
 
PPTX
Big Data Analytics Module-3 as per vtu syllabus.pptx
shilpabl1803
 
PPTX
unit2-ppt1.pptx
revathigollu23
 
PPTX
No sql database
vishal gupta
 
PDF
orical
Gowri Shankar
 
PDF
SURVEY ON IMPLEMANTATION OF COLUMN ORIENTED NOSQL DATA STORES ( BIGTABLE & CA...
IJCERT JOURNAL
 
PPTX
Complete first chapter rdbm 17332
Tushar Wagh
 
PPTX
Unit 3 MongDB
Praveen M Jigajinni
 
PPTX
NoSQL databases - An introduction
Pooyan Mehrparvar
 
PPTX
Nosql
Roxana Tadayon
 
PDF
Artigo no sql x relational
Adenilson Lima Diniz
 
PPTX
Nosql
ROXTAD71
 
PDF
Presentation On NoSQL Databases
Abiral Gautam
 
PDF
NoSQL Databases: An Introduction and Comparison between Dynamo, MongoDB and C...
Vivek Adithya Mohankumar
 
PPTX
DB Luminous... Know Your Data
Russ Pierce
 
PDF
No SQL databases basics module 1 vtu notes
SHRITEJASHASTRYS1
 
NoSQL BIg Data Analytics Mongo DB and Cassandra .pdf
Sharmila Chidaravalli
 
Non relational databases-no sql
Ram kumar
 
NOSQL IN BIGDATA FOR PG STUDENTS FOR COL
DharaniMani4
 
Introduction to No SQL - Learn nosql databases
Sharon Manmothe
 
Big Data Analytics Module-3 as per vtu syllabus.pptx
shilpabl1803
 
unit2-ppt1.pptx
revathigollu23
 
No sql database
vishal gupta
 
SURVEY ON IMPLEMANTATION OF COLUMN ORIENTED NOSQL DATA STORES ( BIGTABLE & CA...
IJCERT JOURNAL
 
Complete first chapter rdbm 17332
Tushar Wagh
 
Unit 3 MongDB
Praveen M Jigajinni
 
NoSQL databases - An introduction
Pooyan Mehrparvar
 
Artigo no sql x relational
Adenilson Lima Diniz
 
Nosql
ROXTAD71
 
Presentation On NoSQL Databases
Abiral Gautam
 
NoSQL Databases: An Introduction and Comparison between Dynamo, MongoDB and C...
Vivek Adithya Mohankumar
 
DB Luminous... Know Your Data
Russ Pierce
 
No SQL databases basics module 1 vtu notes
SHRITEJASHASTRYS1
 
Ad

More from ArangoDB Database (20)

PPTX
ATO 2022 - Machine Learning + Graph Databases for Better Recommendations (3)....
ArangoDB Database
 
PPTX
Machine Learning + Graph Databases for Better Recommendations V2 08/20/2022
ArangoDB Database
 
PPTX
Machine Learning + Graph Databases for Better Recommendations V1 08/06/2022
ArangoDB Database
 
PPTX
ArangoDB 3.9 - Further Powering Graphs at Scale
ArangoDB Database
 
PDF
GraphSage vs Pinsage #InsideArangoDB
ArangoDB Database
 
PDF
Webinar: ArangoDB 3.8 Preview - Analytics at Scale
ArangoDB Database
 
PDF
Graph Analytics with ArangoDB
ArangoDB Database
 
PDF
Getting Started with ArangoDB Oasis
ArangoDB Database
 
PDF
Custom Pregel Algorithms in ArangoDB
ArangoDB Database
 
PPTX
Hacktoberfest 2020 - Intro to Knowledge Graphs
ArangoDB Database
 
PDF
A Graph Database That Scales - ArangoDB 3.7 Release Webinar
ArangoDB Database
 
PDF
gVisor, Kata Containers, Firecracker, Docker: Who is Who in the Container Space?
ArangoDB Database
 
PDF
ArangoML Pipeline Cloud - Managed Machine Learning Metadata
ArangoDB Database
 
PDF
ArangoDB 3.7 Roadmap: Performance at Scale
ArangoDB Database
 
PDF
Webinar: What to expect from ArangoDB Oasis
ArangoDB Database
 
PDF
ArangoDB 3.5 Feature Overview Webinar - Sept 12, 2019
ArangoDB Database
 
PDF
3.5 webinar
ArangoDB Database
 
PDF
Webinar: How native multi model works in ArangoDB
ArangoDB Database
 
PDF
An introduction to multi-model databases
ArangoDB Database
 
PDF
Running complex data queries in a distributed system
ArangoDB Database
 
ATO 2022 - Machine Learning + Graph Databases for Better Recommendations (3)....
ArangoDB Database
 
Machine Learning + Graph Databases for Better Recommendations V2 08/20/2022
ArangoDB Database
 
Machine Learning + Graph Databases for Better Recommendations V1 08/06/2022
ArangoDB Database
 
ArangoDB 3.9 - Further Powering Graphs at Scale
ArangoDB Database
 
GraphSage vs Pinsage #InsideArangoDB
ArangoDB Database
 
Webinar: ArangoDB 3.8 Preview - Analytics at Scale
ArangoDB Database
 
Graph Analytics with ArangoDB
ArangoDB Database
 
Getting Started with ArangoDB Oasis
ArangoDB Database
 
Custom Pregel Algorithms in ArangoDB
ArangoDB Database
 
Hacktoberfest 2020 - Intro to Knowledge Graphs
ArangoDB Database
 
A Graph Database That Scales - ArangoDB 3.7 Release Webinar
ArangoDB Database
 
gVisor, Kata Containers, Firecracker, Docker: Who is Who in the Container Space?
ArangoDB Database
 
ArangoML Pipeline Cloud - Managed Machine Learning Metadata
ArangoDB Database
 
ArangoDB 3.7 Roadmap: Performance at Scale
ArangoDB Database
 
Webinar: What to expect from ArangoDB Oasis
ArangoDB Database
 
ArangoDB 3.5 Feature Overview Webinar - Sept 12, 2019
ArangoDB Database
 
3.5 webinar
ArangoDB Database
 
Webinar: How native multi model works in ArangoDB
ArangoDB Database
 
An introduction to multi-model databases
ArangoDB Database
 
Running complex data queries in a distributed system
ArangoDB Database
 

Recently uploaded (20)

PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PDF
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
PPTX
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PPTX
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
PDF
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PDF
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PPTX
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
PDF
Per Axbom: The spectacular lies of maps
Nexer Digital
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
Economic Impact of Data Centres to the Malaysian Economy
flintglobalapac
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
MASTERDECK GRAPHSUMMIT SYDNEY (Public).pdf
Neo4j
 
What-is-the-World-Wide-Web -- Introduction
tonifi9488
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
AVL ( audio, visuals or led ), technology.
Rajeshwri Panchal
 
Structs to JSON: How Go Powers REST APIs
Emily Achieng
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
Trying to figure out MCP by actually building an app from scratch with open s...
Julien SIMON
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Agile Chennai 18-19 July 2025 Ideathon | AI Powered Microfinance Literacy Gui...
AgileNetwork
 
Per Axbom: The spectacular lies of maps
Nexer Digital
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
The Future of AI & Machine Learning.pptx
pritsen4700
 

Jan Steemann: Modelling data in a schema free world (Talk held at Froscon, 2012-08-26)

  • 1. Modelling data in a schema-free world? FrOSCon 2012-08-26 Jan Steemann (triAGENS) © 2012 triAGENS GmbH | 2012-08-26 1
  • 2. About me  Professional database developer @ triAGENS GmbH, Cologne, Germany  Interested in all kind of databases  Currently working on an open source mixed-model nosql database © 2012 triAGENS GmbH | 2012-08-26 2
  • 3. Schemas in relational databases  Relational databases are around for a long time already  In most relational databases we can deal with these schema elements:  tables  relationships  columns / fields  views  data types  triggers  indexes  procedures / functions  Schema elements are auxiliary means to organise and validate the actual data! © 2012 triAGENS GmbH | 2012-08-26 3
  • 4. Normalisation in relational databases  Relational databases like your data to be in a highly normalized and de-duplicated form  Put simple, normalisation means  dividing big entities (tables, fields) into smaller ones  defining relationships between them  Example data, not normalised: user_id user_name group_name status 1 foo Power user Active 2 bar Rookie Inactive 3 baz Rookie Active © 2012 triAGENS GmbH | 2012-08-26 4
  • 5. Normalisation in relational databases  Example data, normalised (relationships not depicted): Table „users“ Table „groups“ Table „status“ id name group status id name id name 1 foo 1 1 1 Power user 1 Active 2 bar 2 2 2 Rookie 2 Inactive 3 baz 2 1  Normalisation helps to  reduce redundancies and storage requirements  increase consistency and integrity  Normalised data is re-arranged on retrieval using joins, projections etc.  Due to normalisation, data may be fetched from several tables © 2012 triAGENS GmbH | 2012-08-26 5
  • 6. Issues with relational databases  Issues of relational databases:  scaling is hard: This is due to the ACID (atomicity, consistency, isolation, durability) guarantees they usually provide.  rigid database schemas can obstruct rapid application development: Initial and ongoing database schema development and evolution takes time, but you must do it in order to have your database accept any data at all, designing good schemas is hard  fixed database schemas block deployment of changes: When deploying application changes, the database schema needs to stay in sync with the application. Deploying application changes may even cause downtime due to long-running database migrations (e.g. ALTER TABLE statements) © 2012 triAGENS GmbH | 2012-08-26 6
  • 7. Issues with relational databases  Some more issues:  static schemas do not play well with dynamically typed languages: many languages support dynamic keys for their container types, dynamic variables, run-time typing etc. To use this with a relational database, one needs workarounds (e.g. entity-attribute value modelling, auxilliary tables) that increase complexity and make it slow  saving multi-valued data is „forbidden“: Mutli-value containers (e.g. hashes, dictionaries, lists, arrays) are used frequently in programming but must not be saved in the database as such Complex workarounds are necessary to handle that  saving objects can be expensive: saving one object in a relational way may create a lot of noise in relational databases (though this may be obscured by frameworks) © 2012 triAGENS GmbH | 2012-08-26 7
  • 8. NoSQL databases – why?  In the past 5 years, many new database products have emerged  The common term for these new products is „NoSQL“ databases, mainly interpreted as „not only SQL“  „non-relational“ is also a good name for them because almost no NoSQL database follows the relational model  Instead, most NoSQL databases overcome specific issues that relational databases have, especially  scaling  schema rigidity © 2012 triAGENS GmbH | 2012-08-26 8
  • 9. NoSQL databases – scaling  Scaling problems of relational databases are addressed in most NoSQL databases by reducing ACID guarantees and removing relational features:  Atomicity is guaranteed only for some operations, not all  Isolation is only guaranteed for some operations  Strict consistency is relaxed to eventual consistency  Strict durability is normally turned off (but is configurable by admin)  Don't offer referential integrity or do not enforce it  Complex data operations as joins etc. are highly restricted or completely disallowed  These measures help to reduce locking and synchronisation overhead and make scaling possible at all © 2012 triAGENS GmbH | 2012-08-26 9
  • 10. NoSQL databases – schema rigidity  Most NoSQL databases lift the requirement of having to define explicit database schemas:  Some NoSQL databases don't use schemas at all, they are fully schema-free  Some require the user to only predefine highest level schema elements (e.g. databases, collections, column families) upfront (and some allow it as you go)  Some use schemas implicitly by analysing incoming data, using dynamic schemas for each data element inserted instead of predefined schemas  Benefits:  Less time spent on upfront schema development and ongoing schema evolution, more time can be used for actual application development  Can store and retrieve programming language objects more easily  No need to sync application changes deployment with database schema changes, no database downtime required for schema modifications © 2012 triAGENS GmbH | 2012-08-26 10
  • 11. NoSQL databases – overview  There is a multitude of different NoSQL databases around  http://nosql-databases.org/ currently lists 122 NoSQL databases  Some of the most popular ones:  MongoDB  Riak  CouchDB  Redis  RavenDB  Tokyo Cabinet  Hadoop  Voldemort  Hypertable  Neo4j  Cassandra  OrientDB  SimpleDB  InfiniteGraph  They are all different, but a basic categorisation follows © 2012 triAGENS GmbH | 2012-08-26 11
  • 12. NoSQL databases – categorisation  Key-value stores: key value  Map value data to unique string keys (identifiers) obj3 „{ a: 1, b: 1 }“  Allow access to data by key only obj1 „{ a: 2, b: 1 }“  Treat data as opaque (data has no schema) obj2 „{ a: 1, b: 2 }“  Do not save keys in predicatable order numUsers „25“  Sometimes disallow key enumeration because it is expensive userIds „1, 2, 3, 4“  May have one or multiple key spaces to logically similar group keys  Can implement scaling and partitioning easily due to simplistic data model © 2012 triAGENS GmbH | 2012-08-26 12
  • 13. NoSQL databases – categorisation  Key-value store extensions (containers, counters, expiration):  may allow querying on value data, too, by offering secondary indexes. secondary index values must be provided by application because store still treats data as opaque (data has no schema)  may offer atomic container operations, counters (e.g. inc / dec) or value expiration features that allow broader queries and use cases key value index on „a“ atomic operations obj3 „{ a: 1, b: 1 }“ 1 obj1 „{ a: 2, b: 1 }“ 1 obj2 „{ a: 1, b: 2 }“ 2 numUsers „25“ increment userIds „1, 2, 3, 4“ add 5 to set © 2012 triAGENS GmbH | 2012-08-26 13
  • 14. NoSQL databases – categorisation  Ordered key-value stores:  Co-locate values with adjacent keys so keys are stored sorted  Also allow range queries on keys  Still treat value data as opaque (data has no schema) key value prj1_cnt „25“ prj1_user1 „{ a: 2, b: 1 }“ prj1_user2 „{ a: 1, b: 2 }“ prj2_cnt „42“ prj2_user1 „{ a: 5, b: 1 }“ prj2_user2 „{ a: 1, b: 3 }“ © 2012 triAGENS GmbH | 2012-08-26 14
  • 15. NoSQL databases – categorisation  Wide column stores:  Save values in an n-level map-of-maps substructure, e.g. column families, supercolumns and columns with value & timestamp  Allow querying specific attributes for keys (at least at the top levels) key columns user1 name: „foo“ city: „bar“ user2 name: „bar“ city: „baz“ work: „foo“ user3 name: „baz“ key supercolumns columns user1 address city: „bar“ street: „foo“ name first: „foo“ last: „bar“ © 2012 triAGENS GmbH | 2012-08-26 15
  • 16. NoSQL databases – categorisation  Document stores:  Normally based on key-value stores (each document still has a unique key)  Allow to save documents with logical similarity in „databases“ or „collections“  Treat data records as attribute-structured documents (data is no more opaque)  Use inherent schemas of documents to categorise attributes and sub-attributes  Introduce data types (primitive types, compound types)  Allow arbitrary nesting and document complexity using lists, arrays etc.  Allow individual documents to have different attributes, even if in same collection  Often allow querying and indexing document attributes © 2012 triAGENS GmbH | 2012-08-26 16
  • 17. NoSQL databases – categorisation  Example documents in a document store: key attributes and sub-attributes user1 address city: „bar“ street: „foo“ name: „foo“ birthday month: 12 day: 1 year: 1983 likes „skiing“ „fishing“ avgScore: 3.5 user2 address city: „baz“ street: „foobar“ name: „bar“ user42 banana: true © 2012 triAGENS GmbH | 2012-08-26 17
  • 18. NoSQL databases – categorisation  Graph databases:  Often based on key-value or document stores  Additionally save relationships (edges) between documents (vertices)  Allow querying relationships between documents  Relationships can have attributes as well user1 name: „foo“ user2 name: „bar“ age: 32 age: 35 rated value: 3 rated value: 5 © 2012 triAGENS GmbH | 2012-08-26 18
  • 19. Caveat emptor  Things you should be aware of when using a NoSQL database:  multi-query, multi-document or multi-key „transactions“ may not be atomic and not be isolated  the application may need to handle inconsistent / stale reads  the application may need to resolve version conflicts or handle multiple schema versions of documents  referential integrity needs to be enforced by the application  if the datastore treats data as opaque, it will not at all validate it  by design, the database may not be able to answer some classes of queries, e.g. joins need to be performed by the application, not by the database  Put simple: database will do less, application must do more! © 2012 triAGENS GmbH | 2012-08-26 19
  • 20. Which datastore to use?  Relational databases are still a good fit for many problems, but not for all  The same is true for NoSQL databases  Each category of NoSQL databases and each individual product has its own sweet spots (and downsides)  Check what you want to achieve first, then pick the right tool(s) for the job  It may make sense to use different databases in parallel © 2012 triAGENS GmbH | 2012-08-26 20
  • 21. Why care about data modelling in NoSQL?  Even in NoSQL databases, your data will have some structure  This effective data model determines  the consistency level you're able to achieve  the retrieval and update performance  the storage space required for saving the data  the types of queries you will be able to run on the data  So even if you don't define explicit schemas, you still need to care about structuring your data properly © 2012 triAGENS GmbH | 2012-08-26 21
  • 22. Some NoSQL data modelling considerations  Data modelling for schema-less or dynamic schema datastores is different than modelling for relational databases  Different NoSQL databases require different modelling  The techniques to apply are not revolutionary, but in the relational world they are unnecessary or even anti-patterns  A few things to look at:  Proper key and attribute naming  Building „indexes“ by hand for aggregation  Data denormalisation and duplication © 2012 triAGENS GmbH | 2012-08-26 22
  • 23. Picking „good“ keys – key length  In plain key-value stores, keys are the only thing that you can query  Keys are also present in other categories of NoSQL databases. Data elements are still identified using unique keys  Keys should be picked so that they are sensible and legible  Keys are normally stored just once, but...  ...in some NoSQL databases, key values may be duplicated a lot of times in secondary indexes, append-only log structures etc.  In these situations, keys should also be as short as can be tolerated to not waste disk space / RAM © 2012 triAGENS GmbH | 2012-08-26 23
  • 24. Key length – CouchDB example  Case: insert 1000 documents with unique keys into vanilla CouchDB  Key prefix is „very-long-id“  For each document, we also store an attribute named „very-long-value“  Setup: for i in `seq 1000 1999` do curl -X POST --header "Content-Type: application/json" --data "{ "_id": "very-long-id-$i", "very-long-value": "$i" }" http://127.0.0.1:5984/keytest done © 2012 triAGENS GmbH | 2012-08-26 24
  • 25. Key length – CouchDB example  Now check how often the key prefix occurs in the datafile: strings /db/keytest.couch | grep -c "very-long-id" strings /db/keytest.couch | grep -c "very-long-value"  Results:  the non-key attribute name „very-long-value“ is saved 1,000 times  the key prefix is stored 42,492 times instead of the expected 1,000 times !!  This is due to MVCC that organises data in copy-on-write B+ trees © 2012 triAGENS GmbH | 2012-08-26 25
  • 26. Picking „good“ keys – hierarchical keys  If in a key-value store only one global keyspace is available, use „hierarchical keys“ to distinguish keys from multiple applications or multiple users. This helps preventing unintended key collisions. Avoid these keys: Instead, use such keys:  user1  app1_user1  profile42  client6_profile42  item23  app1_client6_item23  If your datastore allows efficient key enumeration with prefixes, this also allows hierarchical operations by prefix, e.g. DELETE client6_*, GET app1_client6_item*  Don't need this in document stores due to collections © 2012 triAGENS GmbH | 2012-08-26 26
  • 27. Attribute names in value data  The value data you save is often a combination of attribute names and values, e.g. { „age“: 35, „gender“: „m“, „name“: „foo“ }  In a fully schema-free datastore (e.g. plain key-value store), the store treats data as opaque and cannot tell attribute names and values in the value data apart  In document stores, data is no more opaque. However, there are no fixed schemas. Each document, even if in the same collection, could have different attributes  This means non-key attribute names are saved redundantly for EACH record/document  Result: using attribute name „f“ will normally result in much lower disk footprint than using attribute name „firstName“.  Check these MongoDB-specific links: © 2012 triAGENS GmbH | 2012-08-26 27  http://christophermaier.name/blog/2011/05/22/MongoDB-key-names
  • 28. Aggregation – home-made indexes  NoSQL stores normally don't support aggregation queries, but you can workaround that by creating „index“ keys on your own. Initial data: key value user1 „{ state: „CA“, age: 35 }“ user2 „{ state: „CA“, age: 26 }“ user3 „{ state: „FL“, age: 21 }“ user42 „{ state: „FL“, age: 39 }“  From that you can derive „index“ keys that provide users by state. You can now run count queries easily: users_CA „user1, user2“ users_FL „user3, user42“ © 2012 triAGENS GmbH | 2012-08-26 28
  • 29. Aggregation – home-made indexes  „Index“ keys that provides users by age: key value users_39 „user42“ users_35 „user1“ users_21 „user3“ users_26 „user2“  Can also directly aggregate categories in „index“ keys if there are (too) many distinct category values: users_20_29 „user3, user2“ users_30_39 „user1, user42“ (I know, I should have used better key names) © 2012 triAGENS GmbH | 2012-08-26 29
  • 30. Aggregation – home-made indexes  Home-made „indexes“ provide about the same functionality as materialised views do in relational databases  And as in materialised views, this means data duplication!  The database won't maintain the „indexes“ for you, so you have to maintain them yourself (from out of the application)  Keeping the „indexes“ up-to-date may require data updates in several places  Can easily get ouf of sync or provide an inconsistent view of the data  Accuracy may still be good enough, but this depends on the use case © 2012 triAGENS GmbH | 2012-08-26 30
  • 31. Aggregation – hierarchical keys  If your datastore allows efficient key range enumeration using leftmost prefix queries, using hierarchical „index“ keys can also be used for multi-dimensional aggregation key query Americas_CA_Toronto Americas_US_New York Asia_CN_Beijing Query: GET Asia_* Asia_TW_Taipeh key Europe_DE_Berlin Query: GET Europe_* Europe_DE_Cologne Europe_FR_Paris Query: GET Europe_FR_* Europe_FR_Toulose © 2012 triAGENS GmbH | 2012-08-26 31
  • 32. Aggregation – key buckets  If the datastore does not allow querying attribute values, buckets with predictable key names can be used to support aggegation  Example case: users online on a website during the last x minutes  Key name convention: „usersOnline_${slot}“, with ${slot} being slot = (time() / 60).floor() * 60 (the current Unix timestamp rounded to a 60 second slot)  This will produce predictable key names such as  usersOnline_1345732920  usersOnline_1345732980  UsersOnline_1345733040  You can now easily save and query which users were online in which time slot and create reports © 2012 triAGENS GmbH | 2012-08-26 32
  • 33. Denormalisation – nesting sub elements  It may be beneficial to all nest sub elements of a logical unit in the unit itself instead of saving the sub elements separately  Document stores are built for exactly this case (nesting attributes), but this pattern can be used in key-value stores as well  Instead of saving all sub elements individually key value user1_name ... user1_address ... user1_likes ... Can store all sub elements of a user in one key / document only: user1 { name: ..., address: ..., likes: .... } © 2012 triAGENS GmbH | 2012-08-26 33
  • 34. Denormalisation – nesting sub elements  Benefit: nesting sub elements reduces the number of elements that keep data for a logical unit (here: „user“)  This reduces the number of read / write operations for each unit  Accessing data of a single key is normally an atomic operation, so nesting prevents sub elements from getting out sync, avoiding inconsistent reads and writes of logical units  In NoSQL databases, nesting is also a natural way to avoid joins, which are not offered by most NoSQL products © 2012 triAGENS GmbH | 2012-08-26 34
  • 35. Denormalisation – nesting sub elements  Downside: nesting will increase the data volume stored for each key, increasing the overhead for each read and write in both the datastore and the application  If you overdo nesting, you might end up having the same data stored in multiple places, and no clear responsibility for it  This will waste space and cause consistency issues  For example, a change to user2's „likes“ attribute requires 3 writes: key value user1 { likes: [ ... ], friends: [ { user2: { likes: [ ... ] } }, user15: { likes: [ ... ] } }, ... ] } user2 { likes: [ ... ], friends: [ { user67: { likes: [ ... ] } }, user1: { likes: [ ... ] } }, ... ] } user15 { likes: [ ... ], friends: [ { user2: { likes: [ ... ] } }, user99: { likes: [ ... ] } }, ... ] } © 2012 triAGENS GmbH | 2012-08-26 35
  • 36. Denormalisation – nesting sub elements  Instead of nesting such elements fully, prefer referencing them by key / id: key value user1 { likes: [ ... ], friends: [ user2, user15 ] } user2 { likes: [ ... ], friends: [ user67, user1 ] } user15 { likes: [ ... ], friends: [ user2, user99 ] }  This will allow you to update data in only one place, saving storage space and improving consistency  The price you pay: to retrieve all details for the referenced items, you now need additional read operations © 2012 triAGENS GmbH | 2012-08-26 36
  • 37. Summary  Data modelling for NoSQL databases is different than for relational databases:  Instead of normalising your schema, you'd rather denormalise your data  Data duplication is common to support analysis and aggregation queries, and for fast retrieval  Different NoSQL databases have different features and to be used effectively, may require different data models  To make best use of a specific store, you should have some knowledge about how it organises data internally and what guarantees it provides:  Atomic operations should be exploited where possible to improve consistency  Cross-record/document consistency is mostly not guaranteed. If your store at all offers it, it will be slowed down by it © 2012 triAGENS GmbH | 2012-08-26 37  In general logic is moved from the database to the application
  • 38. Misc  NoSQL user group Cologne: every 1st Wednesday / month http://www.nosql-cologne.org/  NoSQL matters conference Barcelona: October 6th, 2012 http://www.nosql-matters.org/  ArangoDB 1.0 release: September 2012 http://www.arangodb.org/ © 2012 triAGENS GmbH | 2012-08-26 38