I am currently involved in a friendly debate with a coworker over whether or not using a query that contains multiple LEFT JOIN statements will have any significant performance impact.
Here is some background:
There are certain parts of our code that simply need to check if something exists. This could be done using a query such as the following:
and then seeing if there are any rows returned.
Now I think we can all agree that if there is an index on field for all these three tables, this query will be very inexpensive.
There is another part of the code where we need to fetch additional information, which involves following foreign key relationships using LEFT JOIN clauses, something along these lines:
My coworker is of the belief that these LEFT JOINs add a negligable amount of performance hit as long as the indexes are set up correctly, and suggests that we just reuse this query with all the JOINs to satisfy the above case (which is possible even though it is probably tough to see because of how I have obfuscated the queries)
My argument is that first of all this information is extraneous - if the JOINs can be avoided in some cases why don't we just get rid of them, especially since these queries will be run a fair number of times (maybe 10-100 times).
I understand that to accomplish a LEFT JOIN, the database must compute the cartesian product which involves two nested loops - a fairly expensive operation when compared with a simple SELECT from three tables (which would involve just looking at the indexes for those columns in each table, which as I understand it is a constant-time operation).
Could anyone provide some real-world performance numbers and optimization strategies for queries that use lots of JOINs? Should they be avoided like the plague? Are they OK if your indexes can handle them (i.e. you are mostly JOINing on the primary key)?
Thanks for any insight anyone can provide - and I will be happy to provide more information if this is not sufficient.
Here is some background:
There are certain parts of our code that simply need to check if something exists. This could be done using a query such as the following:
Code:
SELECT id FROM table1 WHERE field = 'value' UNION SELECT id FROM table2 WHERE field = 'value' UNION SELECT id FROM table3 WHERE field = 'value'
and then seeing if there are any rows returned.
Now I think we can all agree that if there is an index on field for all these three tables, this query will be very inexpensive.
There is another part of the code where we need to fetch additional information, which involves following foreign key relationships using LEFT JOIN clauses, something along these lines:
Code:
select p.field1, p.field2, f.field1, pc.id, pc.name,
ppc.id, ppc.name, cspc.id, cspc.name
from table1 p
left join table2 f on p.field2 = f.id
left join table3 cs on p.field3 = cs.id
left join table4 pc on pc.field1 = p.id
left join table4 ppc on pc.field2 = ppc.id
left join table4 cspc on cspc.field1= cs.id
where p.field4 = 'value'
My coworker is of the belief that these LEFT JOINs add a negligable amount of performance hit as long as the indexes are set up correctly, and suggests that we just reuse this query with all the JOINs to satisfy the above case (which is possible even though it is probably tough to see because of how I have obfuscated the queries)
My argument is that first of all this information is extraneous - if the JOINs can be avoided in some cases why don't we just get rid of them, especially since these queries will be run a fair number of times (maybe 10-100 times).
I understand that to accomplish a LEFT JOIN, the database must compute the cartesian product which involves two nested loops - a fairly expensive operation when compared with a simple SELECT from three tables (which would involve just looking at the indexes for those columns in each table, which as I understand it is a constant-time operation).
Could anyone provide some real-world performance numbers and optimization strategies for queries that use lots of JOINs? Should they be avoided like the plague? Are they OK if your indexes can handle them (i.e. you are mostly JOINing on the primary key)?
Thanks for any insight anyone can provide - and I will be happy to provide more information if this is not sufficient.