SlideShare una empresa de Scribd logo
1 de 63
Descargar para leer sin conexión
MYSQL TO
MONGO (FTW)
Thinking Differently About Schema Design
@AJSHARP

Alex Sharp
Lead Developer, OptimisCorp

alexjsharp.com

github.com/ajsharp
CVBEAST
Mongo has many cool
features such as schema-
free, aggregation w
map/reduce and many
others
                           Side project


interested more in
domain modeling more
than performance and
scaling benefits of
Mongo




EMBEDDED OBJECTS
CVBEAST

 App to represent people’s curriculum vitae, but not in an
                    academic sense
CVBEAST

 Focus on “micro” experiences not suitable for a typical CV
           (definitely not suitable for a resumé)
CVBEAST

Simple object model, centered around highlighting attributes
                        of a person
CVBEAST

   Started out building with MySQL, b/c it’s familiar
OBJECT MODEL
                 Person

                 has a

                   CV

                has many

            Experience

                 has many

          ...       ...     ...
OBJECT MODEL
                           Person

                           has a

                             CV

 links, tags, and         has many
  other arbitrary
    properties        Experience

                           has many

                    ...       ...     ...
RELATIONAL SCHEMA
         people
           - id
           - name
           - ...

         cvs
           - id
           - person_id
           - ...

         experiences
           - id
           - cv_id
           - ...
RELATIONAL SCHEMA


       Lots of pointless JOINs
RELATIONAL SCHEMA
         people
           - id
           - name
           - ...

         cvs
           - id
           - person_id
           - ...

         experiences
           - id
           - cv_id
           - ...
RELATIONAL SCHEMA
         people
           - id
           - name
           - ...

         cvs             tags
                           - id
           - id            - name
           - person_id     - ...
           - ...
                         links
         experiences       - id
                           - name
           - id            - ...
           - cv_id
           - ...
not b/c of premature
optimization




                       this bothers me
It misrepresents the object model
        at the storage layer
It also became difficult to work with
 despite my familiarity with MySQL
It makes sense to store object relationships in
     first-class entities/documents/tables
But true object properties should be
        stored as properties
Not relationships.
Especially when properties have no practical
        meaning without the parent
CAVEAT


                              This is not always the case.

                                                    ;-)
if you need to:
* store LOTS of embedded objects (i.e. papermill)
EXAMPLE
Experience Links
EXPERIENCE.LINKS = [...]

     Dead-simple collection of links attached to
                  an experience
RUBY MODEL CODE
 class Person
   include Mongoid::Document

   field :keywords, :type => Array

   index :keywords
   index 'cv.experiences.tags'
 end
class Experience
  include Mongoid::Document

  embeds_many :links
end

class Link
  include Mongoid::Document

  field :url
  field :title
  embedded_in :experience, :inverse_of => :links
end
class Experience
  include Mongoid::Document

  embeds_many :links
end                    Both models are embedded in the
                              person collection
class Link
  include Mongoid::Document

  field :url
  field :title
  embedded_in :experience, :inverse_of => :links
end
EXPERIENCE.LINKS = [...]

      A link is only relevant inside the
      context of an experience object.
EXPERIENCE.LINKS = [...]

      In other words, it is a property,
            not a relationship.
EXPERIENCE.LINKS = [...]

   Mongo brings the storage layer closer to
             the object model
EXPERIENCE.LINKS = [...]
{
    "title": "Presented at MongoLA",
    "links": [
      { "title": "Event Site",
        "url": "http://www.10gen.com/conferences/mongola2011"},
      { "title": "Slides",
        "url": "http://alexjsharp.com/posts/mongola-2010-slides"}
    ]
}
EXAMPLE
Embedded Search
MYSQL
An exercise in masochism
The difficulty in MySQL came in working with
            properties of a person
