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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.