Subject | Optimizing a count query |
---|---|
Author | dinol.softedge |
Post date | 2008-10-12T08:03:07Z |
Hi
I am trying to optimize the following count query by changing it to an
exists query. I have tried various exists statements but I cannot
seem to get it right. Any help would be greatly appreciated
The count subquery is near the bottom
SELECT
TBL_JOB_CARD.EXCESS_PAID,
TBL_JOB_CARD.JOB_CARD_ID,
TBL_JOB_CARD.TIME_START,
TBL_JOB_CARD.TIME_FINISH,
TBL_JOB_CARD.JOB_TYPE_NOTES,
TBL_JOB_CARD.JOB_TYPE,
TBL_JOB_CARD.PAYMENT_TYPE,
TBL_JOB_CARD.DATE_TIME,
TBL_JOB_CARD.BOOKED_TIME,
TBL_JOB_CARD.BOOKED_DATE,
TBL_JOB_CARD.INVOICE_NO,
TBL_JOB_CARD.CLAIM_NO,
TBL_JOB_CARD.DISCOUNT,
TBL_JOB_CARD.MATERIALS,
TBL_JOB_CARD.LABOUR,
TBL_JOB_CARD.STD_CELL,
TBL_JOB_CARD.STD_PETROL,
TBL_JOB_CARD.CALLOUT,
TBL_JOB_CARD.PAID,
TBL_JOB_CARD.CANCELLED,
TBL_JOB_CARD.COMPLETED,
TBL_JOB_CARD.PRINTED,
TBL_JOB_CARD.COMPLAINT,
TBL_JOB_CARD.USER_NAME,
TBL_JOB_CARD.TRAVEL_TIME,
TBL_JOB_CARD.CHEQUE_NO,
TBL_JOB_CARD.EXCESS,
TBL_JOB_CARD.INSURANCE_PAYMENT,
TBL_JOB_CARD.POLICY_NO,
TBL_JOB_CARD.REF_NO,
TBL_JOB_CARD.TIME_TAKEN,
TBL_CLIENT.ADDRESS AS CLIENT_ADDRESS,
TBL_CLIENT.SUBURB AS CLIENT_SUBURB,
TBL_CLIENT.CITY AS CLIENT_CITY,
TBL_CLIENT.TELEPHONE_HOME AS CLIENT_TELEPHONE_HOME,
TBL_CLIENT.TELEPHONE_WORK AS CLIENT_TELEPHONE_WORK,
TBL_CLIENT.FAX AS CLIENT_FAX,
TBL_CLIENT.CELL AS CLIENT_CELL,
TBL_CLIENT.NAME AS CLIENT_NAME,
TBL_CLIENT.SURNAME AS CLIENT_SURNAME,
TBL_CLIENT.COMPANY AS CLIENT_COMPANY,
TBL_CLIENT.EMAIL AS CLIENT_EMAIL,
TBL_CLIENT.PROVINCE AS CLIENT_PROVINCE,
TBL_JOB_CARD.ADVERT,
TBL_INSURER.COMPANY AS INSURER_COMPANY,
TBL_INSURER.TELEPHONE AS INSURER_TELEPHONE,
TBL_INSURER.FAX AS INSURER_FAX,
TBL_INSURER.EMAIL AS INSURER_EMAIL,
TBL_CLIENT.TITLE || ' ' || TBL_CLIENT.SURNAME || ', ' || JOB_TYPE AS
CAPTION,
TBL_CLIENT.ADDRESS || ', ' || TBL_CLIENT.SUBURB || ', ' ||
TBL_CLIENT.CITY AS FULL_ADDRESS,
TBL_JOB_CARD.DOC_TYPE,
TBL_CLIENT.NAME || ' ' || TBL_CLIENT.SURNAME AS FULL_NAME,
TBL_JOB_CARD.QUOTE_DATE,
TBL_JOB_CARD.INVOICE_DATE,
TBL_JOB_CARD.PAYMENT_AMOUNT,
TBL_JOB_CARD.ESTIMATED_TIME_FINISH,
TBL_JOB_CARD.ADVISOR,
TBL_JOB_CARD.ASSESSOR,
TBL_JOB_CARD.COORDINATES,
(SELECT COUNT(EMPLOYEE_ID) FROM TBL_JOB_CARD_EMPLOYEE WHERE
TBL_JOB_CARD.JOB_CARD_ID = TBL_JOB_CARD_EMPLOYEE.JOB_CARD_ID) AS
EMPLOYEE_COUNT,
TBL_JOB_CARD.AMOUNT_DUE,
TBL_JOB_CARD.INVOICE_PRICE_INCL,
TBL_JOB_CARD.VAT
FROM
TBL_JOB_CARD
LEFT OUTER JOIN TBL_CLIENT ON (TBL_JOB_CARD.CLIENT_ID =
TBL_CLIENT.CLIENT_ID)
LEFT OUTER JOIN TBL_INSURER ON (TBL_JOB_CARD.INSURER_ID =
TBL_INSURER.INSURER_ID)
ORDER BY
JOB_CARD_ID DESC
I am trying to optimize the following count query by changing it to an
exists query. I have tried various exists statements but I cannot
seem to get it right. Any help would be greatly appreciated
The count subquery is near the bottom
SELECT
TBL_JOB_CARD.EXCESS_PAID,
TBL_JOB_CARD.JOB_CARD_ID,
TBL_JOB_CARD.TIME_START,
TBL_JOB_CARD.TIME_FINISH,
TBL_JOB_CARD.JOB_TYPE_NOTES,
TBL_JOB_CARD.JOB_TYPE,
TBL_JOB_CARD.PAYMENT_TYPE,
TBL_JOB_CARD.DATE_TIME,
TBL_JOB_CARD.BOOKED_TIME,
TBL_JOB_CARD.BOOKED_DATE,
TBL_JOB_CARD.INVOICE_NO,
TBL_JOB_CARD.CLAIM_NO,
TBL_JOB_CARD.DISCOUNT,
TBL_JOB_CARD.MATERIALS,
TBL_JOB_CARD.LABOUR,
TBL_JOB_CARD.STD_CELL,
TBL_JOB_CARD.STD_PETROL,
TBL_JOB_CARD.CALLOUT,
TBL_JOB_CARD.PAID,
TBL_JOB_CARD.CANCELLED,
TBL_JOB_CARD.COMPLETED,
TBL_JOB_CARD.PRINTED,
TBL_JOB_CARD.COMPLAINT,
TBL_JOB_CARD.USER_NAME,
TBL_JOB_CARD.TRAVEL_TIME,
TBL_JOB_CARD.CHEQUE_NO,
TBL_JOB_CARD.EXCESS,
TBL_JOB_CARD.INSURANCE_PAYMENT,
TBL_JOB_CARD.POLICY_NO,
TBL_JOB_CARD.REF_NO,
TBL_JOB_CARD.TIME_TAKEN,
TBL_CLIENT.ADDRESS AS CLIENT_ADDRESS,
TBL_CLIENT.SUBURB AS CLIENT_SUBURB,
TBL_CLIENT.CITY AS CLIENT_CITY,
TBL_CLIENT.TELEPHONE_HOME AS CLIENT_TELEPHONE_HOME,
TBL_CLIENT.TELEPHONE_WORK AS CLIENT_TELEPHONE_WORK,
TBL_CLIENT.FAX AS CLIENT_FAX,
TBL_CLIENT.CELL AS CLIENT_CELL,
TBL_CLIENT.NAME AS CLIENT_NAME,
TBL_CLIENT.SURNAME AS CLIENT_SURNAME,
TBL_CLIENT.COMPANY AS CLIENT_COMPANY,
TBL_CLIENT.EMAIL AS CLIENT_EMAIL,
TBL_CLIENT.PROVINCE AS CLIENT_PROVINCE,
TBL_JOB_CARD.ADVERT,
TBL_INSURER.COMPANY AS INSURER_COMPANY,
TBL_INSURER.TELEPHONE AS INSURER_TELEPHONE,
TBL_INSURER.FAX AS INSURER_FAX,
TBL_INSURER.EMAIL AS INSURER_EMAIL,
TBL_CLIENT.TITLE || ' ' || TBL_CLIENT.SURNAME || ', ' || JOB_TYPE AS
CAPTION,
TBL_CLIENT.ADDRESS || ', ' || TBL_CLIENT.SUBURB || ', ' ||
TBL_CLIENT.CITY AS FULL_ADDRESS,
TBL_JOB_CARD.DOC_TYPE,
TBL_CLIENT.NAME || ' ' || TBL_CLIENT.SURNAME AS FULL_NAME,
TBL_JOB_CARD.QUOTE_DATE,
TBL_JOB_CARD.INVOICE_DATE,
TBL_JOB_CARD.PAYMENT_AMOUNT,
TBL_JOB_CARD.ESTIMATED_TIME_FINISH,
TBL_JOB_CARD.ADVISOR,
TBL_JOB_CARD.ASSESSOR,
TBL_JOB_CARD.COORDINATES,
(SELECT COUNT(EMPLOYEE_ID) FROM TBL_JOB_CARD_EMPLOYEE WHERE
TBL_JOB_CARD.JOB_CARD_ID = TBL_JOB_CARD_EMPLOYEE.JOB_CARD_ID) AS
EMPLOYEE_COUNT,
TBL_JOB_CARD.AMOUNT_DUE,
TBL_JOB_CARD.INVOICE_PRICE_INCL,
TBL_JOB_CARD.VAT
FROM
TBL_JOB_CARD
LEFT OUTER JOIN TBL_CLIENT ON (TBL_JOB_CARD.CLIENT_ID =
TBL_CLIENT.CLIENT_ID)
LEFT OUTER JOIN TBL_INSURER ON (TBL_JOB_CARD.INSURER_ID =
TBL_INSURER.INSURER_ID)
ORDER BY
JOB_CARD_ID DESC