These properties are represented as tables
And tables must be joined
SIMPLE SEARCH QUERY
select * from people
  inner join cvs on cvs.person_id = people.id
  inner join experiences on experiences.cv_id = cvs.id
  inner join tags on tags.experience_id = experiences.id
  where tags.name = 'ruby';
INDEXES NEEDED
 -   tags.experience_id
 -   tags.name
 -   experiences.cv_id
 -   cvs.person_id
Seems extremely unnecessary for such a
         simple object model
MONGO
An exercise in ... not masochism
Embedded objects make this query easy.
MONGO DOCUMENT “SCHEMA”
{"name": "Alex Sharp",
   "cv": {
     "experiences": [
       { "title": "spoke at MongoLA",
          "date" : "Thu Jan 13 2011",
          "tags" : ["mongodb", "speaking"]
       },
       {"title": "..."}
     ]
   }
}
MONGO DOCUMENT “SCHEMA”
{"name": "Alex Sharp",
   "cv": {
     "experiences": [
       { "title": "spoke at MongoLA",
          "date" : "Thu Jan 13 2011",
          "tags" : ["mongodb", "speaking"]
       },
       {"title": "..."}
     ]
   }
}
   we want to search for these
RUBY MODEL CODE
 class Person
   include Mongoid::Document

   field :keywords, :type => Array

   index :keywords
   index 'cv.experiences.tags'
 end
RUBY MODEL CODE
class Cv
  include Mongoid::Document

  embeds_many :experiences
end

class Experience
  include Mongoid::Document

  field :tags,       :type => Array, :default => []

  embedded_in :cv, :inverse_of => :experiences
  embeds_many :links

  after_save lambda { |exp| exp.cv.person.update_keywords }
end
RUBY MODEL CODE
class Cv
  include Mongoid::Document
                                      simple property
  embeds_many :experiences
end                                  (not relationship)
class Experience
  include Mongoid::Document

  field :tags,       :type => Array, :default => []

  embedded_in :cv, :inverse_of => :experiences
  embeds_many :links

  after_save lambda { |exp| exp.cv.person.update_keywords }
end
RUBY SEARCH CODE
class Person

  # i.e. db.people.find({"cv.experiences.tags": "ruby"})
  def self.search_by_tag(term)
    collection.find('cv.experiences.tags' => 'ruby')
  end

end
COMPARISON
select * from people
  inner join cvs on cvs.person_id = people.id
  inner join experiences on experiences.cv_id = cvs.id
  inner join tags on tags.experience_id = experiences.id
  where tags.name = 'ruby';


                       vs
db.people.find('cv.experiences.tags' => 'ruby')
WINS: IMPEDENCE MIS-MATCH

   Object persistence format is closer to
         application usage format
WINS: F(X)-ALITY

    Plus, we don’t lose any functionality.
WINS: FAMILIARITY

  Indexing principles are extremely familiar to
           relational database users.
WINS: FAMILIARITY

          Only simpler ;)
WINS: SIMPLICITY

        Query syntax is simple.
WINS: SIMPLICITY

    Dot notation > JOIN semantics filth
SUMMARY

     Mongo is perfectly suited for
        an app like CVBeast.
SUMMARY

   Where a relational DB forces storing
    object properties as relationships
SUMMARY

 Mongo narrows this mismatch considerably
SUMMARY

      A CV is a document...
SUMMARY

   So a document-oriented datastore
          seems appropriate.
SUMMARY

    Many object models have this
        tree-like structure.
SUMMARY

 Be willing to step outside of your comfort zone
SUMMARY

   And use Mongo when it’s practical.

Más contenido relacionado

La actualidad más candente

An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDBCésar Trigo
 
Top 10 frameworks of node js
Top 10 frameworks of node jsTop 10 frameworks of node js
Top 10 frameworks of node jsHabilelabs
 
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbPractical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbAlex Sharp
 
Mongodb basics and architecture
Mongodb basics and architectureMongodb basics and architecture
Mongodb basics and architectureBishal Khanal
 
