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
Rails with Massive Data
Search
Yi-Ting Cheng
August 19, 2012
Technology
13
36k
Rails with Massive Data
Ruby Tuesday #21 ( Taiwan )
Yi-Ting Cheng
August 19, 2012
Tweet
Share
More Decks by Yi-Ting Cheng
See All by Yi-Ting Cheng
Blitzbuilding Product with Rails
xdite
2
1.4k
第六屆做自己論壇 - Xdite 鄭伊廷
xdite
4
5.6k
Refactoring lesson : from GPA 1.4 to GPA 3.0
xdite
8
1.5k
全棧班畢業贈語
xdite
1
40k
Intro to RedPotion
xdite
0
250
莫拉克颱風災情支援網
xdite
1
300
Intro to self Growth Hack
xdite
61
20k
Building a workshop / community
xdite
6
1.2k
Building SaaS using Rails
xdite
15
2.1k
Other Decks in Technology
See All in Technology
Autify Company Deck
autifyhq
1
30k
理想の組織も自分たちで作ろう! ―LayerXの「全員採用」を支える文化 / How to create our own ideal team
ar_tama
7
2.5k
本気でプロダクトに向き合うCTOになるために必要な事 (技育祭2024春)
mosa_siru
45
14k
エンジニアブランディングチームの KPI / KPI's of engineer branding team
chaspy
1
130
サービスメッシュ環境における OpenTelemetry 活用 / OpenTelemetry in Service Mesh
k6s4i53rx
2
800
社内共通ルールを値オブジェクトにして社内ライブラリとして運用してみた話
leveragestech
2
720
暗黙知を集積するプラットフォーム : 「健常者エミュレータ事例集」の取り組み
sora32127
1
160
GitHub最新情報キャッチアップ 2024年3月
dzeyelid
16
3.1k
統計的学習理論読み Chapter 1
kmatsui
2
500
学生としてRSGT2024に参加して得られた学び / Lessons learned from participating in RSGT2024 as a student
fujiemon
0
110
#51 “Empowering Azure Storage with RDMA”
cafenero_777
3
200
ハイパフォーマンスな組織をつくるための開発生産性の考え方 / developer-productivity-high-performer-link-and-motivation
lmi
3
220
Featured
See All Featured
The Power of CSS Pseudo Elements
geoffreycrofte
58
4.9k
Statistics for Hackers
jakevdp
789
220k
WebSockets: Embracing the real-time Web
robhawkes
59
6.9k
Automating Front-end Workflow
addyosmani
1353
200k
Why You Should Never Use an ORM
jnunemaker
PRO
50
8.6k
How To Stay Up To Date on Web Technology
chriscoyier
781
250k
Fantastic passwords and where to find them - at NoRuKo
philnash
35
2.4k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
219
21k
The Brand Is Dead. Long Live the Brand.
mthomps
48
21k
Testing 201, or: Great Expectations
jmmastey
27
6.3k
Raft: Consensus for Rubyists
vanstee
130
6.2k
The Art of Programming - Codeland 2020
erikaheidi
40
12k
Transcript
Rails with Massive Data 10 things you should know 12年8月19日星期日
about • fb.com/xdite • twitter.com/xdite • rocodev.com 12年8月19日星期日
Agenda • Don’t use ActiveRecord • Don’t use ActiveRecord •
Don’t use ActiveRecord • Don’t use ActiveRecord • Don’t use ActiveRecord • ............ Unless you know what you’re doing 12年8月19日星期日
#1. Active Record is danger 12年8月19日星期日
typical usage posts = Post.where(:board_id => 5) post.each do |post|
post.board_id = 1 post.save end ~ 1000 data : cool ~ 1000000 data : hell 12年8月19日星期日
problems posts = Post.where(:board_id => 5) post.each do |post| post.board_id
= 1 post.save end load ~1000000 objects in memory trigger ~1000000 callbacks DB transaction update DB indexes 12年8月19日星期日
problems • memory bloat • too much callbacks • too
much DB transaction • slow query ( update db indexes) 12年8月19日星期日
#2. update_all 12年8月19日星期日
update_all posts = Post.where(:board_id => 5) post.each do |post| post.board_id
= 1 post.save end Post.update_all({:board_id => 1}, {:board_id => 5}) 12年8月19日星期日
#3. find_in_batches 12年8月19日星期日
find_in_batches Post.find_in_batches(:conditions => "board_id = 5", :batch_size => 1000) do
|posts| posts.each do |post| post.board_id = 1 post.save end end load only ~1000 objects in memory 12年8月19日星期日
#4. transaction 12年8月19日星期日
transaction (0.3ms) BEGIN (0.5ms) COMMIT ~1000000 DB transaction 12年8月19日星期日
transaction Post.find_in_batches(:conditions => "board_id = 5", :batch_size => 1000) do
| posts| Post.transaction do posts.each do |post| post.board_id = 1 post.save end end end ~ only 1000 transactions 12年8月19日星期日
#5. update_column 12年8月19日星期日
update_column posts = Post.where(:board_id => 5) post.each do |post| post.update_column(:board_id,
1 ) end ~ skip 1000000 * n callbacks 12年8月19日星期日
sneaky-save (gem) posts = Post.where(:board_id => 5) post.each do |post|
post.board_id = 1 post.sneaky_save end ~ skip 1000000 * n callbacks 12年8月19日星期日
any question? 12年8月19日星期日
#6. select only needed 12年8月19日星期日
select only needed posts = Post.where(“id < 10”) Post Load
(18.8ms) SELECT `posts`.* FROM `posts` WHERE (id < 10) “post.content” ~ 100k 10000 record ~ 1G Post.select("column 1, colum2").where 12年8月19日星期日
#7. delegate 12年8月19日星期日
move out big data class Post < ActiveRecord::Base has_one :meta
after_create :create_meta delegate :content, :to => :meta end # -*- encoding : utf-8 -*- # == Schema Information # # Table name: post_data # # id :integer not null, primary key # post_id :integer # content :text # created_at :datetime not null # updated_at :datetime not null # 12年8月19日星期日
#8. indexes 12年8月19日星期日
add index on foreign key posts = Post.where(:board_id => 5)
add_index :posts, :board_id 12年8月19日星期日
integer & varchar # -*- encoding : utf-8 -*- #
== Schema Information # # Table name: post # # id :integer not null, primary key # board_id :integer # content :text # created_at :datetime not null # updated_at :datetime not null # # -*- encoding : utf-8 -*- # == Schema Information # # Table name: post # # id :integer not null, primary key # board_id :string(255) # content :text # created_at :datetime not null # updated_at :datetime not null # ~100x slower 12年8月19日星期日
對 MySQL 的 VARCHAR 欄位使用 INDEX 時 可以增加效率的方法… http://bit.ly/QdEK19 12年8月19日星期日
MySQL Indexing Best Practices http://bit.ly/Spi6F8 12年8月19日星期日
#9. delete / destroy 12年8月19日星期日
delete / destroy • destroy is slow • destroy go
through callbacks 12年8月19日星期日
delete / destroy • delete is also slow..... • DELETE
update indexes 12年8月19日星期日
solution 1. acts_as_archive ( gem ) (soft_delete) 2. INSERT to
new table 12年8月19日星期日
#10. background job 12年8月19日星期日
background job 1. delayed_job ( not recommended) 2. resque 3.
sidekiq 12年8月19日星期日
any question? 12年8月19日星期日
Thanks for listening 12年8月19日星期日