我正在使用Kaminari在我的应用中对页面上的两组数据进行分页。一组数据有大约620条记录,另一组大约有150条。所以与一些数据相比并不多。
然而,分页部分需要大约900毫秒来加载每个。请参阅:21:46:38 rails.1 | Rendered items/_paginator.erb (952.8ms)
显然这是由于Kaminari正在做的COUNT
。更多详情:https://github.com/amatsuda/kaminari/issues/545
我已经环顾四周,即使在这里,我也发现了类似的问题和大量关于如何加速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)
答案 0 :(得分:0)
我做了一些分析。我已经测试了PostgreSQL和MySQL的渲染速度是否相同。在将查询优化为300ms之后,渲染时间为2500-3500ms。有两点使我的渲染速度更快。
gem 'bootstrap-kaminari-views'
,渲染时间减少到1000毫秒,但是很丑陋。如果我将erb文件从该gem复制到我的app/views/kaminari
文件夹中,我也会得到1000毫秒的响应,看起来不错。真令人惊讶。.page(params[:page]).without_count
,渲染时间减少到了150ms。分页是另一种类型,因此我必须考虑它是否适合我的应用程序。顺便说一句,在Rails 4.2上速度更快,现在我要升级到5.2并有此问题。