Permalink
Browse files

Making ActiveRecord faster [skaes]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4069 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
dhh committed Mar 27, 2006
1 parent 1cc2f5c commit c9c185200df71c8aaa5ed42e39531191e76690e4
@@ -1075,6 +1075,7 @@ def select_all_rows(options, join_dependency)
end
def construct_counter_sql_with_included_associations(options, join_dependency)
+ scope = scope(:find)
sql = "SELECT COUNT(DISTINCT #{table_name}.#{primary_key})"
# A (slower) workaround if we're using a backend, like sqlite, that doesn't support COUNT DISTINCT.
@@ -1085,11 +1086,11 @@ def construct_counter_sql_with_included_associations(options, join_dependency)
sql << " FROM #{table_name} "
sql << join_dependency.join_associations.collect{|join| join.association_join }.join
- add_joins!(sql, options)
- add_conditions!(sql, options[:conditions])
- add_limited_ids_condition!(sql, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && (scope(:find, :limit) || options[:limit])
+ add_joins!(sql, options, scope)
+ add_conditions!(sql, options[:conditions], scope)
+ add_limited_ids_condition!(sql, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && ((scope && scope[:limit]) || options[:limit])
- add_limit!(sql, options) if using_limitable_reflections?(join_dependency.reflections)
+ add_limit!(sql, options, scope) if using_limitable_reflections?(join_dependency.reflections)
if !Base.connection.supports_count_distinct?
sql << ")"
@@ -1099,16 +1100,17 @@ def construct_counter_sql_with_included_associations(options, join_dependency)
end
def construct_finder_sql_with_included_associations(options, join_dependency)
- sql = "SELECT #{column_aliases(join_dependency)} FROM #{scope(:find, :from) || options[:from] || table_name} "
+ scope = scope(:find)
+ sql = "SELECT #{column_aliases(join_dependency)} FROM #{(scope && scope[:from]) || options[:from] || table_name} "
sql << join_dependency.join_associations.collect{|join| join.association_join }.join
- add_joins!(sql, options)
- add_conditions!(sql, options[:conditions])
+ add_joins!(sql, options, scope)
+ add_conditions!(sql, options[:conditions], scope)
add_limited_ids_condition!(sql, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && options[:limit]
sql << "ORDER BY #{options[:order]} " if options[:order]
- add_limit!(sql, options) if using_limitable_reflections?(join_dependency.reflections)
+ add_limit!(sql, options, scope) if using_limitable_reflections?(join_dependency.reflections)
return sanitize_sql(sql)
end
@@ -1127,19 +1129,20 @@ def select_limited_ids_list(options, join_dependency)
end
def construct_finder_sql_for_association_limiting(options, join_dependency)
+ scope = scope(:find)
#sql = "SELECT DISTINCT #{table_name}.#{primary_key} FROM #{table_name} "
sql = "SELECT "
sql << "DISTINCT #{table_name}." if include_eager_conditions?(options) || include_eager_order?(options)
sql << "#{primary_key} FROM #{table_name} "
if include_eager_conditions?(options) || include_eager_order?(options)
sql << join_dependency.join_associations.collect{|join| join.association_join }.join
- add_joins!(sql, options)
+ add_joins!(sql, options, scope)
end
- add_conditions!(sql, options[:conditions])
+ add_conditions!(sql, options[:conditions], scope)
sql << "ORDER BY #{options[:order]} " if options[:order]
- add_limit!(sql, options)
+ add_limit!(sql, options, scope)
return sanitize_sql(sql)
end
Oops, something went wrong.

0 comments on commit c9c1852

Please sign in to comment.