SlideShare a Scribd company logo
1 of 13
Download to read offline
10101010101010001010101101101010101011




It’s 11 p.m., Do you know where you queries
are?
Eric Farrar, Sybase iAnywhere
Outline


  What are ORMs and Active Records?
    Tradeoffs
  Playing Nice with your Database
    Managing Indexes
    Eager Loading and Client-Side Joins
    Lazy Loading
  Conclusion
Object-Relational Mapper


  Systems to bridge the gap between object-oriented languages
   and relational databases
                                     class Employee < ActiveRecord::Base
                                         belongs_to :office
                                     end

                                     class Office < ActiveRecord::Base
                                         has_one :employee
                                     end


  Inherently difficult:
     Normalization (splitting data across tables)
     Databases can only store scalar values
     Add an extra layer of abstraction
Active Record Pattern


  The ‘meat’ of an ORM that handles the CRUD work
  Allows regular objects to be treated as persistent objects
  Ideally, totally abstracts all database interaction

     my_office = Office.new()
     my_office.number = 123
     me = Employee.new
     me.name = ‘Eric Farrar’
     me.office = my_office
Examples of ORMs/Active Records


    LINQ (Language Integrated Query)
    Hibernate / NHibernate
    Django
    Ruby on Rails (ActiveRecord)
    Many more…



  For our purposes, we will use Rail’s ActiveRecord for the
   examples
Trade-offs


  Advantages
      Easy to learn
      Simplifies database creation and management
      No context switching between languages
      You don’t need know about the database
  Disadvantages
      Performance suffers (up to 50% slower)
      Often uses lowest-common denominator solution
      Concurrency semantics often very difficult
      You don’t need know about the database
Managing Indexes


  Indexes are used to make things quick to look up
    phone book vs. reverse look-up
  Indexes should be present on anything you will search for
  Searching for non-indexed properties will result in full table
   scan
  By default, indexes are usually only put on primary keys
  Lack of indexes often will not appear during development
  Result will be a gradual slowdown (as data volume increases)
   as opposed to avalanche failure
  Why not put an index on everything?
  Multi-column indexes vs. single column indexes
Client-Side Join


  Objects are usually ‘related’ to each other
        belongs_to
        has_one
        has_many
        has_and_belongs_to_many
  ORMs use these relationship to allow object traversal
    ex. me.office
  Assuming 10000 employees, how many queries will this code
   produce?
   Employees.find(:all).each do |e|
       puts e.office.number
   end
“Man, this is heavy!”


  Answer: 10001
   Employees.find(:all).each do |e|   # <-- 1 query here
     puts e.office.number             # <-- 10,000 queries here
   end


  Why? The application is doing the work of joining the data, not
   the database. This is called a ‘client-side’ join
  This is solved by giving a hint to the ORM and the database
   that you intend to use the ‘office’ property
   Employees.find(:all :include => :office).each do |e|
     puts e.office.number
   end


  This pattern is called eager loading
Inviting the Database to the Party


  Eager loading solves the N+1 problem, but it is still only half
   way there
  In ORMs, the relations are defined inside the object models
  The ORM may know that Employees are Offices are related,
   but the database doesn’t know that
  The database will obediently execute the query, but don’t
   expect it to do anything clever
     Modern query optimizers will use every statistic available when
      determining query paths
     Keeping them ignorant will result in bare-bones optimization
Lazy Loading


  Eager loading deals with the case where you want more than
   your class includes
  What if you want less?
    Suppose your Employee class includes a picture field that is a
     high resolution bitmap (~ 3 mb)
    The previous query will actually return the picture in order to fully
     populate the object

    Employees.find(:all).each do |e|
      puts e.name
    end


    This innocent code will naively return > 30 Gb of data
Be Lazy


  Instead, lazily load your object properties
  Employees.find(:all :select => [“name”]).each do |e|
    puts e.name
  end



  Accessing e.picture will work by issuing another database
   query
  This simple example ignores potential problems with
   concurrency
    Use locking
Conclusions


  ORMs and Active Records can provide large productivity
   advantages, typically at the expense of performance
  ORMs should never be seen as an alternative to learning
   about databases (although it can be a good introduction)
  At times, you will likely need to drop down to the database
   level (profiling, etc) to diagnose problems
  Ideally, a programmer using a ORM will always consider how
   their code will actually look once it hits the database
    Similarities to a C compiler
  You should be able to answer “Yes!” to the question, “Do you
   know where your queries are?”

