문제

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?

도움이 되었습니까?

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top