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

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 139
  • Last Modified:

EXISTS with AND/OR is killing me softly ....

I am using SQL Server 2008 R2. I have an EXISTS with an OR EXISTS... When I use either one by itself, it takes less than a second. But when the two are together as an OR, it takes 54 seconds to run. Here is the clause that's killing me:

 AND ( 
  EXISTS ( 
 select 1 
 from EmployeeUnitJobTypeUnitView 
    INNER JOIN Unit ON Unit.UnitId = EmployeeUnitJobTypeUnitView.UnitId  
  where EmployeeUnitJobTypeUnitView.EmployeeId = Employee.EmployeeId 
      AND Unit.StateCode  
 IN (219) 
 )  
 OR EXISTS ( 
 select 1 
 from EmployeeAccessZoneDistrictUnitView 
    INNER JOIN Unit ON Unit.UnitId = EmployeeAccessZoneDistrictUnitView.UnitId  
  where EmployeeAccessZoneDistrictUnitView.EmployeeId = Employee.EmployeeId 
      AND Unit.StateCode 
 IN (219) 
 )  

Open in new window


Am I doing something wrong on my AND/OR?

BTW, this query, when completed, returns 1 row. The top half returns one row, the second have returns 0 rows.

thanks!
P.S. Just for kicks I also tried an IN and it gives the same amount of time:

AND 
 ( 
  Employee.EmployeeId IN  
    (Select EmployeeId from EmployeeUnitJobTypeUnitView 
    INNER JOIN Unit ON Unit.UnitId = EmployeeUnitJobTypeUnitView.UnitId  
      AND Unit.StateCode  
 IN (219) )
 OR 
  Employee.EmployeeId IN  
    (Select EmployeeId from EmployeeAccessZoneDistrictUnitView  
    INNER JOIN Unit ON Unit.UnitId = EmployeeAccessZoneDistrictUnitView.UnitId  
      AND Unit.StateCode 
 IN (219) )
 ) 

Open in new window

0
Starr Duskk
Asked:
Starr Duskk
  • 5
2 Solutions
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
Additional information:

Just to show stuff at the top and an entire query, this simple one (without further AND clauses for filtering), took 2:33. It returns 3 rows:

/* exists */

SELECT Distinct Employee.EmployeeId
FROM Employee 
 where 
 Employee.ClientId = 1  
 
 AND ( 
  EXISTS ( 
 select 1 
 from EmployeeUnitJobTypeUnitView 
    INNER JOIN Unit ON Unit.UnitId = EmployeeUnitJobTypeUnitView.UnitId  
  where EmployeeUnitJobTypeUnitView.EmployeeId = Employee.EmployeeId 
      AND Unit.StateCode  
 IN (219) 
 )  
 OR EXISTS ( 
 select 1 
 from EmployeeAccessZoneDistrictUnitView 
    INNER JOIN Unit ON Unit.UnitId = EmployeeAccessZoneDistrictUnitView.UnitId  
  where EmployeeAccessZoneDistrictUnitView.EmployeeId = Employee.EmployeeId 
      AND Unit.StateCode 
 IN (219) 
 )  
 )  

Open in new window

0
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
This query, with a FULL OUTER JOIN and a coalesce took less than a second and returns the same 3 rows:

SELECT Distinct COALESCE(table1.EmployeeId, table2.EmployeeId) AS EmployeeId
from 
(SELECT Distinct Employee.EmployeeId
FROM Employee
 where 
 Employee.ClientId = 1  
 AND 
  EXISTS ( 
 select 1 
 from EmployeeUnitJobTypeUnitView 
    INNER JOIN Unit ON Unit.UnitId = EmployeeUnitJobTypeUnitView.UnitId  
  where EmployeeUnitJobTypeUnitView.EmployeeId = Employee.EmployeeId 
      AND Unit.StateCode  
 IN (219) 
 )
 ) AS TABLE1
 FULL OUTER JOIN (
  SELECT Distinct Employee.EmployeeId
FROM Employee
 where 
 Employee.ClientId = 1  

 AND EXISTS ( 
 select 1 
 from EmployeeAccessZoneDistrictUnitView 
    INNER JOIN Unit ON Unit.UnitId = EmployeeAccessZoneDistrictUnitView.UnitId  
  where EmployeeAccessZoneDistrictUnitView.EmployeeId = Employee.EmployeeId 
      AND Unit.StateCode 
 IN (219) 
 )  
 )  table2 ON
    table2.Employeeid = table1.Employeeid
ORDER BY
    Employeeid

Open in new window

0
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
Oh, and you might ask, "Well, why don't you just use the FULL OUTER JOIN?"

Because when building this query based on search/filter values, I could have a lot of these AND EXISTS/OR EXISTS on various tables. (Six to be exact.)

So not sure how I would pull that off with 6 different ones versus 6 different ones. Because this AND/OR has to filter, then this next AND/OR has to filter, etc.

thanks.
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 
Éric MoreauSenior .Net ConsultantCommented:
Instead of doing a OR, can't you just UNION Your 2 queries?
0
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
Thanks for the suggestion..

I think I just figured it out though. If I create a view with my coalesce, I can use it in my queries and it is zipping fast and I don't have to worry about building it as I build my query.

thanks!
0
 
Starr DuskkASP.NET VB.NET DeveloperAuthor Commented:
figured it out.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now