On 3/5/16, Paul Sanderson <sandersonforensics at gmail.com> wrote:
> Clemens,
>
> Tim has the same issue as me, while
>
>   SELECT EXISTS (select status from mytable where status=1);
>
> works the portion in brackets is still executed in full and this is
> what we are trying to avoid.

The query planner in SQLite, while not brilliant, is smart enough to
know that it can stop and return true as soon as it sees the first row
from the query inside of EXISTS().

So if there is an index on mytable.status, the above will be very
query.  Or if there are instances of status=1 early in mytable, the
above will be quick.  But if mytable.status is never 1 and if there is
no index on mytable.status, then SQLite will scan the entire table to
verify that status is never 1 before returning 0.  I don't see anyway
around the latter case, however.

-- 
D. Richard Hipp
drh at sqlite.org

Reply via email to