MongoDB Strange Loop 2009
MongoDB Strange Loop 2009MongoDB Strange Loop 2009
MongoDB Strange Loop 2009Mike Dirolf
 
MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)Uwe Printz
 
Introduction to MongoDB (from Austin Code Camp)
Introduction to MongoDB (from Austin Code Camp)Introduction to MongoDB (from Austin Code Camp)
Introduction to MongoDB (from Austin Code Camp)Chris Edwards
 
MongoDB Java Development - MongoBoston 2010
MongoDB Java Development - MongoBoston 2010MongoDB Java Development - MongoBoston 2010
MongoDB Java Development - MongoBoston 2010Eliot Horowitz
 
Building Apps with MongoDB
Building Apps with MongoDBBuilding Apps with MongoDB
Building Apps with MongoDBNate Abele
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented databaseWojciech Sznapka
 
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsMongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsSpringPeople
 
Building your first app with mongo db
Building your first app with mongo dbBuilding your first app with mongo db
Building your first app with mongo dbMongoDB
 
Webinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsWebinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsMongoDB
 
Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema DesignMongoDB
 
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'tsThe Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'tsMatias Cascallares
 

La actualidad más candente (20)

An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
 
Top 10 frameworks of node js
Top 10 frameworks of node jsTop 10 frameworks of node js
Top 10 frameworks of node js
 
Practical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo DbPractical Ruby Projects With Mongo Db
Practical Ruby Projects With Mongo Db
 
Mongodb basics and architecture
Mongodb basics and architectureMongodb basics and architecture
Mongodb basics and architecture
 
MongoDB Strange Loop 2009
MongoDB Strange Loop 2009MongoDB Strange Loop 2009
MongoDB Strange Loop 2009
 
MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)MongoDB for Coder Training (Coding Serbia 2013)
MongoDB for Coder Training (Coding Serbia 2013)
 
Introduction to MongoDB (from Austin Code Camp)
Introduction to MongoDB (from Austin Code Camp)Introduction to MongoDB (from Austin Code Camp)
Introduction to MongoDB (from Austin Code Camp)
 
MongoDB Java Development - MongoBoston 2010
MongoDB Java Development - MongoBoston 2010MongoDB Java Development - MongoBoston 2010
MongoDB Java Development - MongoBoston 2010
 
Mongo db workshop # 02
Mongo db workshop # 02Mongo db workshop # 02
Mongo db workshop # 02
 
Building Apps with MongoDB
Building Apps with MongoDBBuilding Apps with MongoDB
Building Apps with MongoDB
 
Mongo db operations_v2
Mongo db operations_v2Mongo db operations_v2
Mongo db operations_v2
 
Mongo DB
Mongo DB Mongo DB
Mongo DB
 
Mongo db – document oriented database
Mongo db – document oriented databaseMongo db – document oriented database
Mongo db – document oriented database
 
An introduction to MongoDB
An introduction to MongoDBAn introduction to MongoDB
An introduction to MongoDB
 
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorialsMongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
Mongo DB: Fundamentals & Basics/ An Overview of MongoDB/ Mongo DB tutorials
 
Building your first app with mongo db
Building your first app with mongo dbBuilding your first app with mongo db
Building your first app with mongo db
 
Webinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in DocumentsWebinar: Back to Basics: Thinking in Documents
Webinar: Back to Basics: Thinking in Documents
 
MongoDB
MongoDBMongoDB
MongoDB
 
Webinar: Schema Design
Webinar: Schema DesignWebinar: Schema Design
Webinar: Schema Design
 
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'tsThe Fine Art of Schema Design in MongoDB: Dos and Don'ts
The Fine Art of Schema Design in MongoDB: Dos and Don'ts
 

Similar a Mysql to mongo

X-TREME THEMES
X-TREME THEMESX-TREME THEMES
X-TREME THEMESFITC
 
