I am trying to determine whether a function like EXISTS or COALESCE could reduce the heavy use of nested loops when searching for any match on 300 values that could occur in 16 different columns.
A user supplied a query that looks like the example below and asked to apply it in a large warehouse environment, to which it does not scale very efficiently. Adding indices is not an option. I can use a WITH clause to house the 300 codes for lookup, but cannot determine whether that would help to get the query to abort the table scans once the first match is found and move onto the next record.
SELECT
A.product_id
FROM
A inner join B on A.id = B.id
inner join C on A.id = C.id
WHERE
B.code1 in ('x1','x2','x3', ... 'x298', 'x299', 'x300')
or B.code2 in ('x1','x2','x3', ... 'x298', 'x299', 'x300')
or C.code3 in ('x1','x2','x3', ... 'x298', 'x299', 'x300')
...
or C.code15 in ('x1','x2','x3', ... 'x298', 'x299', 'x300')
or C.code16 in ('x1','x2','x3', ... 'x298', 'x299', 'x300')
While the code supplied works, the cost is high and I would appreciate help in getting execution times improved.