N+1 Problem: Optimized Counts with Joins and Custom Select

preview_player
Показать описание
We show you how to optimize counts with a join clause and a custom select

Рекомендации по теме
Комментарии
Автор

Really great video explaining the n+1 problem and it's solution.

A tip for using the rails console: Instead of `exit; rails c;` you can just reload the enviroment by executing `reload!`. Saves you a few minutes during the day :)

Keep up the excellent work.

monorkin
Автор

This will work for MySQL, but not for PostgreSQL. "GROUP BY" will allow to select only aggregate functions or fields inside GROUP BY.


AlexeyVasilyevleo
Автор

This series on N+1 is excellent, thank you and please keep them coming!

I have a quick question. I'm trying this code on a similar DB setup. I have categories except they are associated with images through a HABTM. I am also running Postgres on Rails 4.1.2.

When I execute your code I do get the proper join as well as results with the correct counts. The problem I'm running into is that the query only returns categories that have associated images. I'd like to see all of my 11 categories so I can display them in a dropdown but I need the categories with 0 associated images as well as those with more than 0. Is there a way to tweak the custom select() or COUNT() to return categories with a 0 image.id count?

DominicGiglio
Автор

great vid! What are you using to get rid of the element rendering messages in the terminal when reloading pages?

grammakov