주어진 시간 척도에 대한 숫자 집합을 반환하는 보고서를 생성해야합니다.많은 SQL 호출로 느린 함수 속도 향상
예를 들어 주어진 달 동안 시청 한 시간을 반환해야합니다.
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 54.09, 54.09, 54.09, 54.09, 54.09, 54.09, 54.09, 54.09, 54.09, 54.09, 0]
문제는이 프리젠 테이션에 30 번 실행해야하고 난 아무데도 10에서해야 할 것입니다 : 여기이 순간에이 같은 모습에
presentation = Presentation.find(1)
presentation.video.stats.minutes_by_date(Date.new(2012,6,1),Date.new(2012,6,30))
def self.by_date(start_date,end_date)
vals = Array.new
(start_date..end_date).map { |date|
yesterdays_stat = daily_watched(date.yesterday)
todays_stat = daily_watched(date)
if todays_stat > 0
if yesterdays_stat > 0
difference = todays_stat - yesterdays_stat
else
difference = todays_stat
end
watched_in_meg = (difference/1024).round(2)
vals.push (watched_in_meg/first.video.meg_per_minute).round(2)
else
vals.push 0
end
}
vals
end
def self.daily_watched(date)
# get the total bytes for the from both flash and mp4
total_bytes = 0
bytes = where(["DATE(generated_date) = ?", date]).group("kbytes")
.order('generated_date')
bytes.each do |byte|
total_bytes += byte.kbytes
end
total_bytes
# Get the bytes of both flash and mp4
end
출력 요구를 실행하는 것입니다 500 건의 프리젠 테이션.
더 효율적인 방법이 있습니까?
이 잘 작동하지만 난 데 문제는 내가 데이터 나는 그렇게 할 것입니다 방법 0으로 반환 된 다른 날 패드에 필요합니까? –