Subject Re: [firebird-support] select in (subquery)
Author Helen Borrie
At 09:15 PM 29/11/2004 -0300, you wrote:

>I'm using the following sentence, where the Personas table has more than
>19000 rows and the subquery returns, more or less, 5 rows.
>
>
>
>select p.Apellido, p.Nombre
>
> from Personas p
>
> where p.Persona_ID in
>
> (select b.Persona_ID
>
> from Beneficiarios b
>
> where b.Beneficiario_Titular_Id = :Titular_ID
>
> )
>
>
>I think the problem is that the engine doesn't know how many rows the
>subquery will return (neither me :-)).

You don't say what database engine you are using. Firebird should optimize
this statement to the following (which does allow use of the index for the
existential search):

select p.Apellido, p.Nombre
from Personas p
where EXISTS(
SELECT select 1 from Beneficiarios b
WHERE p.Persona_id = b.Persona_id
and b.Beneficiario_Titular_Id = :Titular_ID
)



>I'm wondering if there is some way to force the execution plan to use the
>index PERSONAS_PK?

Try this query and compare it with your original - especially if you are
using InterBase. If you need more, come back and tell us what database
server you are using.

./hb