-
Notifications
You must be signed in to change notification settings - Fork 115
Оптимизация БД Сергеенков М.С. #19
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--require spec_helper |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2.6.3 | ||
2.6.5 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,8 +13,13 @@ class Bus < ApplicationRecord | |
].freeze | ||
|
||
has_many :trips | ||
has_and_belongs_to_many :services, join_table: :buses_services | ||
has_many :buses_services | ||
has_many :services, through: :buses_services | ||
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. 👍 |
||
|
||
validates :number, presence: true, uniqueness: true | ||
validates :model, inclusion: { in: MODELS } | ||
|
||
def services_names | ||
services.pluck(:name) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class BusesService < ApplicationRecord | ||
belongs_to :bus | ||
belongs_to :service | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
class ReloadJson | ||
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. Плюс за вынос логики в отдельный файл, но не очень понятное название. 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. Ага, поспешил( |
||
class << self | ||
def call(file_name) | ||
new.call(file_name) | ||
end | ||
end | ||
|
||
attr_reader :cities, :services, :buses, :trips | ||
|
||
def initialize | ||
@cities = {} | ||
@services = {} | ||
@buses = {} | ||
@trips = [] | ||
end | ||
|
||
def call(file_name) | ||
json = Oj.load(File.read(file_name)) | ||
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. Так грузим сразу весь json в память, при большом файле потратим много оперативки, раздуем память процесса. 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. Точно... |
||
|
||
ActiveRecord::Base.transaction do | ||
Trip.delete_all | ||
City.delete_all | ||
BusesService.delete_all | ||
Bus.delete_all | ||
Service.delete_all | ||
|
||
json.each do |trip| | ||
from = find_or_create_city(trip['from']) | ||
to = find_or_create_city(trip['to']) | ||
|
||
bus_services = [] | ||
|
||
trip['bus']['services'].each do |service| | ||
bus_services << find_or_create_service(service) | ||
end | ||
|
||
bus = find_or_create_bus( | ||
trip['bus']['number'], trip['bus']['model'], bus_services | ||
) | ||
|
||
trips << { | ||
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['price_cents'], | ||
} | ||
end | ||
|
||
Trip.import trips, batch_size: 1000 | ||
end | ||
end | ||
|
||
private | ||
|
||
def find_or_create_city(name) | ||
return cities[name] if cities.key?(name) | ||
|
||
cities[name] = City.create(name: name) | ||
end | ||
|
||
def find_or_create_service(name) | ||
return services[name] if services.key?(name) | ||
|
||
services[name] = Service.create(name: name) | ||
end | ||
|
||
def find_or_create_bus(number, model, services) | ||
return buses[number] if buses.key?(number) | ||
|
||
buses[number] = Bus.create( | ||
number: number, | ||
model: model, | ||
services: services | ||
) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,14 +2,14 @@ | |
<%= "Автобусы #{@from.name} – #{@to.name}" %> | ||
</h1> | ||
<h2> | ||
<%= "В расписании #{@trips.count} рейсов" %> | ||
<%= "В расписании #{@trips.size} рейсов" %> | ||
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. 👍 |
||
</h2> | ||
|
||
<% @trips.each do |trip| %> | ||
<ul> | ||
<%= render "trip", trip: trip %> | ||
<% if trip.bus.services.present? %> | ||
<%= render "services", services: trip.bus.services %> | ||
<%= render trip %> | ||
<% trip.bus.services_names.each do |name| %> | ||
<li><%= "#{name}" %></li> | ||
<% end %> | ||
</ul> | ||
<%= render "delimiter" %> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,6 @@ | |
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html | ||
get "/" => "statistics#index" | ||
get "автобусы/:from/:to" => "trips#index" | ||
|
||
mount PgHero::Engine, at: "pghero" | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class CreatePgheroQueryStats < ActiveRecord::Migration[5.2] | ||
def change | ||
create_table :pghero_query_stats do |t| | ||
t.text :database | ||
t.text :user | ||
t.text :query | ||
t.integer :query_hash, limit: 8 | ||
t.float :total_time | ||
t.integer :calls, limit: 8 | ||
t.timestamp :captured_at | ||
end | ||
|
||
add_index :pghero_query_stats, [:database, :captured_at] | ||
end | ||
end |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddUniqIndexToCityName < ActiveRecord::Migration[5.2] | ||
def change | ||
add_index :cities, :name, unique: true | ||
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. В данном случае не важно, но напоминаю про опцию 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 | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddUniqIndexToBusNumber < ActiveRecord::Migration[5.2] | ||
def change | ||
add_index :buses, :number, unique: true | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddUniqIndexToServiceName < ActiveRecord::Migration[5.2] | ||
def change | ||
add_index :services, :name, unique: true | ||
end | ||
end |
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.
👍