我有兩個活動記錄StudentDemographics
和StudentWeeklyReport
均具有has_many
關係是這樣的:優化下面的代碼
class StudentDemographics < ActiveRecord::Base
has_many :student_weekly_reports, :foreign_key => :student_id
end
我要查馬克在最後的第五週研討會,最新一期每一個學生。如果結果是true
,學生應該是積極的,否則不活躍。我有以下代碼。在這裏,我正在重複每個日期的循環。 @distinct
是一組日期。
for i in [email protected]
active = 0
inactive = 0
sum = safe.length
@students = StudentDemographics.where("date <= ?", @distinct[i]).select("student_id") - safe
@students.each do |student|
@stu = StudentWeeklyReport.where(:student_id => student.student_id).select("student_id,golden_eggs").last(5)
if @stu.length > 4
if @stu[4].golden_eggs > @stu[0].golden_eggs
safe << student
active += 1
else
inactive += 1
end
else
safe << student
active += 1
end
end
@active[i] = active + sum
@inactive[i] = inactive
end
表現不好。它需要超過3秒的時間。我的mysql db在StudentWeeklyReports
表中有13600個,在StudentDemographics
表中有2000個。任何人都可以建議如何優化下面的代碼?
感謝您的幫助@Thaha KP。你的回答看起來不錯,但響應時間從3秒增加到12秒。它在控制檯日誌中打印一些說明命令。 – user2906085
@ Thaha的解決方案看起來很好,與你發佈的循環相比,它不會變慢。 Thaha的解決方案肯定會更快。嘗試通過重新啓動服務器來運行這兩個循環,以查看兩者之間的實際差異。您的循環可能需要3秒,因爲ActiveRecord查詢緩存正在由內部執行。 –
此外用&和'替換'&&也會有所幫助,因爲&&會檢查兩個條件,而'和'它不會檢查第二個條件,如果第一個條件爲false。 –