Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In SQL, How does using DISTINCT affect performance?

I am attempting to select a distinct list where duplicates are created over several fields. For example,

SELECT tablename.field1Date, 
       tablename.field2Number, 
       tablename.field3Text 
FROM tablename;

Would select duplicating records over the date, number and text fields respectively.

Now, when I select distinct records to provide what I am looking for, the performance seems to decrease dramatically.

SELECT DISTINCT tablename.field1Date,
                tablename.field2Number, 
                tablename.field3Text 
FROM tablename;

Is there any known reasons for this? I must admit I am using MS Access 2003 which may be the issue.

like image 627
Curtis Inderwiesche Avatar asked Feb 06 '09 16:02

Curtis Inderwiesche


2 Answers

Yes, basically it has to sort the results and then re-processed to eliminate the duplicates. This cull could also be being done during the sort, but we can only speculate as to how exactly the code works in the background. You could try and improve the performance by creating an index composed of all three (3) fields.

like image 136
BIBD Avatar answered Sep 23 '22 19:09

BIBD


This page has tips on improving your query performance and also some information on using the performance analyzer. It will tell you what if any indexes are needed.

http://support.microsoft.com/kb/209126

like image 39
Erik Nedwidek Avatar answered Sep 26 '22 19:09

Erik Nedwidek