Kaminari нумерация страниц медленная с PostgreSQL
Я использую Kaminari для разбиения на страницы двух наборов данных на странице в моем приложении. Один набор данных имеет около 620 записей, а другой около 150. Так что не так много по сравнению с некоторыми.
Тем не менее, для каждой части нумерации страниц требуется около 900 мс. Увидеть: 21:46:38 rails.1 | Rendered items/_paginator.erb (952.8ms)
Видимо это связано с COUNT
что делает Каминари. Более подробная информация здесь: https://github.com/amatsuda/kaminari/issues/545
Я посмотрел вокруг, даже здесь, на SO, и нашел похожие вопросы и множество ответов о том, как ускорить COUNT, но ничего не касалось его ускорения в тандеме с Kaminari.
Есть идеи, как это сделать?
Мой вывод терминала для этого запроса:
22:46:31 rails.1 | (1.6ms) SELECT COUNT(*) FROM "coasters" WHERE "coasters"."superseded_at" IS NULL AND (material = 'steel')
22:46:31 rails.1 | (0.7ms) SELECT COUNT(*) FROM "coasters" WHERE "coasters"."superseded_at" IS NULL AND (material = 'wood')
22:46:31 rails.1 | Rendered shared/_breadcrumb_text.html.erb (9.6ms)
22:46:31 rails.1 | Rendered shared/_fact_bar.html.erb (0.3ms)
22:46:32 rails.1 | ParkChain Load (0.7ms) SELECT "park_chains".* FROM "park_chains"
22:46:32 rails.1 | Rendered theme_parks/parks/_filter_links.html.erb (11.5ms)
22:46:32 rails.1 | Rendered theme_parks/coasters/_filter_links.html.erb (3.2ms)
22:46:33 rails.1 | (0.4ms) SELECT COUNT(*) FROM "parks"
22:46:33 rails.1 | Rendered theme_parks/parks/_park_paginator.erb (1029.5ms)
22:46:33 rails.1 | Park Load (1.2ms) SELECT "parks".* FROM "parks" ORDER BY name ASC LIMIT 25 OFFSET 0
22:46:33 rails.1 | Rendered theme_parks/parks/_park.html.erb (7.9ms)
22:46:33 rails.1 | Rendered theme_parks/parks/_park_grid.html.erb (175.6ms)
22:46:34 rails.1 | Rendered theme_parks/parks/_park_paginator.erb (961.1ms)
22:46:36 rails.1 | (1.7ms) SELECT COUNT(DISTINCT "coasters"."id") FROM "coasters" LEFT OUTER JOIN "parks" ON "parks"."id" = "coasters"."park_id" LEFT OUTER JOIN "cycles" ON "cycles"."coaster_id" = "coasters"."id" WHERE "coasters"."superseded_at" IS NULL
22:46:36 rails.1 | Rendered theme_parks/coasters/_coaster_paginator.erb (1104.5ms)
22:46:36 rails.1 | SQL (2.6ms) SELECT DISTINCT "coasters"."id", parks.name AS alias_0, coasters.name AS alias_1 FROM "coasters" LEFT OUTER JOIN "parks" ON "parks"."id" = "coasters"."park_id" LEFT OUTER JOIN "cycles" ON "cycles"."coaster_id" = "coasters"."id" WHERE "coasters"."superseded_at" IS NULL ORDER BY parks.name ASC, coasters.name ASC LIMIT 25 OFFSET 0
22:46:36 rails.1 | SQL (16.8ms) SELECT "coasters"."id" AS t0_r0, "coasters"."name" AS t0_r1, "coasters"."inversions" AS t0_r2, "coasters"."material" AS t0_r3, "coasters"."notes" AS t0_r4, "coasters"."lat" AS t0_r5, "coasters"."lng" AS t0_r6, "coasters"."manufacturer_id" AS t0_r7, "coasters"."park_id" AS t0_r8, "coasters"."created_at" AS t0_r9, "coasters"."updated_at" AS t0_r10, "coasters"."slug" AS t0_r11, "coasters"."covering" AS t0_r12, "coasters"."style" AS t0_r13, "coasters"."model" AS t0_r14, "coasters"."layout" AS t0_r15, "coasters"."order_ridden" AS t0_r16, "coasters"."dates_ridden" AS t0_r17, "coasters"."on_ride_photo" AS t0_r18, "coasters"."powered" AS t0_r19, "coasters"."length" AS t0_r20, "coasters"."height" AS t0_r21, "coasters"."speed" AS t0_r22, "coasters"."coaster_sort" AS t0_r23, "coasters"."is_milestone" AS t0_r24, "coasters"."version_number" AS t0_r25, "coasters"."version_reason" AS t0_r26, "coasters"."version_ridden_after" AS t0_r27, "coasters"."position" AS t0_r28, "coasters"."relocated" AS t0_r29, "coasters"."renamed" AS t0_r30, "coasters"."rethemed" AS t0_r31, "coasters"."steel_10" AS t0_r32, "coasters"."wood_10" AS t0_r33, "coasters"."original_version_id" AS t0_r34, "coasters"."superseded_at" AS t0_r35, "parks"."id" AS t1_r0, "parks"."name" AS t1_r1, "parks"."location_1" AS t1_r2, "parks"."location_2" AS t1_r3, "parks"."location_3" AS t1_r4, "parks"."lat" AS t1_r5, "parks"."lng" AS t1_r6, "parks"."created_at" AS t1_r7, "parks"."updated_at" AS t1_r8, "parks"."location_4" AS t1_r9, "parks"."country" AS t1_r10, "parks"."park_chain_id" AS t1_r11, "parks"."notes" AS t1_r12, "parks"."visit_count" AS t1_r13, "parks"."dates_visited" AS t1_r14, "parks"."slug" AS t1_r15, "parks"."order_visited" AS t1_r16, "parks"."logo" AS t1_r17, "cycles"."id" AS t2_r0, "cycles"."coaster_id" AS t2_r1, "cycles"."notes" AS t2_r2, "cycles"."date" AS t2_r3, "cycles"."created_at" AS t2_r4, "cycles"."updated_at" AS t2_r5 FROM "coasters" LEFT OUTER JOIN "parks" ON "parks"."id" = "coasters"."park_id" LEFT OUTER JOIN "cycles" ON "cycles"."coaster_id" = "coasters"."id" WHERE "coasters"."superseded_at" IS NULL AND "coasters"."id" IN (21926, 22414, 21927, 21939, 21963, 21964, 21962, 21965, 21800, 21790, 21791, 21793, 21794, 21795, 22378, 21845, 21792, 21842, 21941, 22179, 22144, 22145, 22065, 22196, 22188) ORDER BY parks.name ASC, coasters.name ASC
22:46:36 rails.1 | Rendered theme_parks/coasters/_coaster.html.erb (273.5ms)
22:46:36 rails.1 | Rendered theme_parks/coasters/_coaster_grid.html.erb (576.9ms)
22:46:37 rails.1 | Rendered theme_parks/coasters/_coaster_paginator.erb (968.7ms)
22:46:37 rails.1 | Rendered theme_parks/index.html.erb within layouts/application (6285.8ms)
22:46:38 rails.1 | Rendered shared/_banner_slideshow.html.erb (0.1ms)
22:46:38 rails.1 | Rendered shared/_flash_messages.html.erb (0.2ms)
22:46:38 rails.1 | Completed 200 OK in 7547ms (Views: 7216.3ms | ActiveRecord: 33.3ms)
1 ответ
Я сделал некоторый анализ. Я проверил скорость рендеринга на PostgreSQL и MySQL одинакова. После того как я оптимизировал запрос до 300 мс, рендеринг составил 2500-3500 мс. Есть две вещи, которые сделали мой рендеринг намного быстрее.
- Я закомментировал
gem 'bootstrap-kaminari-views'
, рендеринг уменьшился до 1000 мс, но это было ужасно. Если я скопирую файлы erb из этого драгоценного камня в мойapp/views/kaminari
Папка, я также получаю 1000 мс, и это выглядит хорошо. Это был сюрприз. - Я старался
.page(params[:page]).without_count
и рендеринг уменьшился до 150 мс. Это другой тип нумерации страниц, поэтому я должен подумать, подходит ли он для моего приложения.
Кстати, это было быстрее на Rails 4.2, сейчас я обновляюсь до 5.2 и у меня есть эта проблема.