Question

i) Between these two which one is more efficient:

Select A.* from A, B where A.b_id = B.id

or

Select A.* from A where A.b_id in (select id from B);

ii) How does Select A.* from A where A.b_id in (select id from B); really work? Is it internally translated into something like Select A.* from A, B where A.b_id = B.id or is (select id from B) evaluated for each row of A?

Was it helpful?

OTHER TIPS

This one is more efficient:

Select A.* from A, B where A.b_id = B.id

Yes, Select A.* from A where A.b_id in (select id from B) works.

And, no, (select id from B) is not evaluated for each row. It's evaluated only once.

Normally, JOINS are faster than using the IN operator.

I'm not sure where those sorts of fake-joins come from, but you should join tables using ON, not a mass of WHERE's at the end.

SELECT A.*
FROM A
INNER JOIN B ON (A.b_id = B.id)

It depends on various factors, including the MySQL version, the size of the tables, the existence of indexes, etc.

You should try executing EXPLAIN ..., or even better if supported, EXPLAIN EXTENDED ... followed by SHOW WARNIGS; to try and understand what MySQL is doing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top