2017-04-19 93 views
0

我正在研究導軌加速加載時間的技巧。我現在正在處理一個頁面,在渲染時渲染大量數據。我試圖逐步通過每個負載,看看我是否可以優化時間。預加載數據以加快視圖加載時間 - 導軌

我現在的問題是這樣的;

(5.4ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23022]] 
    (4.4ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23023]] 
    (4.2ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23024]] 
    (6.0ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23029]] 
    (7.1ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23121]] 
    (6.0ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23180]] 
    (4.2ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23229]] 
    (4.2ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23250]] 
    (6.6ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23254]] 
    (4.4ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23255]] 
    (4.0ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23259]] 
    (6.5ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23154]] 
    (6.6ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23177]] 
    (5.1ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23201]] 
    (4.2ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23221]] 
    (4.1ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23232]] 
    (7.9ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23235]] 
    (4.3ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23237]] 
    (4.1ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23238]] 
    (5.9ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23248]] 
    (7.8ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23295]] 
    (6.8ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23119]] 
    (4.2ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23123]] 
    (4.1ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23178]] 
    (7.9ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23231]] 
    (4.4ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23239]] 
    (4.3ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23240]] 
    (6.4ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23241]] 
    (5.1ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23243]] 
    (6.7ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23245]] 
    (4.3ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23247]] 
    (4.2ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23249]] 
    (6.9ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23251]] 
    (4.4ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23256]] 
    (4.2ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23257]] 
    (6.5ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23275]] 
    (6.4ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23350]] 
    (5.0ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23272]] 
    (4.2ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23296]] 
    (4.0ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23401]] 
    (8.8ms) SELECT COUNT(*) FROM "attendees" WHERE "attendees"."rsvp_id" = $1 [["rsvp_id", 23347]] 

我越來越從這個代碼:

def number_attending 
    rsvp.attendees.count 
end 

所以回函有很多人蔘加。我在這裏調用這個方法。

<% rsvps.each do |rsvp| %> 
<span class="event-response__indicator event-response__indicator--guests" title="<%= rsvp.number_attending %> Guests"> 
<% end %> 

所以問題是。我想不必在頁面加載時遍歷每個rsvp和參加者。有沒有預加載數據的方法?像Eagerloading?如果是這樣,我怎麼能用給出的信息來實現它?

回答

0

檢出this link, 這是專門用於避免計數的n + 1查詢。

此解釋執行加入查詢或創建相同的視圖

-1

那麼,你沒有顯示你如何查詢rsvps,所以我會認爲它是rsvps = Rsvp.all。您可以添加includes方法,所以它會像這樣rsvps = Rsvp.includes(:attendees).all

此外,我不確定是否可以避免查詢時運行此行rsvp.attendees.count。我遇到了一些情況,其中count方法觸發了一個查詢,但size方法沒有,所以你可能不得不將這條線替換爲rsvp.attendees.size

+0

我可以知道爲什麼downvote? –