alf-tool / alf-core

Core classes, the kernel of Alf
MIT License
50 stars 5 forks source link

restrict done in memory instead of in sql #8

Open ludov04 opened 9 years ago

ludov04 commented 9 years ago

Hi,

After the migration from alf 0.14.0 to alf 0.16.3, some alf requests were taking much more time than before.

After investigation, we found that a restrict was executed in memory after alf retrieved all the tuples. Here is the code:

module Alf
  module Relvar
    def newer_than(t)
      return self unless t
      time = Alf::Tools.coerce(t, Pointguard::Helpers::TimeClass)
      restrict(Alf::Predicate.gte(:latest_change, time))
    end
  end
end

def tags_of_interest(input = self.input)
  toi = relvar{
    newer_than(tags, input[:newer_than])
  }
  if input[:game_id]
    toi = toi.restrict(game_id: input.game_id)
  end
  toi
end

If we change the code to use the dsl instead of the oop helper, this works correctly and the tags are restricted in sql:

def tags_of_interest(input = self.input)
  relvar{
    toi = newer_than(tags, input[:newer_than])
    if input[:game_id]
      toi = restrict(toi, game_id: input.game_id)
    end
    toi
  }
end

Why do the former restrict the tuples in memory while the latter generate the correct sql request ? How theses two forms are different from one another ?

Could you clarify how and when the database get hit ?