I have a parent table TABLE_A with a, b and c columns and table TABLE_B with a, b and d columns; I have written a function in Postgresql to join these tables, which function query will be faster. NOTE: both a and b columns of the TABLE_A and TABLE_B are indexed, and a and b will be constant values that won’t change throughout the function.
QUERY 1:
SELECT * FROM TABLE_A as ta
LEFT JOIN TABLE_B as tb ON tb.a = ta.a and tb.b = tb.ta.b
WHERE
ta.a = $1 and
ta.b = $2
QUERY 2:
SELECT * FROM TABLE_A as ta
LEFT JOIN TABLE_B as tb ON tb.a = $1 and tb.b = $2
WHERE
ta.a = $1 and
ta.b = $2
NOTE: $1 and $2 are the function’s first and second parameters, respectively.