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
340
4
Share
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
More Decks by Yannick Schutz
See All by Yannick Schutz
All those bots are gonna steal your job
ys
1
1.2k
Ruby loves Postgres
ys
1
160
RubyMotion - Apéro Ruby Paris 05-10-2012
ys
1
3k
Other Decks in Programming
See All in Programming
CDK Deployのための ”反響定位”
watany
5
910
実用!Hono RPC2026
yodaka
2
280
エラー処理の温故知新 / history of error handling technic
ryotanakaya
7
1.8k
10 Tips of AWS ~Gen AI on AWS~
licux
5
510
Going Multiplatform with Your Android App (Android Makers 2026)
zsmb
2
470
의존성 주입과 모듈화
fornewid
0
150
Spec Driven Development | AI Summit Vilnius
danielsogl
PRO
1
120
AIを導入する前にやるべきこと
negima
2
310
ハーネスエンジニアリングとは?
kinopeee
13
6.5k
JAWS-UG横浜 #100 祝・第100回スペシャルAWS は VPC レスの時代へ
maroon1st
0
200
The Less-Told Story of Socket Timeouts
coe401_
3
860
運転動画を検索可能にする〜Cosmos-Embed1とDatabricks Vector Searchで〜/cosmos-embed1-databricks-vector-search
studio_graph
1
540
Featured
See All Featured
Tell your own story through comics
letsgokoyo
1
910
Prompt Engineering for Job Search
mfonobong
0
290
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2k
Music & Morning Musume
bryan
47
7.2k
BBQ
matthewcrist
89
10k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
659
62k
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.2k
The #1 spot is gone: here's how to win anyway
tamaranovitovic
2
1k
Building Applications with DynamoDB
mza
96
7k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
340
WCS-LA-2024
lcolladotor
0
560
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?