mercredi 3 mai 2017

Commutability and order of active record scopes

Is the place of the "sorting" "filters" (Desc or Asc) inside a Rails' Active Record query important in terms of performance and logic ?

For example is the following scope 1 the sam as scope 2

SCOPE 1

scope :default_stream, -> { order(deal_end_date: :asc) } # this is the "sorting query"
scope :scope_1,
      lambda { default_stream.where('deal_start_date <= ? AND deal_end_date >= ?', Time.zone.now, Time.zone.now).where(is_cool: true) }

SCOPE 2

scope :scope_2,
      lambda { Deal.all.where('deal_start_date <= ? AND deal_end_date >= ?', Time.zone.now, Time.zone.now).where(is_cool: true).order(deal_end_date: :asc) }

Should I first use the "sorting query" then the other filters (scope1) or the opposite (scope2) ?

Not sure if it has an impact, but let me that the number of deals can be very important (>100000)

Aucun commentaire:

Enregistrer un commentaire