More Related Content

Similar to Ajax World2008 Eric Farrar

Smart Client Development
Smart Client DevelopmentSmart Client Development
Smart Client DevelopmentTamir Khason
 
When To Use Ruby On Rails
When To Use Ruby On RailsWhen To Use Ruby On Rails
When To Use Ruby On Railsdosire
 
Rails Conf Europe 2007 Notes
Rails Conf  Europe 2007  NotesRails Conf  Europe 2007  Notes
Rails Conf Europe 2007 NotesRoss Lawley
 
U C2007 My S Q L Performance Cookbook
U C2007  My S Q L  Performance  CookbookU C2007  My S Q L  Performance  Cookbook
U C2007 My S Q L Performance Cookbookguestae36d0
 
What SQL should actually be...
What SQL should actually be...What SQL should actually be...
What SQL should actually be...Open Academy
 
Act! Technical Training
Act! Technical TrainingAct! Technical Training
Act! Technical TrainingMike Lazarus
 
MongoDB World 2019: Raiders of the Anti-patterns: A Journey Towards Fixing Sc...
MongoDB World 2019: Raiders of the Anti-patterns: A Journey Towards Fixing Sc...MongoDB World 2019: Raiders of the Anti-patterns: A Journey Towards Fixing Sc...
MongoDB World 2019: Raiders of the Anti-patterns: A Journey Towards Fixing Sc...MongoDB
 
World-class Data Engineering with Amazon Redshift
World-class Data Engineering with Amazon RedshiftWorld-class Data Engineering with Amazon Redshift
World-class Data Engineering with Amazon RedshiftLars Kamp
 
Understanding and building big data Architectures - NoSQL
Understanding and building big data Architectures - NoSQLUnderstanding and building big data Architectures - NoSQL
Understanding and building big data Architectures - NoSQLHyderabad Scalability Meetup
 
Rails and Legacy Databases - RailsConf 2009
Rails and Legacy Databases - RailsConf 2009Rails and Legacy Databases - RailsConf 2009
Rails and Legacy Databases - RailsConf 2009Brian Hogan
 
Inventing the future Business Programming Language
Inventing the future  Business Programming LanguageInventing the future  Business Programming Language
Inventing the future Business Programming LanguageESUG
 
Buildingsocialanalyticstoolwithmongodb
BuildingsocialanalyticstoolwithmongodbBuildingsocialanalyticstoolwithmongodb
BuildingsocialanalyticstoolwithmongodbMongoDB APAC
 
Linux Shell Scripting Craftsmanship
Linux Shell Scripting CraftsmanshipLinux Shell Scripting Craftsmanship
Linux Shell Scripting Craftsmanshipbokonen
 
In-Memory Logical Data Warehouse for accelerating Machine Learning Pipelines ...
In-Memory Logical Data Warehouse for accelerating Machine Learning Pipelines ...In-Memory Logical Data Warehouse for accelerating Machine Learning Pipelines ...
In-Memory Logical Data Warehouse for accelerating Machine Learning Pipelines ...Gianmario Spacagna
 
Variables & Expressions
Variables & ExpressionsVariables & Expressions
Variables & ExpressionsRich Price
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Top 10 Application Problems
Top 10 Application ProblemsTop 10 Application Problems
Top 10 Application ProblemsAppDynamics
 

Similar to Ajax World2008 Eric Farrar (20)

Smart Client Development
Smart Client DevelopmentSmart Client Development
Smart Client Development
 
Workin On The Rails Road
Workin On The Rails RoadWorkin On The Rails Road
Workin On The Rails Road
 
When To Use Ruby On Rails
When To Use Ruby On RailsWhen To Use Ruby On Rails
When To Use Ruby On Rails
 
Do you queue (updated)
Do you queue (updated)Do you queue (updated)
Do you queue (updated)
 
Rails Conf Europe 2007 Notes
Rails Conf  Europe 2007  NotesRails Conf  Europe 2007  Notes
Rails Conf Europe 2007 Notes
 
U C2007 My S Q L Performance Cookbook
U C2007  My S Q L  Performance  CookbookU C2007  My S Q L  Performance  Cookbook
U C2007 My S Q L Performance Cookbook
 
What SQL should actually be...
What SQL should actually be...What SQL should actually be...
What SQL should actually be...
 
Qure Tech Presentation
Qure Tech PresentationQure Tech Presentation
Qure Tech Presentation
 
