Link to home
Start Free TrialLog in
Avatar of bfuchs
bfuchsFlag for United States of America

asked on

Tunning SQL question

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?
Avatar of Ammar Gaffar
Ammar Gaffar
Flag of United Arab Emirates image

Post your select statement.
Avatar of bfuchs

ASKER

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

Hi,

Exists  is always better than select if you are just doing join for filtering.
SOLUTION
Avatar of PortletPaul
PortletPaul
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bfuchs

ASKER

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

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.
Avatar of bfuchs

ASKER

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?