# Should this view be optimized?

**URL:** <https://www.ruby-forum.com/t/should-this-view-be-optimized/158513>\
**Category:** Rails\
**Created:** [January 24, 2009, 2:55am UTC](https://www.ruby-forum.com/t/should-this-view-be-optimized/158513 "2009-01-24T02:55:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 24, 2009, 2:55am UTC](https://www.ruby-forum.com/t/should-this-view-be-optimized/158513/1 "2009-01-24T02:55:24Z")

</div>

I have a survey application with he following models:

Surveys has\_many Questions  
Questions has\_many Answers

When a user takes a survey, they call take\_survey and it renders a  
collection of partials \_survey\_questions. These questions are set to  
display:none and revealed using an AJAX call to ‘reveal\_next\_question’  
and page.show. It also calls an extra load for images as I am using  
FlexImage. Are CACHE hits free or am I doing things wrong? I am having  
trouble interpreting the output from the Mongrel terminal:

## take\_survey METHOD

def take\_survey  
@survey = Survey.find(params[:id], :include =\> [:questions =\>  
:answers])  
end

## SURVEY LOAD

Processing SurveysController#take\_survey (for xxx at 2009-01-23  
20:43:40) [GET]  
Parameters: {“action”=\>“take\_survey”}  
Survey Load (0.4ms) SELECT \* FROM “surveys” WHERE (“surveys”.“id” =  
1)  
Question Load (2.3ms) SELECT “questions”.\* FROM “questions” WHERE  
(“questions”.survey\_id = 1) ORDER BY questions.position ASC  
Answer Load (3.7ms) SELECT “answers”.\* FROM “answers” WHERE  
(“answers”.question\_id IN (1,2,3,4,5)) ORDER BY position ASC  
Rendering template within layouts/survey  
Rendering surveys/take\_survey  
Question Load (0.7ms) SELECT \* FROM “questions” WHERE  
(“questions”.“id” = 1)  
CACHE (0.0ms) SELECT \* FROM “questions” WHERE (“questions”.“id” = 1)  
CACHE (0.0ms) SELECT \* FROM “questions” WHERE (“questions”.“id” = 1)  
CACHE (0.0ms) SELECT \* FROM “questions” WHERE (“questions”.“id” = 1)  
CACHE (0.0ms) SELECT \* FROM “questions” WHERE (“questions”.“id” = 1)  
Rendered questions/\_survey\_question (8.1ms)  
Rendered questions/\_survey\_question (2.1ms)  
Rendered questions/\_survey\_question (2.4ms)  
Question Load (0.6ms) SELECT \* FROM “questions” WHERE  
(“questions”.“id” = 4)  
CACHE (0.0ms) SELECT \* FROM “questions” WHERE (“questions”.“id” = 4)  
CACHE (0.0ms) SELECT \* FROM “questions” WHERE (“questions”.“id” = 4)  
Rendered questions/\_survey\_question (4.2ms)  
Rendered questions/\_survey\_question (1.3ms)  
Completed in 150ms (View: 28, DB: 8) | 200 OK [[http://xxx/take\_survey/1](http://xxx/take_survey/1)]

Processing QuestionsController#show to png (for xxx at 2009-01-23  
20:43:43) [GET]  
Parameters: {“format”=\>“png”, “action”=\>“show”, “id”=\>“1”,  
“controller”=\>“questions”}  
Question Load (0.6ms) SELECT \* FROM “questions” WHERE  
(“questions”.“id” = 1)  
Rendering questions/show  
Completed in 149ms (View: 135, DB: 1) | 200 OK  
[[http://xxx/questions/1.png](http://xxx/questions/1.png)]

## REVEAL NEXT QUESTION

Processing SurveysController#reveal\_next\_question (for xxx at 2009-01-23  
20:50:27) [POST]  
Parameters: {“action”=\>“reveal\_next\_question”, “next\_question”=\>“2”}  
Question Load (0.5ms) SELECT \* FROM “questions” WHERE  
(“questions”.“id” = 2)  
Answer Load (1.3ms) SELECT “answers”.\* FROM “answers” WHERE  
(“answers”.question\_id = 2) ORDER BY position ASC  
Survey Load (0.8ms) SELECT \* FROM “surveys” WHERE (“surveys”.“id” =  
1)  
Completed in 39ms (View: 13, DB: 3) | 200 OK  
[[http://xxx/surveys/reveal\_next\_question](http://xxx/surveys/reveal_next_question)]

---

<div class="post-metadata">

**Author:** ![taylorstrait](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b5a626/32.png) [@taylorstrait](https://www.ruby-forum.com/u/taylorstrait)\
**Post date:** [January 24, 2009, 1:06pm UTC](https://www.ruby-forum.com/t/should-this-view-be-optimized/158513/2 "2009-01-24T13:06:23Z")

</div>

Taylor S. wrote:

> When a user takes a survey, they call take\_survey and it renders a  
> collection of partials \_survey\_questions. These questions are set to  
> display:none and revealed using an AJAX call to ‘reveal\_next\_question’  
> and page.show. It also calls an extra load for images as I am using  
> FlexImage. Are CACHE hits free or am I doing things wrong? I am having  
> trouble interpreting the output from the Mongrel terminal:

It can probably be optimized, but if you’re not experiencing any  
performance issues then optimization may be premature and of little  
value.

Cache hits aren’t as expensive as executing the actual query, but they  
aren’t exactly free either. It should probably be possible to bring the  
number of queries down.

–  
Roderick van Domburg  
[http://www.nedforce.com](http://www.nedforce.com)
