Subject RE: [firebird-support] Re: Optimizing a count query
Author Leyne, Sean
Dinol,

> Plan:
> ------------------------------------------------
> PLAN (TBL_JOB_CARD_EMPLOYEE NATURAL)
> PLAN SORT (JOIN (JOIN (TBL_JOB_CARD NATURAL,TBL_CLIENT INDEX
> (RDB$PRIMARY2)),TBL_INSURER INDEX (RDB$PRIMARY3)))

As Svein has already pointed out, you need to create an index on the
JOB_CARD_ID column in the TBL_JOB_CARD_EMPLOYEE table!

Second, you could turn the problem around ... and try:

SELECT
tjc.EXCESS_PAID,
...
tjc.COORDINATES,
tjc.VAT
FROM (
SELECT DISTINCT JOB_CARD_ID FROM TBL_JOB_CARD_EMPLOYEE
) TJCE
JOIN TBL_JOB_CARD tjc = tjce.JOB_CARD_ID
LEFT OUTER JOIN TBL_CLIENT tc ON (tc.CLIENT_ID = tjc.CLIENT_ID)
LEFT OUTER JOIN TBL_INSURER ti ON (ti.INSURER_ID = tjc.INSURER_ID)
ORDER BY
tjc.JOB_CARD_ID DESC


Sean