Solved

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

Posted on 2006-11-28
7
3,031 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
Backup Your Microsoft Windows Server®

Backup 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduced in Microsoft SQL Server 2005, the Copy Database Wizard (http://msdn.microsoft.com/en-us/library/ms188664.aspx) is useful in copying databases and associated objects between SQL instances; therefore, it is a good migration and upgrade tool…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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 UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

709 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now