Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #9560: only_include_referenced_tables.diff

File only_include_referenced_tables.diff, 3.3 kB (added by dasil003, 2 months ago)

New version of the patch tested against edge rails r8971

  • lib/active_record/associations.rb

    old new  
    13881388 
    13891389        def construct_finder_sql_for_association_limiting(options, join_dependency) 
    13901390          scope       = scope(:find) 
    1391           is_distinct = !options[:joins].blank? || include_eager_conditions?(options) || include_eager_order?(options) 
     1391           
     1392          # Only join tables referenced in order or conditions since this is particularly slow on the pre-query. 
     1393          tables_from_conditions = conditions_tables(options) 
     1394          tables_from_order      = order_tables(options) 
     1395          all_tables             = tables_from_conditions + tables_from_order 
     1396           
     1397          is_distinct = !options[:joins].blank? || include_eager_conditions?(options, tables_from_conditions) || include_eager_order?(options, tables_from_order) 
    13921398          sql = "SELECT " 
    13931399          if is_distinct 
    13941400            sql << connection.distinct("#{connection.quote_table_name table_name}.#{primary_key}", options[:order]) 
     
    13981404          sql << " FROM #{connection.quote_table_name table_name} " 
    13991405 
    14001406          if is_distinct 
    1401             sql << join_dependency.join_associations.collect(&:association_join).join 
     1407            sql << join_dependency.join_associations.reject{ |ja| !all_tables.include?(ja.table_name) }.collect(&:association_join).join 
    14021408            add_joins!(sql, options, scope) 
    14031409          end 
    14041410 
     
    14161422          return sanitize_sql(sql) 
    14171423        end 
    14181424 
    1419         # Checks if the conditions reference a table other than the current model table 
    1420         def include_eager_conditions?(options) 
     1425        def conditions_tables(options) 
    14211426          # look in both sets of conditions 
    14221427          conditions = [scope(:find, :conditions), options[:conditions]].inject([]) do |all, cond| 
    14231428            case cond 
     
    14261431              else            all << cond 
    14271432            end 
    14281433          end 
    1429           return false unless conditions.any? 
    1430           conditions.join(' ').scan(/([\.\w]+).?\./).flatten.any? do |condition_table_name| 
     1434          conditions.join(' ').scan(/([\.\w]+).?\./).flatten 
     1435        end 
     1436 
     1437        def order_tables(options) 
     1438          order = options[:order] 
     1439          return [] unless order && order.is_a?(String) 
     1440          order.scan(/([\.\w]+).?\./).flatten 
     1441        end 
     1442 
     1443        # Checks if the conditions reference a table other than the current model table 
     1444        def include_eager_conditions?(options,tables = nil) 
     1445          tables = conditions_tables(options) 
     1446          return false unless tables.any? 
     1447          tables.any? do |condition_table_name| 
    14311448            condition_table_name != table_name 
    14321449          end 
    14331450        end 
    14341451 
    14351452        # Checks if the query order references a table other than the current model's table. 
    1436         def include_eager_order?(options
    1437           order = options[:order] 
    1438           return false unless order 
    1439           order.to_s.scan(/([\.\w]+).?\./).flatten.any? do |order_table_name| 
     1453        def include_eager_order?(options,tables = nil
     1454          tables = order_tables(options) 
     1455          return false unless tables.any? 
     1456          tables.any? do |order_table_name| 
    14401457            order_table_name != table_name 
    14411458          end 
    14421459        end