Sunday, February 12, 2012

Are these two statements equivalent?

Both return the same records in the same order, with the same values, and I
think I'm right, but I just wanted to get some other eyeballs on this so I
can win the argument we're having here, because mine (the second one) is
more than twice as fast.
-- Uses a WHERE...IN(...) and subquery on one of the joined tables
SELECT DISTINCT ss.SampleSourceKey, dbo.ParseFilename(ss.Filename) AS
Filename
FROM SampleSource ss WITH (NOLOCK)
INNER JOIN CLMR WITH (NOLOCK)
ON clmr.SampleSourceKey = ss.SampleSourceKey
WHERE ss.SampleSourceKey IN (
SELECT clmr.SampleSourceKey
FROM CLMR WITH (NOLOCK)
WHERE CountryKey IS NULL
)
ORDER BY ss.SampleSourceKey
-- Uses a column from one of the joined tables.
SELECT DISTINCT ss.SampleSourceKey, dbo.ParseFilename(ss.Filename) AS
Filename
FROM SampleSource ss WITH (NOLOCK)
INNER JOIN CLMR WITH (NOLOCK)
ON clmr.SampleSourceKey = ss.SampleSourceKey
WHERE clmr.CountryKey IS NULL
ORDER BY ss.SampleSourceKey
Peace & happy computing,
Mike Labosh, MCSD
"When you kill a man, you're a murderer.
Kill many, and you're a conqueror.
Kill them all and you're a god." -- Dave Mustanethe usual: if this query
SELECT clmr.SampleSourceKey
FROM CLMR WITH (NOLOCK)
WHERE CountryKey IS NULL
can ever return a null, then the first query will return nothing...|||> the usual: if this query
> SELECT clmr.SampleSourceKey
> FROM CLMR WITH (NOLOCK)
> WHERE CountryKey IS NULL
> can ever return a null, then the first query will return nothing...
Ooo I had forgotten that, but we're safe here, because clmr.SampleSourceKey
is required.
Thanks for the brainpick!
--
Peace & happy computing,
Mike Labosh, MCSD
"When you kill a man, you're a murderer.
Kill many, and you're a conqueror.
Kill them all and you're a god." -- Dave Mustane

No comments:

Post a Comment