r/SQL • u/i_literally_died • Jun 06 '26
SQL Server What are some obvious reasons a 1:1 join would work better as LEFT than INNER?
I asked the magic box and it spat out paragraph after paragraph of stuff about cardinality and indexing, of which go way over my head and I don't have access to check. But basically:
I work in a system where (as a for instance) there are plenty of obvious 1:1 joins, such as:
SELECT
ol.ProductId,
p.Name
FROM OrderLines ol
JOIN Products p
ON ol.ProductId = p.Id
WHERE ol.OrderId = '1234'So this should give you the product ids on the order, and then their text name from the Products table.
I'm finding in multiple tables and instances that are pk or other 1:1 joins like this, that an inner join can take ~30 seconds to run, where a left outer runs instantly.
The data output is the same, but the timing is all over, and I'm wondering on what the main/obvious reasons for this are?
15
u/ComicOzzy sqlHippo Jun 06 '26
Are you supplying the ID as a string or did you just do that here by accident?
4
u/r3pr0b8 GROUP_CONCAT is da bomb Jun 06 '26
your example query is an INNER JOIN, but if you were to turn it into a LEFT OUTER JOIN like this --
then i would have to question your database's integrity
it suggests that you have rows in OrderLines which have no matching row in Products
huge red flag
2
u/No_Resolution_9252 Jun 06 '26
Are you sure this is for SQL Server?
3
u/Loriken890 Jun 06 '26
That’s not a 1:1 join.
Multiple orderlines point to the same one product. That’s a *:1
The productid on the ordelines table is not unique. You could even have the same product ordered multiple times on the same order.
If you swapped the join of the tables around (product to orderliness), it would be 1:*.
1:1 is when both fields are unique for the tables involved.
Order.orderid and customer.lastorderid would be 1:1. An orderid is unique to the order. No two orders will have the sane orderid. Add the lastordereid is unique to the customer. You won’t find 2 customers with the same lastorderid. And all the lastorderid will point to a single orderid.
7
u/da_chicken Jun 06 '26
Misleading table statistics or cardinality estimates would be my first guess. Is there a foreign key constraint on OrderLine.ProductId --> Products.Id? If not, that can force the server to use an improper join operator (i.e., loop vs hash).
Look at the query execution plans and look for differences in cardinality estimates or operator selection.
2
u/squadette23 Jun 06 '26
I've always thought that LEFT JOIN for N:1 cardinality should be less "work" than INNER JOIN.
Basically the optimizer knows that the output would have the same number of rows as the left table, so you can sort of pre-allocate. With INNER JOIN, the number of output rows could be less due to filtering semantics. Because of that you may want, for example, to allocate output rows on demand.
This is all super handwavy of course, and heavily depends on optimizer capabilities and data distribution statistics, but I'm not surprised at all by what you observe.
I wonder what happens if you do LEFT JOIN + WHERE p.Id IS NOT NULL, haha.
5
u/gumnos Jun 06 '26
Things I would investigate:
ol.ProductIdandp.Idthe same datatype? Or is some implicit conversion happening?OrderLines.OrderIdorProducts.Id, you'd likely have problemsINNER JOINquery is unpredictable, then it could be something locking rows/tablesol.OrderIdwhen you compare it to a string?3
u/ithinkilikerunning Jun 07 '26
Left join would be helpful one a 1:1 join when looking for rows that don’t exist in the right table.
1
u/Groundbreaking-Fish6 Jun 06 '26
Please define your joins as INNER, OUTER, LEFT or RIGHT it makes it much easier to read.
To answer your question, JOINs are not 1:1 unless there is only 1 record in the return set. The WHERE clause is a filter. So your above example will query all the records from Products and OrderLines TABLES then apply the filter on OrderId, barring the SQL Server query optimizer.
For this case a LEFT OUTER JOIN will return at least a row of null values if no Products are found. An INNER JOIN (aka JOIN) will not return any records because there are none that meet the JOIN criteria.
1
u/mikeblas Jun 06 '26
You should compare the execution plans for the two statements. If you don't know how to get them or how to interpret them or find the differences, ask over in /r/SQLOptimization
1
u/Yavuz_Selim Jun 06 '26
What does the execution plan say?
(ID's / primary keys with data type string. Ew.)
1
u/Zestyclose-Turn-3576 Jun 06 '26
Wouldn't order:product be 0-N:1? There are potentially zero-to-many orders per product?
A very long time ago, in Oracle 9 I think it was, this could happen because a hash join had to access the smaller table first, but a left join prevented that from happening so you got stuck with a much less efficient nested loop.
Seems unlikely that this sort of thing would be the case on a modern system, but you'll need to see an execution plan to be able to tell.
1
u/thesqlguy Jun 07 '26 edited Jun 07 '26
If you are asking purely about performance and not logic it depends on the indexes, statistics, parameters/literals, etc. you need to look at the difference plans.
We'd also need to see your schema and exact sql to help more.
Select from a Left join b where a=xxxxwill usually be a loop join from a to b, using the criteria on a in your where clause to seek/scan on a, since the optimizer has no other way to filter a.But when you do an inner join the optimizer has more options now on how to filter a. It may decide to select from b first and then loop join back to a if it thinks that more efficient. But, it may be wrong for your specific criteria and that's why whatever new plan it picked is slower.
That's just an example but you get the idea.
Btw if this is sql server the optimizer knows about foreign keys, so if they exist the plan wouldn't change since it would know left join and inner join are logically equivalent. You can test this of course. Another way to confirm is something like
select from a where exists (select from b where a.fkid=b.id).. You will notice the optimizer doesn't even bother accessing table b at all since it knows the exists clause must be true due to RI!1
u/therealdrsql Jun 08 '26
Look at the plan and the IO and rows output. If you can’t understand the difference, someone can help you. There are no obvious reasons for this, an inner join would be more likely to be faster.