Avatar of Andy Green
Andy Green
Flag for United Kingdom of Great Britain and Northern Ireland asked on

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
Microsoft SQL Server

Avatar of undefined
Last Comment
Andy Green

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Vitor Montalvão

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Andy Green

ASKER
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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck