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
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
Yi-Ting Cheng
August 19, 2012
Technology
37k
13
Share
Rails with Massive Data
Ruby Tuesday #21 ( Taiwan )
Yi-Ting Cheng
August 19, 2012
More Decks by Yi-Ting Cheng
See All by Yi-Ting Cheng
Blitzbuilding Product with Rails
xdite
2
1.8k
第六屆做自己論壇 - Xdite 鄭伊廷
xdite
4
5.9k
Refactoring lesson : from GPA 1.4 to GPA 3.0
xdite
8
1.7k
全棧班畢業贈語
xdite
1
40k
Intro to RedPotion
xdite
0
310
莫拉克颱風災情支援網
xdite
1
400
Intro to self Growth Hack
xdite
61
20k
Building a workshop / community
xdite
6
1.3k
Building SaaS using Rails
xdite
15
2.2k
Other Decks in Technology
See All in Technology
freee-mcpを Local→Remote で出してわかった MCP認可実装のリアル
terara
3
890
layerx-fde-practices
cipepser
6
2.8k
最低限これだけ押さえれ大丈夫_Claude Enterprise/Team企業展開ガバナンス入門
tkikuchi
1
330
JEP 522 Deep Dive - G1 GC同期コスト削減によるスループット向上を徹底検証&解説
tabatad
1
210
インフラが苦手でも大丈夫! 紙芝居 Kubernetes -WWGT 10周年編-
aoi1
1
280
Javaコミュニティをもっと楽しむための9箇条
takasyou
0
320
食べログのサーキットブレーカー導入を振り返って
atpons
1
140
管理アカウント単一運用からAWS Organizationsに移行するの大変で滅
hiramax
0
280
ITエンジニアを取り巻く環境とキャリアパス / A career path for Japanese IT engineers
takatama
4
1.7k
Copilot CLI・IDE・Web・スマホで途切れない開発フローを目指して / One Copilot flow - CLI IDE Web Mobile
aeonpeople
1
1.1k
Cloud Run のアップデート 触ってみる&紹介
gre212
0
190
NFLコンペ2026 解法
lycorptech_jp
PRO
0
120
Featured
See All Featured
Ethics towards AI in product and experience design
skipperchong
2
290
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
31
3.2k
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
Navigating Algorithm Shifts & AI Overviews - #SMXNext
aleyda
1
1.2k
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
380
Thoughts on Productivity
jonyablonski
76
5.2k
What does AI have to do with Human Rights?
axbom
PRO
1
2.2k
Claude Code のすすめ
schroneko
67
220k
The AI Search Optimization Roadmap by Aleyda Solis
aleyda
1
5.8k
Put a Button on it: Removing Barriers to Going Fast.
kastner
60
4.3k
How to Ace a Technical Interview
jacobian
281
24k
Abbi's Birthday
coloredviolet
2
7.8k
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日星期日