Solved

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

Posted on 2006-11-28
7
3,266 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

749 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