28
Modeling Denormalization Duncan Beevers 2008

Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Modeling Denormalization

Duncan Beevers 2008

Page 2: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

What is denormalization?

Duplicating facts

Duplicating facts

Page 3: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Modeling

Piles of things are things

class Game < ActiveRecord::Base has_many :plays has_one :lifetime_plays_countend

Page 4: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Where we started

Fully normalized data

Page 5: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

First denormalization

Page 6: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Why denormalize?

Large number of rows

Frequent expensive queries

Complex business rules about data

Page 7: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Complex Business Rules

They make simple Business Rules?

Page 8: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Model the requirement

Just get it working first

Page 9: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

class DailyHighScore < ActiveRecord::Base set_table_name 'scores'

default_scope :select => "#{quoted_table_name}.*, MAX(value) AS value" named_scope :for_date, lambda { |date| { :conditions => { :created_at => date.beginning_of_day .. date.end_of_day } } }end

class User < ActiveRecord::Base has_many :daily_high_scores end

user.daily_high_scores. for_date(Date.today).first

Page 10: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

class ActiveRecord::Base def self.default_scope options self.scoped_methods << { :find => options } endend

Page 11: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Haphazard denormalization

Page 12: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Minimize Indices-per-tablemore indices = slower writes

Page 13: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Minimize complex queriesMake sure you're using your indices!

Page 14: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Eventual Consistency

Why use real tables instead of triggers or views?

Page 15: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Do it later

Batch processing can save you writes and lighten replication load

Page 16: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Take a picture, it'll last longer

Denormalized models capture history

Page 17: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Like Observers, but different

class DailyHighScore uniqueness :user_id, :statistic_id, :date

takes :user_id, :statistic_id, :from => Score

takes :value, :from => Score, :if => :new_score_better?

def new_score_better? new_score new_score.better_than? self.value endend

Page 18: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Use Descriptive NamesVeryLongDescriptiveNamesThatProgrammingPairsThinkProvideGoodDescriptions

class LifetimeHighScoreend

class DailyGameplaysCountend

class MonthlyPaymentRunend

Page 19: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Avoiding our mistakes

Updates to models triggered external API calls

Single Table Inheritance

Page 20: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Use a queue

Lots of choices out there, or just roll your own

Page 22: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Moving to the new scheme

Pulling up stakes

Page 23: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Interim strategies

Disabling features and turning tables into placeholders

CREATE TABLE dummy_#{table_name}LIKE #{table_name}ENGINE=BLACKHOLE

ewww SQL

Page 24: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Toolbox

Bulk insert http://gist.github.com/7681

Hash rewrite http://www.dweebd.com/ruby/hash-key-rewrite/

{ :username => 'johnnyfive' }. rewrite(:username => :login)

=> { :login => 'johnnyfive' }

Gameplay.insert( [ { :user_id => 1, :game_id => 1 }, { :user_id => 2, :game_id => 1 }] )

Page 25: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Catch-up

Creating denormalized data should be idempotent

Page 26: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Pick the easy ones first

There are lots of ways to speed up an application

Page 27: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Make models judiciously

Page 28: Modeling Denormalization - O'Reilly Mediaassets.en.oreilly.com/1/event/13/Modeling... · Haphazard denormalization. Minimize Indices-per-table more indices = slower writes. Minimize

Over 3 million badges awarded in August

~1.5 badges per second