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.7k
第六屆做自己論壇 - Xdite 鄭伊廷
xdite
4
5.8k
Refactoring lesson : from GPA 1.4 to GPA 3.0
xdite
8
1.6k
全棧班畢業贈語
xdite
1
40k
Intro to RedPotion
xdite
0
260
莫拉克颱風災情支援網
xdite
1
370
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
MCPを活用した検索システムの作り方/How to implement search systems with MCP #catalks
quiver
1
500
Tokyo dbt Meetup #13 dbtと連携するBI製品&機能ざっくり紹介
sagara
0
420
はてなの開発20年史と DevOpsの歩み / DevOpsDays Tokyo 2025 Keynote
daiksy
5
1.3k
ウォンテッドリーにおける Platform Engineering
bgpat
0
180
SREの視点で考えるSIEM活用術 〜AWS環境でのセキュリティ強化〜
coconala_engineer
1
240
やさしいMCP入門
minorun365
PRO
146
94k
アジャイル脅威モデリング#1(脅威モデリングナイト#8)
masakane55
3
140
Micro Frontends: Necessity, Implementation, and Challenges
rainerhahnekamp
0
320
入社後SREチームのミッションや課題の整理をした話
morix1500
1
240
クォータ監視、AWS Organizations環境でも楽勝です✌️
iwamot
PRO
1
230
All You Need Is Kusa 〜Slackデータで始めるデータドリブン〜
jonnojun
0
140
試験は暗記より理解 〜効果的な試験勉強とその後への活かし方〜
fukazawashun
0
340
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
5
520
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
16k
How To Stay Up To Date on Web Technology
chriscoyier
790
250k
GraphQLとの向き合い方2022年版
quramy
46
14k
It's Worth the Effort
3n
184
28k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
34
2.9k
Imperfection Machines: The Place of Print at Facebook
scottboms
267
13k
A Modern Web Designer's Workflow
chriscoyier
693
190k
The Invisible Side of Design
smashingmag
299
50k
How to Create Impact in a Changing Tech Landscape [PerfNow 2023]
tammyeverts
52
2.4k
GraphQLの誤解/rethinking-graphql
sonatard
71
10k
Why You Should Never Use an ORM
jnunemaker
PRO
55
9.3k
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日星期日