Solved

Tunning SQL question

Posted on 2015-01-07
9
139 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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 45

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 45

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

708 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

12 Experts available now in Live!

Get 1:1 Help Now