Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2006-11-28
7
Medium Priority
?
3,618 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 1000 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 1000 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

971 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