Word2vec in Postgres
Word2vec in PostgresWord2vec in Postgres
Word2vec in PostgresGary Sieling
 
OOScss Architecture For Rails Apps
OOScss Architecture For Rails AppsOOScss Architecture For Rails Apps
OOScss Architecture For Rails AppsNetguru
 
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3Salvatore Iaconesi
 
Semantic technologies in practice - KULeuven 2016
Semantic technologies in practice - KULeuven 2016Semantic technologies in practice - KULeuven 2016
Semantic technologies in practice - KULeuven 2016Aad Versteden
 
Zend Framework And Doctrine
Zend Framework And DoctrineZend Framework And Doctrine
Zend Framework And Doctrineisaaczfoster
 
Stop the noise! - Introduction to the JSON:API specification in Drupal
Stop the noise! - Introduction to the JSON:API specification in DrupalStop the noise! - Introduction to the JSON:API specification in Drupal
Stop the noise! - Introduction to the JSON:API specification in DrupalBjörn Brala
 
Using Mongoid with Ruby on Rails
Using Mongoid with Ruby on RailsUsing Mongoid with Ruby on Rails
Using Mongoid with Ruby on RailsNicholas Altobelli
 
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram ArnoldEfficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram ArnoldMarakana Inc.
 
Sass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LASass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LAJake Johnson
 
Getting into ember.js
Getting into ember.jsGetting into ember.js
Getting into ember.jsreybango
 
Keynote - Speaker: Grigori Melnik
Keynote - Speaker: Grigori Melnik Keynote - Speaker: Grigori Melnik
Keynote - Speaker: Grigori Melnik MongoDB
 
MongoDB Days UK: Building Apps with the MEAN Stack
MongoDB Days UK: Building Apps with the MEAN StackMongoDB Days UK: Building Apps with the MEAN Stack
MongoDB Days UK: Building Apps with the MEAN StackMongoDB
 
Construction Techniques For Domain Specific Languages
Construction Techniques For Domain Specific LanguagesConstruction Techniques For Domain Specific Languages
Construction Techniques For Domain Specific LanguagesThoughtWorks
 
Rails Gems realize RESTful modeling patterns
Rails Gems realize RESTful modeling patternsRails Gems realize RESTful modeling patterns
Rails Gems realize RESTful modeling patternsToru Kawamura
 
Model Inheritance
Model InheritanceModel Inheritance
Model InheritanceLoren Davie
 

Similar a Mysql to mongo (20)

X-TREME THEMES
X-TREME THEMESX-TREME THEMES
X-TREME THEMES
 
Word2vec in Postgres
Word2vec in PostgresWord2vec in Postgres
Word2vec in Postgres
 
OOScss Architecture For Rails Apps
OOScss Architecture For Rails AppsOOScss Architecture For Rails Apps
OOScss Architecture For Rails Apps
 
MongoDb and Windows Azure
MongoDb and Windows AzureMongoDb and Windows Azure
MongoDb and Windows Azure
 
The emerging world of mongo db csp
The emerging world of mongo db   cspThe emerging world of mongo db   csp
The emerging world of mongo db csp
 
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3
Sperimentazioni di Tecnologie e Comunicazioni Multimediali: Lezione 3
 
Semantic technologies in practice - KULeuven 2016
Semantic technologies in practice - KULeuven 2016Semantic technologies in practice - KULeuven 2016
Semantic technologies in practice - KULeuven 2016
 
Zend Framework And Doctrine
Zend Framework And DoctrineZend Framework And Doctrine
Zend Framework And Doctrine
 
Stop the noise! - Introduction to the JSON:API specification in Drupal
Stop the noise! - Introduction to the JSON:API specification in DrupalStop the noise! - Introduction to the JSON:API specification in Drupal
Stop the noise! - Introduction to the JSON:API specification in Drupal
 
