Solved

Tunning SQL question

Posted on 2015-01-07
9
143 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 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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
 
LVL 48

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:Scott Pletcher
Scott Pletcher 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 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 48

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.

789 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