AFAIK, :include always results in a LEFT OUTER JOIN, which cannot
impact the count and hence should be dropped from the count query
since it slows down the query.
For one LEFT OUTER JOINed object, on PostgreSQL 8.1, I’m getting a 50%
boost after dropping the LEFT OUTER JOIN in the ‘SELECT COUNT’ query
with the same count.
Consider the following case:
table - blogs :id, :title, :body, :user_id, :published_on etc…
table - users :id, :username, :password, etc
Say I do:
Blog.find(:all, :conditions => "published_on > ‘2007-01-01’
", :include => :user)
This would result in a query like (simplified):
SELECT blogs., users. from blogs left outer join users on
blogs.user_id = users.id WHERE blogs.published_on > ‘2007-01-01’
And a count query like
SELECT count(DISTINCT blogs.id) AS count_all from blogs left outer
join users on blogs.user_id = users.id WHERE blogs.published_on >
But since it’s a LEFT OUTER JOIN (:include will always do a left outer
join), The above will always be equal to
SELECT count(DISTINCT blogs.id) from blogs WHERE blogs.published_on >
Basically, IMO, we can drop :include. Dropping the :joins key would
never work cos they may be inner joins.
Or am I missing something???