Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bulk InsertをRailsで検証してみた

H Hasegawa
February 15, 2020

Bulk InsertをRailsで検証してみた

H Hasegawa

February 15, 2020
Tweet

Other Decks in Programming

Transcript

  1. INSERT文とは?(よく使うやつ) データをデータベースに挿入するためのクエリ Railsでレコードを作るとよくみるやつ INSERT INTO "users" ("name", "email", "created_at", "updated_at")

    VALUES ($1, $2, $3, $4) RETURNING "id" [["name", "fjord"], ["email", "[email protected]"], ["created_at", "2020-02-14 13:03:21.219745"], ["updated_at", "2020-02-14 13:03:21.219745"]]
  2. Bulk Insertとは? SQL1回の発行で複数のレコードを挿入することができる機能。 INSERT INTO "users"("name","email","created_at","updated_at") VALUES ('hoge', '[email protected]', '2020-02-14

    09:26:57.549471', '2020-02-14 09:26:57.549472'), ('fuga', '[email protected]', '2020-02-14 09:26:57.549473', '2020-02-14 09:26:57.549474') ※データベースによっては BulkInsert用の書き方がある(sqlserverなど)
  3. 検証環境 環境 - OS: macOS Mojave 10.14.6 - CPU: 2.7

    GHz i7 - memory 16GB - ruby: 2.7.0 - rails 6.0.2.1 - db: postgresql seed.rbにコードを書いて時間を計測
  4. 検証① Createで100万件入れるのにかかる時間を測定 require 'benchmark' Benchmark.bm 15 do |r| r.report "Create

    users" do 1_000_000.times do |i| User.create( name: "Fjord No.#{i}", email: "hogehoge#{i}@example.com" ) end end
  5. 検証その② 生SQLで100万件入れるのにかかる時間を測定 Benchmark.bm 15 do |r| r.report "Create users" do

    con = ActiveRecord::Base.connection 1_000_000.times do |i| time = Time.now con.execute("INSERT INTO users (name, email, created_at, updated_at) VALUES ('Fjord#{i}', 'hogehoge#{i}@example.com', '#{time}', '#{time}')") end end end
  6. 検証その③ Bulk Insertで100万件入れるのにかかる時間を測定 を Benchmark.bm 15 do |r| r.report "Create

    users" do users = [] 1_000_000.times do |i| now = Time.now users << { name: "FJORD#{i}", email: "hogehoge#{i}@example.com", created_at: now, updated_at: now } end User.insert_all(users) end end
  7. records/insert by create by raw SQL Bulk Insert 1,000 2.078143

    0.433111 0.047257 10,000 21.482302 4.251136 0.384161 100,000 227.163341(≒4 min) 40.733448 3.569448 1,000,000 2135.108541(≒32 min) 424.461862(≒7min) 45.788185 処理時間まとめ ・単位は秒 ・Benchmarkのtotalで比較