Subject RE: [firebird-support] Two complicated questions firebird 2.5 cs
Author Svein Erling Tysvær
> and (
> (select count(*) from tfachres where artnr = :artnr) > 0
> and a.dispo = 0
> or coalesce((select count(*) from tfachres where artnr = :artnr), 0) <= 0
> and bestandsnr is null
> )
>
> Not sure if the query optimizer is smart enough to eval the count
> subquery only once.

Why bother to count at all? OK, it might not matter if the count is binary, but if it can be millions, then it can be time consuming. Rather, try

and ((exists(select * from tfachres where artnr = :artnr)
and a.dispo = 0)
or (not exists(select * from tfachres where artnr = :artnr)
and a.bestandsnr is null))

HTH,
Set