Solved

Tunning SQL question

Posted on 2015-01-07
9
140 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
9 Comments
 
LVL 12

Expert Comment

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

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 14

Expert Comment

by:Vikas Garg
ID: 40537165
Hi,

Exists  is always better than select if you are just doing join for filtering.
0
 
LVL 48

Assisted Solution

by:PortletPaul
PortletPaul earned 100 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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 46

Accepted Solution

by:
Vitor Montalvão earned 300 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:ScottPletcher
ScottPletcher earned 100 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 3

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 46

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 3

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now