Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Postgresql + Ruby = :heart:
Search
Yannick Schutz
April 08, 2014
Programming
4
300
Postgresql + Ruby = :heart:
How you could use postgresql in ways you didn't imagine when using it as a simple datastore.
Yannick Schutz
April 08, 2014
Tweet
Share
More Decks by Yannick Schutz
See All by Yannick Schutz
All those bots are gonna steal your job
ys
1
1k
Ruby loves Postgres
ys
1
150
RubyMotion - Apéro Ruby Paris 05-10-2012
ys
1
3k
Other Decks in Programming
See All in Programming
Better Code Design in PHP
afilina
PRO
0
110
Hotwire or React? ~アフタートーク・本編に含めなかった話~ / Hotwire or React? after talk
harunatsujita
1
110
【Kaigi on Rails 2024】YOUTRUST スポンサーLT
krpk1900
1
310
推し活としてのrails new/oshikatsu_ha_iizo
sakahukamaki
3
2k
PLoP 2024: The evolution of the microservice architecture pattern language
cer
PRO
0
2.4k
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
510
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
340
外部システム連携先が10を超えるシステムでのアーキテクチャ設計・実装事例
kiwasaki
1
280
JavaでLチカしたい! / JJUG CCC 2024 Fall LT
nhayato
0
120
Ethereum_.pdf
nekomatu
0
380
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
4
640
Make Impossible States Impossibleを 意識してReactのPropsを設計しよう
ikumatadokoro
0
130
Featured
See All Featured
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Navigating Team Friction
lara
183
14k
Build The Right Thing And Hit Your Dates
maggiecrowley
33
2.4k
Unsuck your backbone
ammeep
668
57k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
PRO
10
700
Being A Developer After 40
akosma
86
590k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
6.9k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
505
140k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
126
18k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Fashionably flexible responsive web design (full day workshop)
malarkey
404
65k
Transcript
Ruby+ Postgresql=♥♥♥♥
Hello, I’m Yannick
None
None
PostgreSQL is more than a datastore
PostgreSQL is a relational database
PostgreSQL is webscale
PostgreSQL is webscale
PostgreSQL is open-source
PostgreSQL is maintained
PostgreSQL has a great community
PostgreSQL has new awesome features coming!
PostgreSQL will do your coffee
Let me show you it’s POWER
PROBLEM
You fetch something from an API call. Like that Oauth
call to twitter.
a) You add a new cache layer like Redis b)
You add a table with all the fields c) You add fields to an existing model d) Something else?
a) You add a new cache layer like Redis b)
You add a table with all the fields c) You add fields to an existing model d) Something else?
a) You add a new cache layer like Redis b)
You add a table with all the fields c) You add fields to an existing model d) Something else?
a) You add a new cache layer like Redis b)
You add a table with all the fields c) You add fields to an existing model d) Something else?
a) You add a new cache layer like Redis b)
You add a table with all the fields c) You add fields to an existing model d) Something else?
JSON
class AddJsonPayloadToTwitterAuth < ActiveRecord::Migration def change add_column(:twitter_auths, :json_payload, :json) end
end
PostgreSQL learn you japanese
PROBLEM
You add new user settings every two weeks and needs
new avatar sizes every three.
a) You store that in Redis b) You add a
table with all the fields c) You add fields to the existing model d) Something else?
a) You store that in Redis b) You add a
table with all the fields c) You add fields to the existing model d) Something else?
a) You store that in Redis b) You add a
table with all the fields c) You add fields to the existing model d) Something else?
a) You store that in Redis b) You add a
table with all the fields c) You add fields to the existing model d) Something else?
a) You store that in Redis b) You add a
table with all the fields c) You add fields to the existing model d) Something else?
HSTORE
add_column :users, :settings, :hstore ! class User < ActiveRecord::Base #
This exposes accessors. user.wants_push = false store_accessor :settings, :wants_push, :wants_mails, :auto_save store_accessor :avatars, :large, :medium, :small end
PostgreSQL is not mysql
PROBLEM
You have a really complex query that is blazing fast
but when you come back a week after you’re like ‘WTF is that!’
a) You keep it unreadable b) You change the behavior
c) Something else?
a) You keep it unreadable b) You change the behavior
c) Something else?
a) You keep it unreadable b) You change the behavior
c) Something else?
a) You keep it unreadable b) You change the behavior
c) Something else?
CTE AKA WITH clause
def bad_users_with_karma User.find_by_sql(query) end ! def query <<-SQL WITH bad_users
AS (SELECT * FROM users WHERE bad = true) SELECT * FROM bad_users WHERE karma > #{karma_limit} SQL end
VIEWS THANKS PSQL
PostgreSQL believes you can fly
PROBLEM
You really need to add all this complexity that makes
that query run in seconds…
a) You use a complex query b) You denormalize in
MongoDB c) Something else?
a) You use a complex query b) You denormalize in
MongoDB c) Something else?
a) You use a complex query b) You denormalize in
MongoDB c) Something else?
a) You use a complex query b) You denormalize in
MongoDB c) Something else?
VIEWS
CREATE MATERIALIZED VIEW bad_users AS SELECT * FROM users WHERE
bad = false; ! class BadUser < User def readonly? true end end
PostgreSQL might contains kittens and unicorns
PROBLEM
You need to search your users by username, name and
email.
a) You add that elastic search b) You select in
ruby c) Something else?
a) You add that elastic search b) You select in
ruby c) Something else?
a) You add that elastic search b) You select in
ruby c) Something else?
a) You add that elastic search b) You select in
ruby c) Something else?
SEARCH
# Using textacular ! User.basic_search(“yannick”) User.basic_search(name: “yan:*”) User.fuzzy_search(“yannick”) # uses
trigrams ! create index on users using gin(to_tsvector('english', name));
PostgreSQL comes in multiple flavors and colors
Now, you know that the elephant database is more than
a datastore!
Thanks!
Questions?
Questions?