D
D
des1roer2017-06-06 17:54:27
PostgreSQL
des1roer, 2017-06-06 17:54:27

Left join vs Multiple selects?

Which is better in terms of performance? versus
select a left join b on b.id = a.b_id

select from a // (получили 1,2,3)
select from b where id = 1,2,3

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
terrier, 2017-06-06
@terrier

one). Your "SQL" is more like pseudocode
2). These queries are not equivalent (read more about LEFT JOIN)
3). The first option is faster (under reasonable assumptions), the second is a well-known anti-pattern used by novice programmers who are frightened by "slow joins". In fact, joins are exactly what relational databases can do very quickly and very advanced.

R
RidgeA, 2017-06-06
@RidgeA

join if there are indices

A
Artem Kislenko, 2017-06-06
@webwork

I don't know about you, of course. But when I tested it on my database, it processes a request like:
select from b where id in (select id from a)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question