The idea is to create limit for Filter function.
It would work like: Iterate over data until limit is reached, then break the loop.
For example:
q.Filter(
[1, 2, 3,4,5],
q.Lambda(
'i',
q.GT(0,q.Var('i'))),
3 //limit is here
)
would return just [1,2,3]
The problem is that when someone would iterated over 100,000 results he would have filter activated for each item in index.
Limit would reduce that until there’s satisfying number of items returned.
You could use the Take function and specify the number of elements to be returned. But, the Lambda Function would have been executed on the entire set.
Since in the above case, it will only iterate over the results of the first page. If that page is 100 000 big then you are right.
However, if you calculate it on a set which probably is often the case, you can actually use page size to achieve the same thing. If I’m not mistaking, filter will only be executed that many times until enough values for the first page are calculated. This is quite similar to what you are hoping to achieve.
@ambrt
absolutely, that’s why you use indexes where you can. In this case since it’s a GT you could have used an index if I’m not mistaken. Probably not immediately obvious because you need Greater Than and not Greater Than or Equal but it can still be done. If I didn’t make a mistake, this should be the equivalent of SELECT * FROM table1 WHERE a > 5 AND a < 10