Solved

Execution Plan: How to reduce a Sort / Distinct Sort ?

Posted on 2006-11-28
7
3,149 Views
Last Modified: 2008-02-01
Hi I have the following query:

select distinct a,b,c,d
from table
where condition1

UNION

select distinct a,b,c,d
from table
where condition2

The result returns me 6 lines from about 4000 lines of the table which is correct.
The problems is that the query is very slow, more than 4mn with a P4 Bi-processor 3Ghz !

After a look in the execution plan I notice that a Sort / distinct Sort takes me 94% of cost !

Is there a way to improve the query and to reduce the time of execution ?

Thanks & regards.


0
Comment
Question by:bruno_boccara
  • 3
  • 3
7 Comments
 
LVL 35

Accepted Solution

by:
Raynard7 earned 250 total points
ID: 18032488
One very quick thing that you could do is remove the distinct from your above queries - the union will remove duplicates anyway
0
 
LVL 29

Assisted Solution

by:Nightman
Nightman earned 250 total points
ID: 18032494
UNION will already apply distinct

What about this:

select a,b,c,d
from table
where condition1

UNION

select a,b,c,d
from table
where condition2

Also, consider placing a non-clustered compound covering index on columns a,b,c,d
0
 

Author Comment

by:bruno_boccara
ID: 18032534
I removed the distinct but it didn't change anything.
I still have the Sort / Distinct Sort with 94% cost.
0
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

Author Comment

by:bruno_boccara
ID: 18032550
I also have indexes on columns used in condition1 and condition2
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 18032665
is this from the same table or different tables? if it is from the same table can you combine the conditions into one statement?

how fast to the individual queries run? this may be the problem.
0
 

Author Comment

by:bruno_boccara
ID: 18032984
It's Ok I found the error.
there was an unwanted table in the from.

I will split the points between us to thanks you for your tries.
0
 
LVL 35

Expert Comment

by:Raynard7
ID: 18032998
glad its now working
0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question