?
Solved

Tunning SQL question

Posted on 2015-01-07
9
Medium Priority
?
146 Views
Last Modified: 2015-02-08
Hi Experts,
I have an access app linked to SQL BE, and using the following code as part of a dynamic build filter.
    If Not IsNull(Me.ComboSupervisor) Then
        sWhere = sWhere & " And PatientID in (Select id from Patients where supervisor = '" & ComboSupervisor & "')"
    End If
    If Not IsNull(Me.ComboCounty) Then
        sWhere = sWhere & " And PatientID in (Select id from Patients where County = '" & ComboCounty & "')"
    End If
    If Not IsNull(Me.ComboGender) Then
        sWhere = sWhere & " And PatientID in (Select id from Patients where Gender= '" & ComboGender & "')"
    End If

Open in new window

Now I have the following questions re performance
1- would exists be a better choice than using in select?
2- if yes what is the correct sql syntax, I tried PatientID exists and didn't work?
3- Is it advisable to program that if user selects all 3 conditions they should all be included in one "in select" statement?
0
Comment
Question by:bfuchs
[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
9 Comments
 
LVL 12

Expert Comment

by:Ammar Gaffar
ID: 40537147
Post your select statement.
0
 
LVL 4

Author Comment

by:bfuchs
ID: 40537152
Hi, here it goes.
SELECT PatientsOpenCases_HC.* FROM PatientsOpenCases_HC ORDER BY IIf([CoveredDate]=Date(),1,2), PatientsOpenCases_HC.StartDay, PatientsOpenCases_HC.CoveredDate;

Open in new window

0
 
LVL 15

Expert Comment

by:Vikas Garg
ID: 40537165
Hi,

Exists  is always better than select if you are just doing join for filtering.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 400 total points
ID: 40537471
Use a JOIN to the patients table, e.g.

SELECT PatientsOpenCases_HC.*
FROM PatientsOpenCases_HC
INNER JOIN Patients on PatientsOpenCases_HC.PatientID = Patients.ID
WHERE 

-- now add the required conditions
-- Patients.supervisor = '" & ComboSupervisor & "')"
-- Patients.County = '" & ComboCounty & "')"
-- Patients.Gender= '" & ComboGender & "')"

ORDER BY IIf([CoveredDate] = DATE (), 1, 2)
 , PatientsOpenCases_HC.StartDay
 , PatientsOpenCases_HC.CoveredDate;

Open in new window


This should perform better than both IN() or EXISTS()
0
 
LVL 51

Accepted Solution

by:
Vitor Montalvão earned 1200 total points
ID: 40537529
You can search all criteria in a single subselect:
    
If Not IsNull(Me.ComboSupervisor) Or Not IsNull(Me.ComboCounty) Or Not IsNull(Me.ComboGender) Then
    sWhere = sWhere & " And PatientID in (Select id from Patients where "
    sCriteria = ""
    If Not IsNull(Me.ComboSupervisor) Then
        sCriteria = "supervisor = '" & ComboSupervisor & "'"
    End If
    If Not IsNull(Me.ComboCounty) Then
        If sCriteria = "" Then
            sCriteria = sCriteria & "County = '" & ComboCounty & "'"
        Else
             sCriteria = sCriteria & " And County = '" & ComboCounty & "'"
        End If
    End If
    If Not IsNull(Me.ComboGender) Then
        If sCriteria = "" Then
            sCriteria = sCriteria & "Gender= '" & ComboGender & "'"
        Else
             sCriteria = sCriteria & " And Gender= '" & ComboGender & "'"
        End If
    End If
    sWhere = sWhere & sCriteria & ")"
End If

Open in new window

0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 400 total points
ID: 40538298
1.  Yes, EXISTS is better.
2.  Here's the general structure of an EXISTS WHERE condition:
     SELECT ...
     FROM outer_table ot
     WHERE EXISTS(SELECT 1 FROM Patients p WHERE p.id = ot.PatientID AND supervisor = ...)
or "county = ..." or whatever the other condition is.
0
 
LVL 4

Author Comment

by:bfuchs
ID: 40538952
Hi Experts,
First thanks to all for replying.
@PortletPaul,
The problems I have with your suggestion is
1- this form is a continuous form where the top section is designated for filtering and the rest displays the data, the user selects a set of records they want to deal with, and then do all types of work, adding editing deleting etc, its not meant only for viewing.
2- I only copied one portion of the code building the filter string, however there are more parts, which some of them involves filtering for values in other tables, for example
    For Each v In Me.ListLanguages.ItemsSelected
        sin = sin & ",'" & Me.ListLanguages.ItemData(v) & "'"
    Next
    If Len(sin) > 0 Then
        sWhere = sWhere & " And PatientID in (Select PatientsID from PatientsLanguages_HC where language in (" & Mid(sin, 2) & "))"
        sin = ""
    End If

Open in new window

how would I deal with all those, I cant be changing the base table join for each type of option user may select, or rather conjunction of types?

@Vitor Montalvão,
Something like that I had in my mind to apply, thanks for pre-designing it for me, however just wonder if that would make a difference on performance?

@Scott,
Thanks, I did that and so far in my testing it reduced up to 33% of the execution time.
this is what I currently have
If Not IsNull(Me.ComboSupervisor) Then
        sWhere = sWhere & " And exists (Select 1 from Patients where ID = PatientsOpenCases_HC.PatientID and supervisor = '" & ComboSupervisor & "')"
    End If
    If Not IsNull(Me.ComboCounty) Then
        sWhere = sWhere & " And exists (Select 1 from Patients where ID = PatientsOpenCases_HC.PatientID and County = '" & ComboCounty & "')"
    End If
    If Not IsNull(Me.ComboGender) Then
        sWhere = sWhere & " And exists (Select 1 from Patients where ID = PatientsOpenCases_HC.PatientID and Gender = '" & ComboGender & "')"
    End If

Open in new window

0
 
LVL 51

Expert Comment

by:Vitor Montalvão
ID: 40539663
Something like that I had in my mind to apply, thanks for pre-designing it for me, however just wonder if that would make a difference on performance?
Yes, will make difference because you are going to perform a single subselect instead of 3 as you have now.
0
 
LVL 4

Author Comment

by:bfuchs
ID: 40543482
Hi Experts,
Before finalize this post I just want to double check if someone have a way I could make PortletPaul's suggestion work on my case, as this if possible would likely be the best option re performance?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

752 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