Mixin' it Up: Datastore_edition

41
Mixin' it Up: Datastore Edition Chris Cahoon Eric Oestrich

description

Combining relational and NoSQL databases in a Rails app.

Transcript of Mixin' it Up: Datastore_edition

Page 1: Mixin' it Up: Datastore_edition

Mixin' it Up: DatastoreEdition

Chris CahoonEric Oestrich

Page 2: Mixin' it Up: Datastore_edition
Page 3: Mixin' it Up: Datastore_edition

Using only an object storeon a largeish appand then remembering you can use more than one database in a system

Page 4: Mixin' it Up: Datastore_edition

Hierarchical forms & responses

The (Central) Problem

Page 5: Mixin' it Up: Datastore_edition

Survey

Question (text)

Question Group

Question (date)

Response

Answer

Answer Group

Answer (date)

Question (phone #) Answer (phone #)

Page 6: Mixin' it Up: Datastore_edition

Lots of questionsLots of formsLots of users Lots of responses= Lots of queries

NUMBERS

Page 7: Mixin' it Up: Datastore_edition

"We need an object store!"

Page 8: Mixin' it Up: Datastore_edition

feels like ActiveRecord inside MongoDB

MONGOID!

Page 9: Mixin' it Up: Datastore_edition
Page 10: Mixin' it Up: Datastore_edition

"This form belongs_to user!"

Page 11: Mixin' it Up: Datastore_edition

"Mongoid has helpers for relations?!"

Page 12: Mixin' it Up: Datastore_edition

Response Survey

User

Not bad.

Page 13: Mixin' it Up: Datastore_edition

(months pass...)

Page 14: Mixin' it Up: Datastore_edition
Page 15: Mixin' it Up: Datastore_edition

Response

SurveyUser

Oh.

Organization

Campaign

(etc.) (very etc.)

Page 16: Mixin' it Up: Datastore_edition
Page 17: Mixin' it Up: Datastore_edition

Mongoid works perfectly for those two models

Page 18: Mixin' it Up: Datastore_edition

...but everything else is relational!

Page 19: Mixin' it Up: Datastore_edition
Page 20: Mixin' it Up: Datastore_edition

There are problems with

object stores

Page 21: Mixin' it Up: Datastore_edition

... used like a relational db

(there are things we missed from relational DBs)

Page 22: Mixin' it Up: Datastore_edition

Relations

Page 23: Mixin' it Up: Datastore_edition

Can only query one collection at

a time

Page 24: Mixin' it Up: Datastore_edition

Denormalization

Page 25: Mixin' it Up: Datastore_edition

No foreign keys

Page 26: Mixin' it Up: Datastore_edition

I accidentally the object

Page 27: Mixin' it Up: Datastore_edition

and eat it too?

Can I have my cake?

Page 28: Mixin' it Up: Datastore_edition
Page 29: Mixin' it Up: Datastore_edition

Just Mongoid

Page 30: Mixin' it Up: Datastore_edition

Querying# 2 queriessurveys = Survey.where(:campaign_id.in =>

@user.campaigns.map(&:id)) survey_ids = surveys.map(&:id) # + 1 queryResponse.where(:survey_id.in => survey_ids,

:complete => true).first.answers

# = 3 total queries for search, not chainable

Page 31: Mixin' it Up: Datastore_edition

Interlude

Page 32: Mixin' it Up: Datastore_edition
Page 33: Mixin' it Up: Datastore_edition

Mongoid & ActiveRecord

Page 34: Mixin' it Up: Datastore_edition

Bridging the gap

Page 35: Mixin' it Up: Datastore_edition
Page 36: Mixin' it Up: Datastore_edition

Search with SQL, store with Mongo# 1 queryResponse.

joins(:survey, :campaign).where('complete' => true,

"campaigns.user_id" => @user.id). first.answer_collection

# = 1 total query for search, chainable

Page 37: Mixin' it Up: Datastore_edition
Page 38: Mixin' it Up: Datastore_edition
Page 39: Mixin' it Up: Datastore_edition

Should we gemify this?

Page 40: Mixin' it Up: Datastore_edition

GitHub:

https://github.com/oestrich/mixin_it_up_datastore

(look at the branches)

Page 41: Mixin' it Up: Datastore_edition

Thanks