April 26, 2013 at 1:40 am
Hi all,
Here are two very small queries in which only difference is the use of DISTINCT. Both the queries are returning same rows and have identical IO Statistics and execution plan.
So, technically as there there is no performance gain we can use either of them. But I still want to use 2nd query as it is not having DISTINCT clause.
Can you share your experience on this.
1.
SELECT
top 100000 *
FROM
account_site
WHERE
account_id IN (
SELECT
DISTINCT account_id
FROM
account
)
2.
SELECT
top 100000 *
FROM
account_site
WHERE
account_id IN (
SELECT
account_id
FROM
account
)
Execution plan attached.
April 26, 2013 at 1:55 am
Is there a PRIMARY KEY or UNIQUE constraint on account.account_id? If there is, that'll be why the execution plans are identical.
John
April 26, 2013 at 3:07 am
Yes, There is a primary key on account.account_id
April 26, 2013 at 3:21 am
That's your answer. Account_id is the primary key. It is already unique. The use of distinct will produce the same number of rows as the query without it.
If you are trying to make performance better, try using EXISTS or a simple inner join.
Michael L John
If you assassinate a DBA, would you pull a trigger?
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
April 26, 2013 at 3:30 am
Michael L John (4/26/2013)
That's your answer. Account_id is the primary key. It is already unique. The use of distinct will produce the same number of rows as the query without it.If you are trying to make performance better, try using EXISTS or a simple inner join.
Using INNER JOIN may change the results as the query here is just performing an existence check.
EXISTS might give a different plan and probably better performance with the same output.
How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
April 26, 2013 at 3:40 am
I have tested same scenario with a non unique and unindexed column. Results are anyways same.
Yes, I have many other ways to perform the task. But point here is to test the query with or without DISTINCT.
In theory DISTINCT should not be used, but if it is giving same results then why/why not use DISTINCT!!!
April 26, 2013 at 3:46 am
Use DISTINCT when you want to eliminate duplicates from your result test. If you use it in an existence test, like you did in your original post, then it won't make any difference to the results. That applies whether the column has a unique/PK constraint or not.
John
April 26, 2013 at 5:04 am
John Mitchell-245523 (4/26/2013)
Use DISTINCT when you want to eliminate duplicates from your result test. If you use it in an existence test, like you did in your original post, then it won't make any difference to the results. That applies whether the column has a unique/PK constraint or not.John
+1
DISTINCT used in the EXISTS check is logically irrelevant. Check the plans - it may even be optimized away.
For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Viewing 8 posts - 1 through 7 (of 7 total)
You must be logged in to reply to this topic. Login to reply