I realized the below SQL using DISTINCT
is a bad thing for performance.
However, if that is not included it will return too many rows (due to the one-to-many relationship between Instrument
and Party
tables).
I have searched the internet and see a few suggestions about getting rid of the DISTINCT
, but I honestly have no idea what the best solution is.
Can you please provide recommendations for improving this SQL and ensuring it only returns one row per instrument (as opposed to multiple rows for each instrument)?
Just FYI - some things I saw as performance suggestions:
Get rid of IN statement that is easy and will be done
Replace DISTINCT with GROUP BY does this really improve performance?
Perhaps do an outer SELECT that includes DISTINCT around the big SQL without the DISTINCT. My thought behind this is that the DISTINCT would be performed on a much smaller subset than the 1M++ rows in INSTRUMENT table.
My query:
select *
from
(
SELECT DISTINCT
I.UOID,
I.INSTRUMENT_ID,
I.STATUS,
I.A_ACTIVITY_ORIG,
I.A_CURRENCY,
I.A_OPER_BK_ORG_ORIG,
I.A_POSITION_ACTIVE,
I.A_PRODUCT,
I.A_PRODUCT_TYPE,
I.A_TERMS_ACTIVE,
I.CURR_COI,
I.BOUT_COI,
I.A_CUST_RELATIONSHP,
I.DATE_START,
I.DATE_END,
I.CLIB_COI,
I.CLIB_BASE,
I.BLIB_COI,
I.BLIB_BASE,
I.BOUT_BASE,
I.AVAL_COI,
I.SEQUENCE_NUM,
I.AVAL_BASE,
I.MAXU_COI,
I.MAXU_BASE,
I.A_CURRENCY_BASE,I.A_CLIENT_BANK,
I.A_PRODUCT_CATEGORY,
I.A_ASSIGNMENT_ACTV,
I.A_RELATED_ACTIVITY,
C.CUSTOMER_ID,
C.SHORT_NAME
FROM instrument I
INNER JOIN PARTY P ON P.A_INSTRUMENT = I.UOID
INNER JOIN CUSTOMER C ON P.A_CUSTOMER = C.UOID AND C.CUSTOMER_ID = :customerId
WHERE (I.STATUS <> 'TMP')
AND (I.A_CLIENT_BANK = :clientBank)
AND I.A_PRODUCT_CATEGORY <> 'CM'
AND I.STATUS NOT IN ( 'CAN','CLO','DEA','LIQ')
)
where rownum <= :maxSize;