Using Mongoid with Ruby on Rails
Using Mongoid with Ruby on RailsUsing Mongoid with Ruby on Rails
Using Mongoid with Ruby on Rails
 
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram ArnoldEfficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
 
Sass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LASass Essentials at Mobile Camp LA
Sass Essentials at Mobile Camp LA
 
A Blink Into The Rails Magic
A Blink Into The Rails MagicA Blink Into The Rails Magic
A Blink Into The Rails Magic
 
Ams adapters
Ams adaptersAms adapters
Ams adapters
 
Getting into ember.js
Getting into ember.jsGetting into ember.js
Getting into ember.js
 
Keynote - Speaker: Grigori Melnik
Keynote - Speaker: Grigori Melnik Keynote - Speaker: Grigori Melnik
Keynote - Speaker: Grigori Melnik
 
MongoDB Days UK: Building Apps with the MEAN Stack
MongoDB Days UK: Building Apps with the MEAN StackMongoDB Days UK: Building Apps with the MEAN Stack
MongoDB Days UK: Building Apps with the MEAN Stack
 
Construction Techniques For Domain Specific Languages
Construction Techniques For Domain Specific LanguagesConstruction Techniques For Domain Specific Languages
Construction Techniques For Domain Specific Languages
 
Rails Gems realize RESTful modeling patterns
Rails Gems realize RESTful modeling patternsRails Gems realize RESTful modeling patterns
Rails Gems realize RESTful modeling patterns
 
Model Inheritance
Model InheritanceModel Inheritance
Model Inheritance
 

Más de Alex Sharp

Bldr: A Minimalist JSON Templating DSL
Bldr: A Minimalist JSON Templating DSLBldr: A Minimalist JSON Templating DSL
Bldr: A Minimalist JSON Templating DSLAlex Sharp
 
Bldr - Rubyconf 2011 Lightning Talk
Bldr - Rubyconf 2011 Lightning TalkBldr - Rubyconf 2011 Lightning Talk
Bldr - Rubyconf 2011 Lightning TalkAlex Sharp
 
Refactoring in Practice - Sunnyconf 2010
Refactoring in Practice - Sunnyconf 2010Refactoring in Practice - Sunnyconf 2010
Refactoring in Practice - Sunnyconf 2010Alex Sharp
 
Refactoring in Practice - Ruby Hoedown 2010
Refactoring in Practice - Ruby Hoedown 2010Refactoring in Practice - Ruby Hoedown 2010
Refactoring in Practice - Ruby Hoedown 2010Alex Sharp
 
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Alex Sharp
 
Practical Ruby Projects with MongoDB - Ruby Midwest
Practical Ruby Projects with MongoDB - Ruby MidwestPractical Ruby Projects with MongoDB - Ruby Midwest
Practical Ruby Projects with MongoDB - Ruby MidwestAlex Sharp
 
Practical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSFPractical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSFAlex Sharp
 
Getting Comfortable with BDD
Getting Comfortable with BDDGetting Comfortable with BDD
Getting Comfortable with BDDAlex Sharp
 
Testing Has Many Purposes
Testing Has Many PurposesTesting Has Many Purposes
Testing Has Many PurposesAlex Sharp
 

Más de Alex Sharp (9)

Bldr: A Minimalist JSON Templating DSL
Bldr: A Minimalist JSON Templating DSLBldr: A Minimalist JSON Templating DSL
Bldr: A Minimalist JSON Templating DSL
 
Bldr - Rubyconf 2011 Lightning Talk
Bldr - Rubyconf 2011 Lightning TalkBldr - Rubyconf 2011 Lightning Talk
Bldr - Rubyconf 2011 Lightning Talk
 
Refactoring in Practice - Sunnyconf 2010
Refactoring in Practice - Sunnyconf 2010Refactoring in Practice - Sunnyconf 2010
Refactoring in Practice - Sunnyconf 2010
 
