子查询还是连接?
我想知道是否有人可以向我解释何时使用子查询以及何时使用联接。
例如。
我有以下查询:
SELECT * from contacts where id in (select contactId from contactgrouplink where groupId = 1);
对此子查询进行联接有什么好处?
I am wondering if anyone can explain to me when you would use a sub query and when you would use a join.
for example.
I have this query:
SELECT * from contacts where id in (select contactId from contactgrouplink where groupId = 1);
What would the benefit be of a join over this sub query?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看看这里讨论的
子查询与连接
Look at here well discussed
Subqueries vs joins
在每个查询之前使用 EXPLAIN 来查看查询的作用!
Use EXPLAIN just before each of those query's... to see what query does!
做一个解释,我的经验法则是尝试摆脱 DEPENDENT SUBQUERY,因为这意味着对于外部 SQL 语句中的每一行,都会执行一个查询。此外,您可以尝试将其实现为联接,并查看每个版本将检查多少行并从那里进行调用。
Do an EXPLAIN, my rule of thumb is to try to get rid of DEPENDENT SUBQUERY since that means for each row in the outer SQL statement, a query is executed. Also, you can try to implement it as a join and see how many rows each version would examine and make the call from there.
据我所知,子查询比 join 更好。
我还使用子查询,因为“加入”会影响性能。 (根据My-Sql偏好)
In my knowledge, Sub-query is batter than join .
I'm also use Sub-query Becoz "Join" is Effecting performance. (According My-Sql Preference)