时间:2021-07-01 10:21:17 帮助过:9人阅读
In SQL Server, NOT EXISTS
and NOT IN
predicates are the best way to search for missing values, as long as both columns in question are NOT NULL
. They produce the safe efficient plans with some kind of an Anti Join
.
LEFT JOIN / IS NULL
is less efficient, since it makes no attempt to skip the already matched values in the right table, returning all results and filtering them out instead.
回答1
EXISTS
is used to return a boolean value, JOIN
returns a whole other table
EXISTS
is only used to test if a subquery returns results, and short circuits as soon as it does. JOIN
is used to extend a result set by combining it with additional fields from another table to which there is a relation.
In your example, the queries are semantically equivalent.
In general, use EXISTS
when:
JOIN
can cause duplicate rows if values are repeated)LEFT OUTER JOIN...NULL
condition)If you have proper indexes, most of the time the EXISTS
will perform identically to the JOIN
. The exception is on very complicated subqueries, where it is normally quicker to use EXISTS
.
If your JOIN
key is not indexed, it may be quicker to use EXISTS
but you will need to test for your specific circumstance.
JOIN
syntax is easier to read and clearer normally as well.
回答2
So with 3 rows and 5 rows matching
The result is the "short circuit" effect mentioned by others and no need to use DISTINCT with a JOIN. EXISTS is almost always quicker when looking for existence of rows on the n side of a 1:n relationship.
Joe‘s link is a good starting point. Quassnoi covers this too.
In general, if your fields are properly indexed, OR if you expect to filter out more records (i.e. have a lots of rows EXIST
in the subquery) NOT EXISTS
will perform better.
EXISTS
and NOT EXISTS
both short circuit - as soon as a record matches the criteria it‘s either included or filtered out and the optimizer moves on to the next record.
LEFT JOIN
will join ALL RECORDS regardless of whether they match or not, then filter out all non-matching records. If your tables are large and/or you have multiple JOIN
criteria, this can be very very resource intensive资源密集型.
I normally try to use NOT EXISTS
and EXISTS
where possible. For SQL Server, IN
and NOT IN
are semantically equivalent and may be easier to write. These are among the only operators you will find in SQL Server that are guaranteed to short circuit.
NOT IN vs. NOT EXISTS vs. LEFT JOIN / IS NULL: SQL Server
标签:either ESS lsp sum struct guarantee pes uil iss