Refactoring in Practice - Ruby Hoedown 2010
Refactoring in Practice - Ruby Hoedown 2010Refactoring in Practice - Ruby Hoedown 2010
Refactoring in Practice - Ruby Hoedown 2010
 
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
Practical Ruby Projects with MongoDB - Ruby Kaigi 2010
 
Practical Ruby Projects with MongoDB - Ruby Midwest
Practical Ruby Projects with MongoDB - Ruby MidwestPractical Ruby Projects with MongoDB - Ruby Midwest
Practical Ruby Projects with MongoDB - Ruby Midwest
 
Practical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSFPractical Ruby Projects with MongoDB - MongoSF
Practical Ruby Projects with MongoDB - MongoSF
 
Getting Comfortable with BDD
Getting Comfortable with BDDGetting Comfortable with BDD
Getting Comfortable with BDD
 
Testing Has Many Purposes
Testing Has Many PurposesTesting Has Many Purposes
Testing Has Many Purposes
 

Último

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 

Último (20)

Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 

Mysql to mongo

  • 1. MYSQL TO MONGO (FTW) Thinking Differently About Schema Design
  • 2. @AJSHARP Alex Sharp Lead Developer, OptimisCorp alexjsharp.com github.com/ajsharp
  • 3. CVBEAST Mongo has many cool features such as schema- free, aggregation w map/reduce and many others Side project interested more in domain modeling more than performance and scaling benefits of Mongo EMBEDDED OBJECTS
  • 4. CVBEAST App to represent people’s curriculum vitae, but not in an academic sense
  • 5. CVBEAST Focus on “micro” experiences not suitable for a typical CV (definitely not suitable for a resumé)
  • 6. CVBEAST Simple object model, centered around highlighting attributes of a person
  • 7. CVBEAST Started out building with MySQL, b/c it’s familiar
  • 8. OBJECT MODEL Person has a CV has many Experience has many ... ... ...
  • 9. OBJECT MODEL Person has a CV links, tags, and has many other arbitrary properties Experience has many ... ... ...
  • 10. RELATIONAL SCHEMA people - id - name - ... cvs - id - person_id - ... experiences - id - cv_id - ...
  • 11. RELATIONAL SCHEMA Lots of pointless JOINs
  • 12. RELATIONAL SCHEMA people - id - name - ... cvs - id - person_id - ... experiences - id - cv_id - ...
  • 13. RELATIONAL SCHEMA people - id - name - ... cvs tags - id - id - name - person_id - ... - ... links experiences - id - name - id - ... - cv_id - ...
  • 14. not b/c of premature optimization this bothers me
  • 15. It misrepresents the object model at the storage layer
  • 16. It also became difficult to work with despite my familiarity with MySQL
  • 17. It makes sense to store object relationships in first-class entities/documents/tables
  • 18. But true object properties should be stored as properties
  • 20. Especially when properties have no practical meaning without the parent
  • 21. CAVEAT This is not always the case. ;-) if you need to: * store LOTS of embedded objects (i.e. papermill)
  • 23. EXPERIENCE.LINKS = [...] Dead-simple collection of links attached to an experience
  • 24.
  • 25. RUBY MODEL CODE class Person include Mongoid::Document field :keywords, :type => Array index :keywords index 'cv.experiences.tags' end
  • 26. class Experience include Mongoid::Document embeds_many :links end class Link include Mongoid::Document field :url field :title embedded_in :experience, :inverse_of => :links end
  • 27. class Experience include Mongoid::Document embeds_many :links end Both models are embedded in the person collection class Link include Mongoid::Document field :url field :title embedded_in :experience, :inverse_of => :links end
  • 28. EXPERIENCE.LINKS = [...] A link is only relevant inside the context of an experience object.
  • 29. EXPERIENCE.LINKS = [...] In other words, it is a property, not a relationship.
  • 30. EXPERIENCE.LINKS = [...] Mongo brings the storage layer closer to the object model
  • 31. EXPERIENCE.LINKS = [...] { "title": "Presented at MongoLA", "links": [ { "title": "Event Site", "url": "http://www.10gen.com/conferences/mongola2011"}, { "title": "Slides", "url": "http://alexjsharp.com/posts/mongola-2010-slides"} ] }
  • 33.
  • 34. MYSQL An exercise in masochism
  • 35. The difficulty in MySQL came in working with properties of a person
  • 36. These properties are represented as tables
  • 37. And tables must be joined
  • 38. SIMPLE SEARCH QUERY select * from people inner join cvs on cvs.person_id = people.id inner join experiences on experiences.cv_id = cvs.id inner join tags on tags.experience_id = experiences.id where tags.name = 'ruby';
  • 39. INDEXES NEEDED - tags.experience_id - tags.name - experiences.cv_id - cvs.person_id
  • 40. Seems extremely unnecessary for such a simple object model
  • 41. MONGO An exercise in ... not masochism
  • 42. Embedded objects make this query easy.
  • 43. MONGO DOCUMENT “SCHEMA” {"name": "Alex Sharp", "cv": { "experiences": [ { "title": "spoke at MongoLA", "date" : "Thu Jan 13 2011", "tags" : ["mongodb", "speaking"] }, {"title": "..."} ] } }
  • 44. MONGO DOCUMENT “SCHEMA” {"name": "Alex Sharp", "cv": { "experiences": [ { "title": "spoke at MongoLA", "date" : "Thu Jan 13 2011", "tags" : ["mongodb", "speaking"] }, {"title": "..."} ] } } we want to search for these
  • 45. RUBY MODEL CODE class Person include Mongoid::Document field :keywords, :type => Array index :keywords index 'cv.experiences.tags' end
  • 46. RUBY MODEL CODE class Cv include Mongoid::Document embeds_many :experiences end class Experience include Mongoid::Document field :tags, :type => Array, :default => [] embedded_in :cv, :inverse_of => :experiences embeds_many :links after_save lambda { |exp| exp.cv.person.update_keywords } end
  • 47. RUBY MODEL CODE class Cv include Mongoid::Document simple property embeds_many :experiences end (not relationship) class Experience include Mongoid::Document field :tags, :type => Array, :default => [] embedded_in :cv, :inverse_of => :experiences embeds_many :links after_save lambda { |exp| exp.cv.person.update_keywords } end
  • 48. RUBY SEARCH CODE class Person # i.e. db.people.find({"cv.experiences.tags": "ruby"}) def self.search_by_tag(term) collection.find('cv.experiences.tags' => 'ruby') end end
  • 49. COMPARISON select * from people inner join cvs on cvs.person_id = people.id inner join experiences on experiences.cv_id = cvs.id inner join tags on tags.experience_id = experiences.id where tags.name = 'ruby'; vs db.people.find('cv.experiences.tags' => 'ruby')
  • 50. WINS: IMPEDENCE MIS-MATCH Object persistence format is closer to application usage format
  • 51. WINS: F(X)-ALITY Plus, we don’t lose any functionality.
  • 52. WINS: FAMILIARITY Indexing principles are extremely familiar to relational database users.
  • 53. WINS: FAMILIARITY Only simpler ;)
  • 54. WINS: SIMPLICITY Query syntax is simple.
  • 55. WINS: SIMPLICITY Dot notation > JOIN semantics filth
  • 56. SUMMARY Mongo is perfectly suited for an app like CVBeast.
  • 57. SUMMARY Where a relational DB forces storing object properties as relationships
  • 58. SUMMARY Mongo narrows this mismatch considerably
  • 59. SUMMARY A CV is a document...
  • 60. SUMMARY So a document-oriented datastore seems appropriate.
  • 61. SUMMARY Many object models have this tree-like structure.
  • 62. SUMMARY Be willing to step outside of your comfort zone
  • 63. SUMMARY And use Mongo when it’s practical.