Act! Technical Training
Act! Technical TrainingAct! Technical Training
Act! Technical Training
 
MongoDB World 2019: Raiders of the Anti-patterns: A Journey Towards Fixing Sc...
MongoDB World 2019: Raiders of the Anti-patterns: A Journey Towards Fixing Sc...MongoDB World 2019: Raiders of the Anti-patterns: A Journey Towards Fixing Sc...
MongoDB World 2019: Raiders of the Anti-patterns: A Journey Towards Fixing Sc...
 
World-class Data Engineering with Amazon Redshift
World-class Data Engineering with Amazon RedshiftWorld-class Data Engineering with Amazon Redshift
World-class Data Engineering with Amazon Redshift
 
Understanding and building big data Architectures - NoSQL
Understanding and building big data Architectures - NoSQLUnderstanding and building big data Architectures - NoSQL
Understanding and building big data Architectures - NoSQL
 
Rails and Legacy Databases - RailsConf 2009
Rails and Legacy Databases - RailsConf 2009Rails and Legacy Databases - RailsConf 2009
Rails and Legacy Databases - RailsConf 2009
 
Inventing the future Business Programming Language
Inventing the future  Business Programming LanguageInventing the future  Business Programming Language
Inventing the future Business Programming Language
 
Buildingsocialanalyticstoolwithmongodb
BuildingsocialanalyticstoolwithmongodbBuildingsocialanalyticstoolwithmongodb
Buildingsocialanalyticstoolwithmongodb
 
Linux Shell Scripting Craftsmanship
Linux Shell Scripting CraftsmanshipLinux Shell Scripting Craftsmanship
Linux Shell Scripting Craftsmanship
 
In-Memory Logical Data Warehouse for accelerating Machine Learning Pipelines ...
In-Memory Logical Data Warehouse for accelerating Machine Learning Pipelines ...In-Memory Logical Data Warehouse for accelerating Machine Learning Pipelines ...
In-Memory Logical Data Warehouse for accelerating Machine Learning Pipelines ...
 
Variables & Expressions
Variables & ExpressionsVariables & Expressions
Variables & Expressions
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Top 10 Application Problems
Top 10 Application ProblemsTop 10 Application Problems
Top 10 Application Problems
 

More from rajivmordani

Web 2 0 Data Visualization With Jsf
Web 2 0 Data Visualization With JsfWeb 2 0 Data Visualization With Jsf
Web 2 0 Data Visualization With Jsfrajivmordani
 
X Aware Ajax World V1
X Aware Ajax World V1X Aware Ajax World V1
X Aware Ajax World V1rajivmordani
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5arajivmordani
 
Tripit Ajaxworld V5
Tripit Ajaxworld V5Tripit Ajaxworld V5
Tripit Ajaxworld V5rajivmordani
 
Turbo Enterprise Web 2.0 Ajax World 20081
Turbo Enterprise Web 2.0 Ajax World 20081Turbo Enterprise Web 2.0 Ajax World 20081
Turbo Enterprise Web 2.0 Ajax World 20081rajivmordani
 
Sue Googe Spice Up Ux
Sue Googe Spice Up UxSue Googe Spice Up Ux
Sue Googe Spice Up Uxrajivmordani
 
Social Networking Intranet
Social Networking IntranetSocial Networking Intranet
Social Networking Intranetrajivmordani
 
Practical Thin Server Architecture With Dojo Peter Svensson
Practical Thin Server Architecture With Dojo Peter SvenssonPractical Thin Server Architecture With Dojo Peter Svensson
Practical Thin Server Architecture With Dojo Peter Svenssonrajivmordani
 
Server Side Javascript
Server Side JavascriptServer Side Javascript
Server Side Javascriptrajivmordani
 
Slow Cool 20081009 Final
Slow Cool 20081009 FinalSlow Cool 20081009 Final
Slow Cool 20081009 Finalrajivmordani
 
Pushing Datatothe Browserwith Comet Ajax W
Pushing Datatothe Browserwith Comet Ajax WPushing Datatothe Browserwith Comet Ajax W
Pushing Datatothe Browserwith Comet Ajax Wrajivmordani
 
I Phone Dev Summit Prezo Guy Naor Final
I Phone Dev Summit Prezo Guy Naor FinalI Phone Dev Summit Prezo Guy Naor Final
I Phone Dev Summit Prezo Guy Naor Finalrajivmordani
 
