Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to avoid N+1 queries in CamaleonCms? #556

Closed
marat-chardymov opened this issue Jan 10, 2017 · 5 comments
Closed

How to avoid N+1 queries in CamaleonCms? #556

marat-chardymov opened this issue Jan 10, 2017 · 5 comments

Comments

@marat-chardymov
Copy link

I'm using bullet to detect N+1 queries. And it complains about custom_fields:
N+1 Query detected CamaleonCms::CustomFieldsRelationship => [:custom_fields] Add to your finder: :includes => [:custom_fields]
Here is my view with query code:

<% @post.get_fields_object.dig(:gallery, :values).each_with_index do |image, index| %>
  <div class="item <%= 'active' if index == 0%>">
    <img src="<%= image %>" />
  </div>
<% end %>
@marat-chardymov
Copy link
Author

Come up with monkey patching, but shouldn't be a better way of doing this?

@owen2345
Copy link
Owner

Solved! please test using github version

@brian-kephart
Copy link
Collaborator

I'm getting N+1 query warnings as well, due to a couple of issues.

The first issue is the draw_menu method, which in turn calls cama_draw_menu_items, which calls cama_menu_parse_items. It appears to me that these last two methods may be executing queries within a loop, leading to an N+1 issue.

I also have an issue with a list of links to related posts. When I loop over a group of posts to display a link for each, I get an N+1 warning. I think this is the result of calling the_url within the loop, since this method appears to check URL display options on each call. Is there a method for getting a group of post URLs with a single query?

My code:

<% posts = current_site.the_posts(@post.post_type.slug).includes(:post_type).latest.limit(7).decorate %>
                <% posts.each do |p| %>
                    <li><a href="<%= p.the_url %>" data-turbolinks="false"><%= p.the_title %></a></li>
                    <% unless p == posts.last %><br><% end %>

@owen2345
Copy link
Owner

@briank-musichouse
1: it is rendering visible menus recursively (depends of qty of menu items), maybe you can cache and use javascript to mark the current menu. (Anyway I will review and improve for the next version)
2: In the first line you can add

mylist.limit(7).eager_load(:my_model).decorate

Regards,

@brian-kephart
Copy link
Collaborator

Thanks, I'll give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants