Subject Re: [firebird-support] LEFT JOIN AND ORDER BY
Author Dmitry Yemanov
"aleskahanek" <akahanek@...> wrote:
>
> This query takes about 20 seconds
>
> SELECT PERSON.LNAME, COMPANY.COM_NAME
> FROM PERSON
> LEFT JOIN COMPANY ON PERSON.COM_ID=COMPANY.COM_ID
> ORDER BY PERSON.LNAME
>
> PLAN SORT (JOIN (PERSON NATURAL,COMPANY INDEX (RDB$PRIMARY4)))
>
> while this returns almost immediately (without sorting)
>
> SELECT PERSON.LNAME
> FROM PERSON
> LEFT JOIN COMPANY ON PERSON.COM_ID=COMPANY.COM_ID
>
> Why Firebird does not use the LNAME index for table PERSON?

Because it's not smart enough. Although I doubt this index would help you
much unless you need only a few rows instead of all 300K.

> I need to use LEFT JOIN and also order the result set according to
> LNAME.

SELECT
PERSON.LNAME,
( SELECT COMPANY.COM_NAME FROM COMPANY WHERE COMPANY.COM_ID =
PERSON.COM_ID )
FROM
PERSON

???


Dmitry