-
Notifications
You must be signed in to change notification settings - Fork 115
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
Optimization task3 #123
base: master
Are you sure you want to change the base?
Optimization task3 #123
Conversation
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.
✅ спасибо за работу!
@@ -0,0 +1,53 @@ | |||
# frozen_string_literal: true | |||
|
|||
class TripsImporter |
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.
лайк за отдельный класс
<% end %> | ||
</ul> | ||
<%= render "delimiter" %> | ||
==================================================== |
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.
Just for your info: https://guides.rubyonrails.org/layouts_and_rendering.html#spacer-templates
Переписала TripsImporter с использованием Activerecord-Import, время загрузки изменилось | ||
для файла `small.json` данные загружаются за 1.69s | ||
для файла `medium.json` данные загружаются за 4.1s | ||
для файла `large.json` данные загружаются за 13.8s |
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.
👍
22% времени тратится на запрос `SELECT "trips".* FROM "trips" WHERE "trips"."from_id" = $1 AND "trips"."to_id" = $2 ORDER BY "trips"."start_time" ASC` | ||
Предлагается | ||
`CREATE INDEX CONCURRENTLY ON trips (from_id, to_id)` | ||
И 18% на `SELECT COUNT(*) FROM "trips" WHERE "trips"."from_id" = $1 AND "trips"."to_id" = $2` |
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.
count запрос вообще не нужен, так как мы грузим данные и можем просто взять size
И 18% на `SELECT COUNT(*) FROM "trips" WHERE "trips"."from_id" = $1 AND "trips"."to_id" = $2` | ||
Предлагается то же самое. | ||
Добавляю индексы. | ||
После добавления индексов процент снизился до 17% и 14% соответственно, скорость загрузки сократилась до 5837ms |
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.
скорость загрузки -> время загрузки (сорри за душноту 😁)
rack-mini-profiler показывает что запросы на buses, buses_services и services выполняются отдельно. | ||
Предполагаю что надо заменить в контроллере includes на eager_load, чтобы избавиться от этого. Теперь вместо 7 запросов - 4 и страница загружается за 1439ms | ||
|
||
Менее 1,5 секунд уже приемлемое время, а что еще оптимизировать я с имеющимися инструментами не обнаружила. |
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.
👍 ✅
После добавления индексов процент снизился до 17% и 14% соответственно, скорость загрузки сократилась до 5837ms | ||
|
||
Так же pghero предлагает добавить индекс `CREATE INDEX CONCURRENTLY ON buses_services (bus_id)` | ||
Хотя запрос `SELECT "buses_services".* FROM "buses_services" WHERE ....` занимает 3% |
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.
да, на время рендеренга нашей страницы это почти не повлияет; но если мы занимались оптимизацией именно нагрузки на БД, то там бы это имело смысл
expect(bus.services.map(&:name)).to match_array(['WiFi', 'Туалет']) | ||
end | ||
|
||
it 'creates trips with correct attributes' do |
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.
👍 лайк за тест
No description provided.