Skip to content

Instantly share code, notes, and snippets.

@mitsuhirookuno
Created February 6, 2020 03:39
Show Gist options
  • Save mitsuhirookuno/d1ede2b4c781813fe8736f697201c6c4 to your computer and use it in GitHub Desktop.
Save mitsuhirookuno/d1ede2b4c781813fe8736f697201c6c4 to your computer and use it in GitHub Desktop.
RDBアクセス方法比較
db = Sequel.mysql2(ApplicationRecord.connection_config)
# media_daily_referrer_campaign_report
Benchmark.bm do |x|
# 1. sequel
# 1-1. idのみ
x.report('sequel(id) ') do
db.fetch('select id from media_daily_referrer_campaign_reports limit 50000'){|r| r[:id] }
end
# 1-2. 全部
x.report('sequel(all)') do
db.fetch('select * from media_daily_referrer_campaign_reports limit 50000'){|r| r[:id] }
end
# 2. find_by_sql + pluck
# 2-1. idのみ
x.report('pluck(id) ') do
MediaDailyReferrerCampaignReport.uncached do
media_daily_referrer_campaign_reports = MediaDailyReferrerCampaignReport.find_by_sql("select id from media_daily_referrer_campaign_reports limit 50000")
.pluck(:id)
media_daily_referrer_campaign_reports.each {|r| r[0] }
end
end
# 2-2. 全部
x.report('pluck(all) ') do
MediaDailyReferrerCampaignReport.uncached do
media_daily_referrer_campaign_reports = MediaDailyReferrerCampaignReport.find_by_sql("select * from media_daily_referrer_campaign_reports limit 50000")
.pluck(*MediaDailyReferrerCampaignReport.new.attributes.keys.map(&:to_sym))
media_daily_referrer_campaign_reports.each {|r| r[0] }
end
end
# 3. ActiveRecord
# 3-1. each
x.report('each ') do
MediaDailyReferrerCampaignReport.uncached do
media_daily_referrer_campaign_reports = MediaDailyReferrerCampaignReport.limit(50_000)
media_daily_referrer_campaign_reports.each {|r| r.id }
end
end
# 3-2. find_each
x.report('find_each ') do
MediaDailyReferrerCampaignReport.uncached do
media_daily_referrer_campaign_reports = MediaDailyReferrerCampaignReport.limit(50_000)
media_daily_referrer_campaign_reports.find_each {|r| r.id }
end
end
end
@mitsuhirookuno
Copy link
Author

スクリーンショット 2020-02-06 12 42 02

@mitsuhirookuno
Copy link
Author

mitsuhirookuno commented Feb 6, 2020

  • sequel早い
    • コネクションプーリング出来ない(mysqlだと)
  • ActiveRecordもpluckだと、まーまー
    • 書きぶりがイケてないように思える
  • ActiveRecordは、モデルにすると激重(Timezoneが特に癌)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment