Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-04-14
6
Medium Priority
?
136 Views
Last Modified: 2014-04-19
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
Comment
Question by:Starr Duskk
[X]
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
  • 5
6 Comments
 
LVL 2

Author Comment

by:Starr Duskk
ID: 40000492
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
 
LVL 2

Author Comment

by:Starr Duskk
ID: 40000493
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
 
LVL 2

Author Comment

by:Starr Duskk
ID: 40000504
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 2000 total points
ID: 40000644
Instead of doing a OR, can't you just UNION Your 2 queries?
0
 
LVL 2

Accepted Solution

by:
Starr Duskk earned 0 total points
ID: 40000699
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
 
LVL 2

Author Closing Comment

by:Starr Duskk
ID: 40010170
figured it out.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
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.

664 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