Netapp Michael Galpin
Netapp Michael GalpinNetapp Michael Galpin
Netapp Michael Galpinrajivmordani
 
Mike Grushin Developing Ugc Sites That Scale
Mike Grushin    Developing Ugc Sites That ScaleMike Grushin    Developing Ugc Sites That Scale
Mike Grushin Developing Ugc Sites That Scalerajivmordani
 
Java Fx Ajaxworld Rags V1
Java Fx Ajaxworld Rags V1Java Fx Ajaxworld Rags V1
Java Fx Ajaxworld Rags V1rajivmordani
 
Good Parts of JavaScript Douglas Crockford
Good Parts of JavaScript Douglas CrockfordGood Parts of JavaScript Douglas Crockford
Good Parts of JavaScript Douglas Crockfordrajivmordani
 
Flying Pixels Ent Apps Jeremy Chone
Flying Pixels Ent Apps Jeremy ChoneFlying Pixels Ent Apps Jeremy Chone
Flying Pixels Ent Apps Jeremy Chonerajivmordani
 

More from rajivmordani (20)

Web 2 0 Data Visualization With Jsf
Web 2 0 Data Visualization With JsfWeb 2 0 Data Visualization With Jsf
Web 2 0 Data Visualization With Jsf
 
X Aware Ajax World V1
X Aware Ajax World V1X Aware Ajax World V1
X Aware Ajax World V1
 
The Beauty Of Java Script V5a
The Beauty Of Java Script V5aThe Beauty Of Java Script V5a
The Beauty Of Java Script V5a
 
Tripit Ajaxworld V5
Tripit Ajaxworld V5Tripit Ajaxworld V5
Tripit Ajaxworld V5
 
Turbo Enterprise Web 2.0 Ajax World 20081
Turbo Enterprise Web 2.0 Ajax World 20081Turbo Enterprise Web 2.0 Ajax World 20081
Turbo Enterprise Web 2.0 Ajax World 20081
 
Sue Googe Spice Up Ux
Sue Googe Spice Up UxSue Googe Spice Up Ux
Sue Googe Spice Up Ux
 
Social Networking Intranet
Social Networking IntranetSocial Networking Intranet
Social Networking Intranet
 
Ssjs Presentation
Ssjs PresentationSsjs Presentation
Ssjs Presentation
 
Jsf Ajax
Jsf AjaxJsf Ajax
Jsf Ajax
 
Practical Thin Server Architecture With Dojo Peter Svensson
Practical Thin Server Architecture With Dojo Peter SvenssonPractical Thin Server Architecture With Dojo Peter Svensson
Practical Thin Server Architecture With Dojo Peter Svensson
 
Server Side Javascript
Server Side JavascriptServer Side Javascript
Server Side Javascript
 
Ria Enterprise
Ria EnterpriseRia Enterprise
Ria Enterprise
 
Slow Cool 20081009 Final
Slow Cool 20081009 FinalSlow Cool 20081009 Final
Slow Cool 20081009 Final
 
Pushing Datatothe Browserwith Comet Ajax W
Pushing Datatothe Browserwith Comet Ajax WPushing Datatothe Browserwith Comet Ajax W
Pushing Datatothe Browserwith Comet Ajax W
 
I Phone Dev Summit Prezo Guy Naor Final
I Phone Dev Summit Prezo Guy Naor FinalI Phone Dev Summit Prezo Guy Naor Final
I Phone Dev Summit Prezo Guy Naor Final
 
Netapp Michael Galpin
Netapp Michael GalpinNetapp Michael Galpin
Netapp Michael Galpin
 
Mike Grushin Developing Ugc Sites That Scale
Mike Grushin    Developing Ugc Sites That ScaleMike Grushin    Developing Ugc Sites That Scale
Mike Grushin Developing Ugc Sites That Scale
 
Java Fx Ajaxworld Rags V1
Java Fx Ajaxworld Rags V1Java Fx Ajaxworld Rags V1
Java Fx Ajaxworld Rags V1
 
Good Parts of JavaScript Douglas Crockford
Good Parts of JavaScript Douglas CrockfordGood Parts of JavaScript Douglas Crockford
Good Parts of JavaScript Douglas Crockford
 
Flying Pixels Ent Apps Jeremy Chone
Flying Pixels Ent Apps Jeremy ChoneFlying Pixels Ent Apps Jeremy Chone
Flying Pixels Ent Apps Jeremy Chone
 

