-
Notifications
You must be signed in to change notification settings - Fork 12
Optimize json import #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
pustserg
wants to merge
1
commit into
spajic:master
Choose a base branch
from
pustserg:optimize
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| 2.6.1 | ||
| 2.6.2 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,23 @@ | ||
| source 'https://rubygems.org' | ||
| git_source(:github) { |repo| "https://github.com/#{repo}.git" } | ||
|
|
||
| ruby '2.6.1' | ||
| ruby '2.6.2' | ||
|
|
||
| gem 'rails', '~> 5.2.3' | ||
| gem 'pg', '>= 0.18', '< 2.0' | ||
| gem 'puma', '~> 3.11' | ||
| gem 'bootsnap', '>= 1.1.0', require: false | ||
| gem 'oj' | ||
| gem 'bulk_insert' | ||
| gem 'pry' | ||
|
|
||
| group :development, :test do | ||
| # Call 'byebug' anywhere in the code to stop execution and get a debugger console | ||
| gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] | ||
| gem 'benchmark-ips' | ||
| gem 'ruby-prof' | ||
| gem 'pghero' | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| gem 'pg_query', '>= 0.9.0' | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| end | ||
|
|
||
| group :development do | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| class BusService < ApplicationRecord | ||
| self.table_name = 'buses_services' | ||
|
|
||
| belongs_to :bus | ||
| belongs_to :service | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| #!/usr/bin/env ruby | ||
| # | ||
| require 'benchmark' | ||
| # require 'fileutils' | ||
|
|
||
| FILES = %w( | ||
| small.json | ||
| medium.json | ||
| large.json | ||
| ).freeze | ||
|
|
||
| APP_ROOT = File.expand_path('..', __dir__) | ||
|
|
||
| # FileUtils.chdir APP_ROOT do | ||
| FILES.each do |fname| | ||
| result = Benchmark.measure do | ||
| puts "----------Load data from #{fname}----------" | ||
| `rake reload_json[#{APP_ROOT}/fixtures/#{fname}]` | ||
| end | ||
| puts result | ||
| end | ||
| # end | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| При изучении utils.rake первым делом в глаза бросается обилие find_or_create_by | ||
|
|
||
| Решил посмотреть с помощью Benchmark.ips как они выполняются последовательно | ||
| ``` | ||
| ActiveRecord::Base.transaction do | ||
| City.delete_all | ||
| Bus.delete_all | ||
| Service.delete_all | ||
| Trip.delete_all | ||
| ActiveRecord::Base.connection.execute('delete from buses_services;') | ||
|
|
||
| Benchmark.ips do |x| | ||
| x.report('find_or_create_by City') do | ||
| json.each { |trip| City.find_or_create_by(name: trip['from']) } | ||
| end | ||
|
|
||
| x.report('Find find_or_create_by services') do | ||
| json.each do |trip| | ||
| trip['bus']['services'].each do |service| | ||
| Service.find_or_create_by(name: service) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| x.report('find_or_create_by Bus') do | ||
| json.each { |trip| Bus.find_or_create_by(number: trip['bus']['number']) } | ||
| end | ||
| end | ||
| end | ||
| ``` | ||
|
|
||
| Получается так | ||
| ``` | ||
| Warming up -------------------------------------- | ||
| find_or_create_by City | ||
| 1.000 i/100ms | ||
| Find find_or_create_by services | ||
| 1.000 i/100ms | ||
| find_or_create_by Bus | ||
| 1.000 i/100ms | ||
| Calculating ------------------------------------- | ||
| find_or_create_by City | ||
| 2.134 (± 0.0%) i/s - 11.000 in 5.167181s | ||
| Find find_or_create_by services | ||
| 0.491 (± 0.0%) i/s - 3.000 in 6.109831s | ||
| find_or_create_by Bus | ||
| 0.497 (± 0.0%) i/s - 3.000 in 6.090513s | ||
| ``` | ||
|
|
||
| при этом общее время импорта small.json | ||
| 11.723580999998376 | ||
|
|
||
| Окей, пробуем добавить индексы | ||
| Показатель времени стал хуже | ||
| 13.188413999974728 | ||
|
|
||
| Показатели бенчмарка не "взлетели в небеса" | ||
| ``` | ||
| Warming up -------------------------------------- | ||
| find_or_create_by City | ||
| 1.000 i/100ms | ||
| Find find_or_create_by services | ||
| 1.000 i/100ms | ||
| find_or_create_by Bus | ||
| 1.000 i/100ms | ||
| Calculating ------------------------------------- | ||
| find_or_create_by City | ||
| 2.102 (± 0.0%) i/s - 11.000 in 5.260946s | ||
| Find find_or_create_by services | ||
| 0.510 (± 0.0%) i/s - 3.000 in 5.885058s | ||
| find_or_create_by Bus | ||
| 0.441 (± 0.0%) i/s - 3.000 in 6.919219s | ||
| ``` | ||
|
|
||
| Окей, индексы не выход (для импорта данных так уж точно) | ||
|
|
||
| Другой день, импорт small.json занимает 18-20 секунд | ||
| пробую испольщовать gem `oj` | ||
|
|
||
| 13.56506800011266 s | ||
|
|
||
| хмм, после серии тестов среднее время 13-15 секунд. Неплохо, оставляем | ||
|
|
||
| Замена AR методов `delete all` на raw sql дает еще чуть выигрыш в пару секунд | ||
|
|
||
| Окей, ставим и настраиваем pg_hero | ||
|
|
||
| 10 000 элементов в small.json генерируют 4,229 запросов `SELECT FROM services` и всего 10 `INSERT INTO services` | ||
|
|
||
| Время работы сейчас важнее потребляемой памяти, попробую не делать на каждую строку find_or_create, а сделать массив и вставить c помощью bulk_insert | ||
|
|
||
| Стало намного лучше | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
| ``` | ||
| ----------Load data from small.json---------- | ||
| 0.000284 0.001181 1.671884 ( 2.216141) | ||
| ----------Load data from medium.json---------- | ||
| 0.000137 0.000838 2.695500 ( 2.925685) | ||
| ----------Load data from large.json---------- | ||
| 0.000154 0.000889 12.920520 ( 14.627362) | ||
| ``` | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,8 @@ | ||
| Rails.application.routes.draw do | ||
| # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | ||
|
|
||
| mount PgHero::Engine, at: "pghero" | ||
|
|
||
| get "/" => "statistics#index" | ||
| get "автобусы/:from/:to" => "trips#index" | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| class CreatePgheroSpaceStats < ActiveRecord::Migration[5.2] | ||
| def change | ||
| create_table :pghero_space_stats do |t| | ||
| t.text :database | ||
| t.text :schema | ||
| t.text :relation | ||
| t.integer :size, limit: 8 | ||
| t.timestamp :captured_at | ||
| end | ||
|
|
||
| add_index :pghero_space_stats, [:database, :captured_at] | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,34 +1,88 @@ | ||
| # Наивная загрузка данных из json-файла в БД | ||
| # rake reload_json[fixtures/small.json] | ||
|
|
||
| task :reload_json, [:file_name] => :environment do |_task, args| | ||
| json = JSON.parse(File.read(args.file_name)) | ||
|
|
||
| ActiveRecord::Base.transaction do | ||
| City.delete_all | ||
| Bus.delete_all | ||
| Service.delete_all | ||
| Trip.delete_all | ||
| ActiveRecord::Base.connection.execute('delete from buses_services;') | ||
|
|
||
| ActiveRecord::Base.connection.execute <<-SQL | ||
| delete from cities; | ||
| delete from buses; | ||
| delete from services; | ||
| delete from trips; | ||
| delete from buses_services; | ||
| SQL | ||
|
|
||
| cities = Set.new | ||
| services = Set.new | ||
| buses = Set.new | ||
| buses_services = Set.new | ||
| trips = Set.new | ||
|
|
||
| json.each do |trip| | ||
| from = City.find_or_create_by(name: trip['from']) | ||
| to = City.find_or_create_by(name: trip['to']) | ||
| services = [] | ||
| trip['bus']['services'].each do |service| | ||
| s = Service.find_or_create_by(name: service) | ||
| services << s | ||
| cities << { name: trip['from'] } | ||
| cities << { name: trip['to'] } | ||
| buses << { number: trip['bus']['number'], model: trip['bus']['model'] } | ||
| trip['bus']['services'].each do |service_name| | ||
| services << { name: service_name } | ||
| buses_services << { bus_number: trip['bus']['number'], service_name: service_name } | ||
| end | ||
| bus = Bus.find_or_create_by(number: trip['bus']['number']) | ||
| bus.update(model: trip['bus']['model'], services: services) | ||
|
|
||
| Trip.create!( | ||
| from: from, | ||
| to: to, | ||
| bus: bus, | ||
| trips << { | ||
| from_name: trip['from'], | ||
| to_name: trip['to'], | ||
| bus_number: trip['bus']['number'], | ||
| start_time: trip['start_time'], | ||
| duration_minutes: trip['duration_minutes'], | ||
| price_cents: trip['price_cents'], | ||
| ) | ||
| price_cents: trip['price_cents'] | ||
| } | ||
| end | ||
|
|
||
| City.bulk_insert do |worker| | ||
| cities.each do |city_attrs| | ||
| worker.add(city_attrs) | ||
| end | ||
| end | ||
|
|
||
| Service.bulk_insert do |worker| | ||
| services.each do |service_attrs| | ||
| worker.add(service_attrs) | ||
| end | ||
| end | ||
|
|
||
| Bus.bulk_insert do |worker| | ||
| buses.each do |bus_attrs| | ||
| worker.add(bus_attrs) | ||
| end | ||
| end | ||
|
|
||
| cities_objects = City.pluck(:name, :id).to_h | ||
| services_objects = Service.all.index_by(&:name) | ||
| buses_objects = Bus.all.index_by(&:number) | ||
|
|
||
| BusService.bulk_insert do |worker| | ||
| buses_services.each do |bs| | ||
| bus_id = buses_objects[bs[:bus_number]].id | ||
| service_id = services_objects[bs[:service_name]].id | ||
| worker.add(bus_id: bus_id, service_id: service_id) | ||
| end | ||
| end | ||
|
|
||
| Trip.bulk_insert do |worker| | ||
| trips.each do |trip| | ||
| from_id = cities_objects[trip[:from_name]] | ||
| to_id = cities_objects[trip[:to_name]] | ||
| bus_id = buses_objects[trip[:bus_number]].id | ||
|
|
||
| worker.add( | ||
| from_id: from_id, | ||
| to_id: to_id, | ||
| bus_id: bus_id, | ||
| start_time: trip[:start_time], | ||
| duration_minutes: trip[:duration_minutes], | ||
| price_cents: trip[:duration_minutes] | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