Solved

Optimizing my SQL Query - which is better?

Posted on 2016-09-14
7
31 Views
Last Modified: 2016-09-14
I have two ways of doing this query, and would like the experts advice on which is better in terms of performance.

One where statement that says
 pe.PAT_ID NOT IN
  (SELECT PAT_ID FROM Patient_type WHERE PATIENT_TYPE_C IN  (1,2))

Open in new window


or
pt.PATIENT_TYPE_C not in  (1,2)

Open in new window

--where I have to add the table connections in the FROM clause of the main query
0
Comment
Question by:Becky Edwards
  • 3
  • 3
7 Comments
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
No doubt at all for the 2nd option.
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
Sorry, but this question makes no senses, cause both options refer to different table alias names.

Please post a concise and complete example. In your case the entire queries.
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
Comment Utility
My understanding is if she should have a JOIN between Patient and Patient_type or if she should use the NOT IN clause.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Becky Edwards
Comment Utility
Thank you Vitor for clarifying.
   Some coworkers said I should not use the Not In Clause, which enables me to filter out certain patients without pulling in the table PATIENT_TYPE into the main query itself.  

So instead of doing this in the where clause:
PAT_ENC.PAT_ID NOT IN
  (SELECT PAT_ID FROM Patient_type WHERE PATIENT_TYPE_C IN  (1,2))

Open in new window


I would do this:
IN THE FROM CLAUSE
Add left outer join PATIENT_TYPE            pt ON PAT_ENC.PAT_ID=pt.PAT_ID
and this:
IN THE WHERE CLAUSE
pt.PATIENT_TYPE_C not in  (1,2)
0
 

Author Comment

by:Becky Edwards
Comment Utility
Also Viktor:
Can you tell me why someone would use the first option?  I copied it from someone else's query.
0
 
LVL 45

Accepted Solution

by:
Vitor Montalvão earned 500 total points
Comment Utility
I think you don't need the LEFT OUTER JOIN since you're filtering by PATIENT_TYPE_C. An INNER JOIN should be better here.

Can you tell me why someone would use the first option?
Because you're not getting the benefit of the relationship between the two tables (PK and FK that you'll use in the JOIN clause).
0
 

Author Closing Comment

by:Becky Edwards
Comment Utility
Thank you Vitor for your assistance.  I will make changes accordingly.  Have a great day!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now