Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Tunning SQL question

Posted on 2015-01-07
9
Medium Priority
?
147 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
Industry Leaders: 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 52

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 70

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 52

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

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!

Question has a verified solution.

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

If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

618 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