Subselects V slow.

Hi

Running on SQL Server 2008

I have a large (ish) query with 4 sub selects in the where clause:

    And vw.id not In (Select Appointment_ID From AppointmentEvent Where isnull(ArrivalTime,0) <> 0 And isnull(CompleteTime,0) <> 0)
    And vw.id not In (Select Appointment_ID From AppointmentEvent Where Event_ID = -11 And isnull(RequestTime,0) <> 0) --Exclude Return to wait
    And vw.id not In (Select Appointment_ID From AppointmentEvent Where isnull(RequestTime,0) <> 0 And isnull(CompleteTime,0) = 0 And Event_ID > 0)--Exclude Any User defined Activity
    And vw.id not In (Select Appointment_ID From AppointmentEvent Where Event_ID = -3 And Location_ID = @Location_ID) -- Exclude patients where call has already been made
    

Open in new window


These ran OK until we got to about 14000 rows in AppointmentEvent  table. Because we are looking for rows NOT IN the table is there a more efficient way to do this.

Would Joins be better?

Andy
LVL 3
Andy GreenAsked:
Who is Participating?
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
Why not use a single subselect?
And vw.id not In (Select Appointment_ID 
				From AppointmentEvent 
				Where (isnull(ArrivalTime,0) <> 0 And isnull(CompleteTime,0) <> 0)
					Or (Event_ID = -11 And isnull(RequestTime,0) <> 0) --Exclude Return to wait
					Or (isnull(RequestTime,0) <> 0 And isnull(CompleteTime,0) = 0 And Event_ID > 0) --Exclude Any User defined Activity
					Or (Event_ID = -3 And Location_ID = @Location_ID)) -- Exclude patients where call has already been made

Open in new window


Would Joins be better?
Depends. We need to see the full SELECT that you are running and also know more about the tables (PK and FK).
0
 
Andy GreenAuthor Commented:
Thanks Vitor

For now this combining has worked, and since this is a production environment, I'm going to run with this for now.

Thanks again saved my *rse.

Andy
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.