嵌套的存在子句vs相邻的存在子句



有人能告诉我这2个查询中哪一个比另一个好,为什么?我应该使用连接吗?

select * from grandChildTable gct
where exists(
    select * from childTable ct
    where some condition
    and gct.parentId = ct.Id
    and exists(
        select * from Table t
        where some other condition
        and ct.parentId = t.Id))
select * from grandChildTable gct
where exists(
    select * from childTable ct
    where some condition
    and gct.parentId = ct.Id)
and exists(
    select * from Table t
    where some other condition
    and gct.grandParentId = t.Id)

指出:GrandChildTable具有ChildTableTable的id,因为它们的id是复合的。

这些表没有引用任何其他表。

表之间的关系如下:

GrandChild to Child
n:1
Child to Table
n:1

在这种情况下(与父表相比,父表的行数比子表少),使用join可能没有什么区别。然而,EXISTS通常更快,并且被证明是这样的。

我还希望优化器为两个基于EXISTS的查询生成相同的计划:检查查询计划并查看是否有任何差异(也包括这个Red Gate链接)。如果没有,那就考虑可读性。

最新更新