SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


With or without DISTINCT ???


With or without DISTINCT ???

Author
Message
T.Ashish
T.Ashish
SSC Eights!
SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)

Group: General Forum Members
Points: 921 Visits: 599
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.
Attachments
distinct.txt (10 views, 7.00 KB)
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14676 Visits: 15980
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
T.Ashish
T.Ashish
SSC Eights!
SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)

Group: General Forum Members
Points: 921 Visits: 599
Yes, There is a primary key on account.account_id
Michael L John
Michael L John
SSCrazy
SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)SSCrazy (3K reputation)

Group: General Forum Members
Points: 2996 Visits: 7811
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/
Kingston Dhasian
Kingston Dhasian
Hall of Fame
Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)Hall of Fame (3.8K reputation)

Group: General Forum Members
Points: 3761 Visits: 5180
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.


Kingston Dhasian

How to post data/code on a forum to get the best help - Jeff Moden
http://www.sqlservercentral.com/articles/Best+Practices/61537/
T.Ashish
T.Ashish
SSC Eights!
SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)SSC Eights! (921 reputation)

Group: General Forum Members
Points: 921 Visits: 599
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!!!
John Mitchell-245523
John Mitchell-245523
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14676 Visits: 15980
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
ChrisM@Work
ChrisM@Work
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16878 Visits: 19557
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.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

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
Exploring Recursive CTEs by Example Dwain Camps
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search