内部查询是否可以?

编程入门 行业动态 更新时间:2024-10-25 09:25:43
本文介绍了内部查询是否可以?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我经常看到类似...

SELECT events.id, events.begin_on, events.name FROM events WHERE events.user_id IN ( SELECT contacts.user_id FROM contacts WHERE contacts.contact_id = '1') OR events.user_id IN ( SELECT contacts.contact_id FROM contacts WHERE contacts.user_id = '1')

查询中有查询可以吗?是内部查询"吗?子查询"?它算作三个查询吗(我的例子)?如果这样做不好……我该如何重写我的示例?

Is it okay to have query in query? Is it "inner query"? "Sub-query"? Does it counts as three queries (my example)? If its bad to do so... how can I rewrite my example?

推荐答案

你的例子还不错.最大的问题通常来自存在所谓的相关子查询"的情况.那是子查询依赖于外部查询中的列的时候.这些问题尤其糟糕,因为实际上需要为潜在结果中的每一行重新运行子查询.

Your example isn't too bad. The biggest problems usually come from cases where there is what's called a "correlated subquery". That's when the subquery is dependent on a column from the outer query. These are particularly bad because the subquery effectively needs to be rerun for every row in the potential results.

您可以使用连接和 GROUP BY 重写您的子查询,但由于您拥有它,性能可能会有所不同,尤其是取决于您的 RDBMS.

You can rewrite your subqueries using joins and GROUP BY, but as you have it performance can vary, especially depending on your RDBMS.

更多推荐

内部查询是否可以?

本文发布于:2023-08-02 17:59:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1279927.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!