Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

With or without DISTINCT ??? Expand / Collapse
Author
Message
Posted Friday, April 26, 2013 1:40 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: 2 days ago @ 11:53 PM
Points: 714, Visits: 516
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.


  Post Attachments 
distinct.txt (7 views, 7.97 KB)
Post #1446830
Posted Friday, April 26, 2013 1:55 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:55 AM
Points: 5,073, Visits: 8,900
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
Post #1446835
Posted Friday, April 26, 2013 3:07 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: 2 days ago @ 11:53 PM
Points: 714, Visits: 516
Yes, There is a primary key on account.account_id
Post #1446860
Posted Friday, April 26, 2013 3:21 AM


SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: Today @ 2:33 PM
Points: 859, Visits: 2,616
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
To properly post on a forum:
http://www.sqlservercentral.com/articles/61537/
Post #1446868
Posted Friday, April 26, 2013 3:30 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Thursday, April 10, 2014 5:09 AM
Points: 2,567, Visits: 4,654
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/
Post #1446870
Posted Friday, April 26, 2013 3:40 AM


Right there with Babe

Right there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with BabeRight there with Babe

Group: General Forum Members
Last Login: 2 days ago @ 11:53 PM
Points: 714, Visits: 516
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!!!
Post #1446875
Posted Friday, April 26, 2013 3:46 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 5:55 AM
Points: 5,073, Visits: 8,900
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
Post #1446881
Posted Friday, April 26, 2013 5:04 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:01 AM
Points: 6,742, Visits: 12,834
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
Post #1446897
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse