Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2006-11-28
7
3,213 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Nowadays, some of developer are too much worried about data. Who is using data, who is updating it etc. etc. Because, data is more costlier in term of money and information. So security of data is focusing concern in days. Lets' understand the Au…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

807 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