bfuchs
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.
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?
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
Now I have the following questions re performance1- 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?
Post your select statement.
ASKER
Hi, here it goes.
SELECT PatientsOpenCases_HC.* FROM PatientsOpenCases_HC ORDER BY IIf([CoveredDate]=Date(),1,2), PatientsOpenCases_HC.StartDay, PatientsOpenCases_HC.CoveredDate;
Hi,
Exists is always better than select if you are just doing join for filtering.
Exists is always better than select if you are just doing join for filtering.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
@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
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
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
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.
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?
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?