I have a pretty big query.
The performance has always been reasonably acceptable, but I now have this problem.
I need to join a table more than once, using aliases of course, but doing so causes a massive performance hit.
It’s a huge query so I won’t post it all but here’s the gist:
SELECT table1.name,
ALIAS1.CODE AS "Sequence"
...
FROM table1
...
LEFT JOIN table2 ALIAS1
ON ALIAS1.COLUMN1 = table1.key1
AND ALIAS1.COLUMN2 = 'aaba'
AND ALIAS1.COLUMN3 = '00001'
...
This one is fine, running in about 90 seconds. But if I add the same table again as in the following, execution time increases exponentially (so far 2 hours and still running):
SELECT table1.name,
ALIAS1.CODE AS "Sequence",
ALIAS2.CODE AS "Rating"
...
FROM table1
...
LEFT JOIN table2 ALIAS1
ON ALIAS1.COLUMN1 = table1.key1
AND ALIAS1.COLUMN2 = 'aaba'
AND ALIAS1.COLUMN3 = '00001'
LEFT JOIN table2 ALIAS2
ON ALIAS2.COLUMN1 = table1.key1
AND ALIAS2.COLUMN2 = 'ffhr'
AND ALIAS2.COLUMN3 = '00107'
What I need to know is, what factors could be causing this?
It looks to me like the indexes should be ok otherwise the first join would be a problem.
Why would the second join be such an issue?
I actually need to join the same table a third time, but obviously this is a no-go as things stand!
When you join on three columns simultaneously, you should have a multi-column index that includes all three columns.
Perhaps instead of joining on constant values, you could put those filters in a WHERE clause.