admin 管理员组

文章数量: 1086019

I have the following two queries. The first runs in less than 1 second. The latter query takes minutes to run. All of the columns being joined are indexed. Both queries are doing the same thing, the second just combines the two not exists from the first into a single not exists.

Is the second written in a way which is known to perform worse than two not exists?

Executes in less than 1 second:

select  *
from
    A
    join B on B.AId = A.Id
where
    not exists (select * from C where C.ObjectId = A.Id)
    and not exists (select * from C where C.ObjectId = B.Id)

Runs for minutes:

select  *
from
    A
    join B on B.AId = A.Id
where
    not exists (select * from C where C.ObjectId = A.Id or C.ObjectId = B.Id)

本文标签: sqlOR is slower than two NOT EXISTSStack Overflow