Skip to content
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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

iris-the-fox
Copy link

No description provided.

Copy link
Collaborator

@spajic spajic left a 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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

лайк за отдельный класс

<% end %>
</ul>
<%= render "delimiter" %>
====================================================
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Переписала TripsImporter с использованием Activerecord-Import, время загрузки изменилось
для файла `small.json` данные загружаются за 1.69s
для файла `medium.json` данные загружаются за 4.1s
для файла `large.json` данные загружаются за 13.8s
Copy link
Collaborator

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`
Copy link
Collaborator

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
Copy link
Collaborator

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 секунд уже приемлемое время, а что еще оптимизировать я с имеющимися инструментами не обнаружила.
Copy link
Collaborator

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%
Copy link
Collaborator

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 лайк за тест

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants