Tuesday, August 20, 2013

Indexes, just like relational database!

Week 4 on M101J - Indexes

  • Indexes take addtional space, but provide much faster data retrieval.
  • Creating indexes
    • .ensureIndex( )
    • e.g. "db.students.ensureIndex( { student_id : 1 } );
    • e.g. "db.students.ensureIndex( { student_id : 1, "class" : -1 } );
  • Multi-key index
    • index key can be array
    • e.g. db.bbb.insert( { a: [1, 2, 3], b: 1 } );
    • keys cannot both be arrays
    • e.g. db.bbb.insert( { a: [1, 2, 3], b: [4, 5, 6] } );
  • Unique index
    • index key has to be unique, duplicate not allowed
    • e.g. db.students.ensureIndex( { student_id: 1, name: 1 }, {unique: true} );
  • Foreground vs background indexing
    • Foreground: fast, but blocks writes. Suitable for DBA. Can lock a replica while it's being indexed.
    • Background: slow (2~4x slower), concurrent with writes. Suitable for developers in a production setting.
  • .explained( )
    • Useful to examine a query to see if indexing is utlilized
    • Important keys: "cursor" (did it use BtreeCursor?), "nscannedObjects" (how many objects actually queried?)
  • .hint( )
    • $natural: returns result in its natural order
    • .hint( { $natural: 1 } ) will use BasicCursor instead of BTreeCursor


Monday, August 19, 2013

Nice course from 10gen

10gen (maker and distributor of MongoDB) offers some very nice courses on MongoDB:
https://education.10gen.com/

Notes from M101J - week3:

Cool stuff about MongoDB schema

  • Rich documents
    • Store array of data
  • Pre-join data (embed data)
    • Fast access
    • No "Mongo Joins"
    • No constraints
      • No primary key / foreign key
  • Atomic transaction operation
    • Within one document
  • No declared schema
    • Similar structure in documents
Living without transactions
  • Atomic operation
    • In order to accomplish it:
      • restructure code to work within same document.
      • Implement locking mechanism / semaphore
      • Tolerate inconsistency
  • One to one relationship
    • Embed or not to embed depends on:
      • Freq of access
      • sSize of items ( > 16MB? )
      • Atomicity of data
  • Benefits of embedding
    • Improved read performance
    • One round trip to DB
    • High latency: 1ms
    • High bandwidth
    • "Write" latency can be sig. improved by embedding data
  • Decision to denormalize
    • 1:1 - Embed
    • 1: many - Embed (from many to 1)
    • many : many - Link (using array of _id)

Multi-dimentional skills needed

After two weeks into all things MongoDB, this is what I think needed to be an expert:

  • Setup, monitor, and administer MongoDB on servers.
  • Understand / use MongoDB in application development.
  • Troubleshoot issues as they arise.
  • Database migration, from other dbs to MongoDB
  • "Sharding" - understand deeper scope, when does it occur and how?
  • Distinguish differences between MongoDB and other dbs, pros / cons.
  • Understand how MongoDB performs / reacts on different storage technologies (SAS vs SSD vs PCI).
  • Understand advanced inner-working of MongoDB.