Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
DB Engine
SQL ServerMSDESQL Server CE
Services
Analysis (Data Mining)Analysis (OLAP)DTSIntegration ServicesNotification ServicesReporting Services
Programming
CLRConnectivitySQLXML
Other Technologies
ClusteringEnglish QueryFull-Text SearchReplicationService Broker
General
Data WarehousingPerformanceSecuritySetupSQL Server ToolsOther SQL Server Topics
DirectoryUser Groups
Related Topics
MS AccessOther DB ProductsMS Server Products.NET DevelopmentVB DevelopmentJava DevelopmentMore Topics ...

SQL Server Forum / DB Engine / SQL Server / March 2008

Tip: Looking for answers? Try searching our database.

Killer Union

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Roy Sinclair - 12 Mar 2008 21:03 GMT
I have two queries joined with a union, one query by itself takes 34ms to
complete and the other runs by itself  in 340ms but when they are joined by
the union (or a union all) the combined query takes an incredible one minute
and 15 seconds.  Why does the union com with such an incredible cost?

The query is:

select
        U.[Name] COLLATE SQL_Latin1_General_CP1_CI_AS as UserID
        ,U.LastName COLLATE SQL_Latin1_General_CP1_CI_AS + ', ' + U.FirstName
COLLATE SQL_Latin1_General_CP1_CI_AS + ' ' + U.MiddleName COLLATE
SQL_Latin1_General_CP1_CI_AS + ' (' + P.PartnerName COLLATE
SQL_Latin1_General_CP1_CI_AS + ')' as UserName
from Team..Users U with (nolock)
Left Join vwTeamPartners P with (nolock) on U.ID = P.UserID
where P.UserID is not null and Len(U.Name) = 6 and Len(U.FirstName) > 0 and
Len(U.LastName) > 0 and Lower(Substring(U.Name,1,1)) ='v' and
IsNumeric(Substring(U.Name,2,5))=1
union all
select
        Case Len(E.EmplID)
            When 6 then E.EmplID
            When 5 then 'C' + E.Emplid
            else null
            end as UserID
        ,E.Full_Name + ' (' + E.DeptID + ')' as UserName
from vwPS_Employees E with (nolock)
left join vwTeamUsers T with (nolock) on
        Case Len(E.EmplID)
            When 6 then E.EmplID
            When 5 then 'C' + E.EmplID
            end = T.[Name] collate database_default
where E.Empl_Status in('A','P','L','S') and E.DeptID <> '000' and
Len(E.EmplID) in (5,6) and T.[ID] is not null
Order by UserName
Aaron Bertrand [SQL Server MVP] - 12 Mar 2008 21:26 GMT
We are not really going to be able to tell why, unless we can see the view
statements, structure of base tables, query plans, etc.  I do have a couple
of questions though... why all the collate clauses?  Why not let the front
end deal with parentheses, concatenation, etc.?   Why left join with
vwTeamPartners and then make it an inner join by including it in the where
clause?  Why left join with vwTeamUsers and then make it an inner join by
including it in the where clause?

>I have two queries joined with a union, one query by itself takes 34ms to
> complete and the other runs by itself  in 340ms but when they are joined
[quoted text clipped - 34 lines]
> Len(E.EmplID) in (5,6) and T.[ID] is not null
> Order by UserName
Randy Pitkin - 13 Mar 2008 04:46 GMT
UNIONS and anything but Inner joins are always expensive.
There is alwasy a better way to do it, as long as you are using stored
procedures as the method of access.
If you are not, then you have bigger problems

The biggest issue is that both selects have to complete in entirity before
the union can begin.

Things I noticed about your Query:
 Your Collates are in series in the same column of the select.
  Only the last one would count, and it is the default for SQL.
  They should be omitted.
 The only time Collate is normally seen is when you have different
collations in the return from multiple linked servers.

Performance Hit 2 )
 always specify the schema, Database..Table Only works if the only schema
is dbo.
  It forces QA to check the sys.objects table for table ownership and
access

WAIT WAIT WAIT
 Your using a case statment in a join ??
 Your Joining to Views, I bet they are well written as this one.
 Did you put indexes on your views.
 If we are Left joining P but P.userid can't be null, THAT's AN Inner

 I understand now this is an example of how to get a 3 minute execution on
2 tables with 2 rows of data each.

Hire A DBA

SELECT

U.[Name] as UserID

, U.LastName + ', ' + U.FirstName + ' ' + U.MiddleName + ' (' +
P.PartnerName + ')' as UserName

FROM

Team..Users U with (nolock)

Left Join vwTeamPartners P with (nolock) on U.ID = P.UserID

WHERE

P.UserID is not null

and Len(U.Name) = 6

and Len(U.FirstName) = 0

and Len(U.LastName)=0

and Lower(Substring(U.Name,1,1)) ='v'

and IsNumeric(Substring(U.Name,2,5))=1

UNION ALL

SELECT

Case Len(E.EmplID)

When 6 then E.EmplID

When 5 then 'C' + E.Emplid

else null

end as UserID

,E.Full_Name + ' (' + E.DeptID + ')' as UserName

from

vwPS_Employees E with (nolock)

left join vwTeamUsers T with (nolock) on

Case Len(E.EmplID)

When 6 then E.EmplID

When 5 then 'C' + E.EmplID

end = T.[Name]

where

E.Empl_Status in('A','P','L','S')

and E.DeptID < '000'

and Len(E.EmplID) in (5,6)

and T.[ID] is not null

Order by

UserName

> We are not really going to be able to tell why, unless we can see the view
> statements, structure of base tables, query plans, etc.  I do have a
[quoted text clipped - 42 lines]
>> Len(E.EmplID) in (5,6) and T.[ID] is not null
>> Order by UserName
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.