Recently uploaded

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
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
 
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
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
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
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
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
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
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
 
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
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
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
 

Recently uploaded (20)

08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
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...
 
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
 
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
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
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
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
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
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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
 
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
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
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?
 

Ajax World2008 Eric Farrar

  • 1. 10101010101010001010101101101010101011 It’s 11 p.m., Do you know where you queries are? Eric Farrar, Sybase iAnywhere
  • 2. Outline  What are ORMs and Active Records?  Tradeoffs  Playing Nice with your Database  Managing Indexes  Eager Loading and Client-Side Joins  Lazy Loading  Conclusion
  • 3. Object-Relational Mapper  Systems to bridge the gap between object-oriented languages and relational databases class Employee < ActiveRecord::Base belongs_to :office end class Office < ActiveRecord::Base has_one :employee end  Inherently difficult:  Normalization (splitting data across tables)  Databases can only store scalar values  Add an extra layer of abstraction
  • 4. Active Record Pattern  The ‘meat’ of an ORM that handles the CRUD work  Allows regular objects to be treated as persistent objects  Ideally, totally abstracts all database interaction my_office = Office.new() my_office.number = 123 me = Employee.new me.name = ‘Eric Farrar’ me.office = my_office
  • 5. Examples of ORMs/Active Records  LINQ (Language Integrated Query)  Hibernate / NHibernate  Django  Ruby on Rails (ActiveRecord)  Many more…  For our purposes, we will use Rail’s ActiveRecord for the examples
  • 6. Trade-offs  Advantages  Easy to learn  Simplifies database creation and management  No context switching between languages  You don’t need know about the database  Disadvantages  Performance suffers (up to 50% slower)  Often uses lowest-common denominator solution  Concurrency semantics often very difficult  You don’t need know about the database
  • 7. Managing Indexes  Indexes are used to make things quick to look up  phone book vs. reverse look-up  Indexes should be present on anything you will search for  Searching for non-indexed properties will result in full table scan  By default, indexes are usually only put on primary keys  Lack of indexes often will not appear during development  Result will be a gradual slowdown (as data volume increases) as opposed to avalanche failure  Why not put an index on everything?  Multi-column indexes vs. single column indexes
  • 8. Client-Side Join  Objects are usually ‘related’ to each other  belongs_to  has_one  has_many  has_and_belongs_to_many  ORMs use these relationship to allow object traversal  ex. me.office  Assuming 10000 employees, how many queries will this code produce? Employees.find(:all).each do |e| puts e.office.number end
  • 9. “Man, this is heavy!”  Answer: 10001 Employees.find(:all).each do |e| # <-- 1 query here puts e.office.number # <-- 10,000 queries here end  Why? The application is doing the work of joining the data, not the database. This is called a ‘client-side’ join  This is solved by giving a hint to the ORM and the database that you intend to use the ‘office’ property Employees.find(:all :include => :office).each do |e| puts e.office.number end  This pattern is called eager loading
  • 10. Inviting the Database to the Party  Eager loading solves the N+1 problem, but it is still only half way there  In ORMs, the relations are defined inside the object models  The ORM may know that Employees are Offices are related, but the database doesn’t know that  The database will obediently execute the query, but don’t expect it to do anything clever  Modern query optimizers will use every statistic available when determining query paths  Keeping them ignorant will result in bare-bones optimization
  • 11. Lazy Loading  Eager loading deals with the case where you want more than your class includes  What if you want less?  Suppose your Employee class includes a picture field that is a high resolution bitmap (~ 3 mb)  The previous query will actually return the picture in order to fully populate the object Employees.find(:all).each do |e| puts e.name end  This innocent code will naively return > 30 Gb of data
  • 12. Be Lazy  Instead, lazily load your object properties Employees.find(:all :select => [“name”]).each do |e| puts e.name end  Accessing e.picture will work by issuing another database query  This simple example ignores potential problems with concurrency  Use locking
  • 13. Conclusions  ORMs and Active Records can provide large productivity advantages, typically at the expense of performance  ORMs should never be seen as an alternative to learning about databases (although it can be a good introduction)  At times, you will likely need to drop down to the database level (profiling, etc) to diagnose problems  Ideally, a programmer using a ORM will always consider how their code will actually look once it hits the database  Similarities to a C compiler  You should be able to answer “Yes!” to the question, “Do you know where your queries are?”