forked from fixin.me/fixin.me
		
	Replace additional joins with NOT EXISTS subquery
This commit is contained in:
		
							parent
							
								
									4c09989788
								
							
						
					
					
						commit
						7234d60afc
					
				@ -18,20 +18,22 @@ class Unit < ApplicationRecord
 | 
				
			|||||||
  scope :defaults_diff, ->{
 | 
					  scope :defaults_diff, ->{
 | 
				
			||||||
    other_units = Unit.arel_table.alias('other_units')
 | 
					    other_units = Unit.arel_table.alias('other_units')
 | 
				
			||||||
    other_bases_units = Unit.arel_table.alias('other_bases_units')
 | 
					    other_bases_units = Unit.arel_table.alias('other_bases_units')
 | 
				
			||||||
    constraints = other_bases_units[:id].eq(other_units[:base_id])
 | 
					
 | 
				
			||||||
      .and(other_bases_units[:symbol].eq(Arel::Table.new(:bases_units)[:symbol]))
 | 
					    # add 'portable' fields (import on !default == export) to select
 | 
				
			||||||
 | 
					    with_defaults
 | 
				
			||||||
 | 
					      .where("NOT EXISTS (?)",
 | 
				
			||||||
 | 
					             Unit.select(1).from(other_units).joins(
 | 
				
			||||||
 | 
					               arel_table.create_join(
 | 
				
			||||||
 | 
					                 other_bases_units,
 | 
				
			||||||
 | 
					                 arel_table.create_on(other_bases_units[:id].eq(other_units[:base_id])),
 | 
				
			||||||
 | 
					                 Arel::Nodes::OuterJoin
 | 
				
			||||||
 | 
					               )
 | 
				
			||||||
 | 
					             ).where(
 | 
				
			||||||
 | 
					               other_bases_units[:symbol].eq(Arel::Table.new(:bases_units)[:symbol])
 | 
				
			||||||
               .and(other_units[:symbol].eq(arel_table[:symbol]))
 | 
					               .and(other_units[:symbol].eq(arel_table[:symbol]))
 | 
				
			||||||
               .and(other_units[:user_id].not_eq(arel_table[:user_id]))
 | 
					               .and(other_units[:user_id].not_eq(arel_table[:user_id]))
 | 
				
			||||||
 | 
					 | 
				
			||||||
    with_defaults
 | 
					 | 
				
			||||||
      .joins(
 | 
					 | 
				
			||||||
        arel_table.create_join(
 | 
					 | 
				
			||||||
          arel_table.grouping([other_units, other_bases_units]),
 | 
					 | 
				
			||||||
          arel_table.create_on(constraints),
 | 
					 | 
				
			||||||
          Arel::Nodes::OuterJoin
 | 
					 | 
				
			||||||
        ).to_sql
 | 
					 | 
				
			||||||
             )
 | 
					             )
 | 
				
			||||||
      .where({other_units: {id: nil}})
 | 
					            )
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  scope :ordered, ->{
 | 
					  scope :ordered, ->{
 | 
				
			||||||
    left_outer_joins(:base)
 | 
					    left_outer_joins(:base)
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user