?
Solved

Optimizing my SQL Query - which is better?

Posted on 2016-09-14
7
Medium Priority
?
46 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
[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
  • 3
  • 3
7 Comments
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 41797828
No doubt at all for the 2nd option.
0
 
LVL 35

Expert Comment

by:ste5an
ID: 41797844
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 51

Expert Comment

by:Vitor Montalvão
ID: 41797853
My understanding is if she should have a JOIN between Patient and Patient_type or if she should use the NOT IN clause.
0
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 

Author Comment

by:Becky Edwards
ID: 41797878
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
ID: 41797882
Also Viktor:
Can you tell me why someone would use the first option?  I copied it from someone else's query.
0
 
LVL 51

Accepted Solution

by:
Vitor Montalvão earned 2000 total points
ID: 41797902
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
ID: 41797916
Thank you Vitor for your assistance.  I will make changes accordingly.  Have a great day!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Suggested Courses

771 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