Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Query Rewrite

Posted on 2013-01-04
5
Medium Priority
?
271 Views
Last Modified: 2013-01-07
I have a query that is performing poorly and I would like suggestions on how to rewrite it without using the "not in" syntax. The query is:

SELECT
E.[ENCOUNTER_NBR]
,E.[MEMBER_NBR]
,E.[SUB_SOURCE_CD]
FROM [IRADS].[dbo].[IRAD_ENCOUNTER] E
where
E.IRADS_DATA_Source =1
and E.ENCOUNTER_KEY not in
(SELECT distinct EE.ENCOUNTER_KEY
FROM [IRADS].[dbo].[IRAD_ENCOUNTERERROR] EE
where
EE.[ERROR_KEY] in
(select ER.ERROR_KEY
from [IRADS].[dbo].[IRAD_ERROR] ER
where ER.ERROR_CD in ('X01','X02','X03','X04','X05')
and ER.[IRADS_DATA_Source] =1 )
and EE. [IRADS_DATA_Source] =1
and EE.ENCOUNTER_KEY not in
(select EEF.ENCOUNTER_KEY
from [IRADS].[dbo].[IRAD_ENCOUNTERERROR_Filtered] EEF
where EEF.IRADS_DATA_Source =1 ))
0
Comment
Question by:SharonBernal
5 Comments
 
LVL 12

Expert Comment

by:Jared_S
ID: 38745258
This just replaces the "in" with an "exists" . There may be some other way to improve on the joins that can't be found without the data.

SELECT
E.[ENCOUNTER_NBR]
,E.[MEMBER_NBR]
,E.[SUB_SOURCE_CD]

FROM [IRADS].[dbo].[IRAD_ENCOUNTER] E

where
E.IRADS_DATA_Source =1

AND

NOT EXISTS
(SELECT null FROM
[IRADS].[dbo].[IRAD_ENCOUNTERERROR] EE
WHERE EE.ENCOUNTER_KEY = E.ENCOUNTER_KEY
AND EE. [IRADS_DATA_Source] =1

AND EXISTS
(SELECT null FROM [IRADS].[dbo].[IRAD_ERROR] ER
WHERE ER.ERROR_KEY = EE.[ERROR_KEY] AND
ER.ERROR_CD in ('X01','X02','X03','X04','X05')
and ER.[IRADS_DATA_Source] =1 )

AND NOT EXISTS
(SELECT null FROM [IRADS].[dbo].[IRAD_ENCOUNTERERROR_Filtered] EEF
WHERE EEF.ENCOUNTER_KEY = EE.ENCOUNTER_KEY
AND EEF.IRADS_DATA_Source =1))
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 38745471
It seems like "IRAD_ENCOUNTERERROR_Filtered" might be a view on "IRAD_ENCOUNTERERROR" -- if so, it's likely a more extensive re-write could help further.
0
 
LVL 12

Expert Comment

by:sachitjain
ID: 38751270
Could you please tell count of records in each of following tables:
[IRADS].[dbo].[IRAD_ENCOUNTER]
[IRADS].[dbo].[IRAD_ENCOUNTERERROR]
[IRADS].[dbo].[IRAD_ERROR]
IRADS].[dbo].[IRAD_ENCOUNTERERROR_Filtered]

And also what are counts of records returned by each of following queries

select ER.ERROR_KEY
from [IRADS].[dbo].[IRAD_ERROR] ER
where ER.ERROR_CD in ('X01','X02','X03','X04','X05')
      and ER.[IRADS_DATA_Source] =1


select EEF.ENCOUNTER_KEY
from [IRADS].[dbo].[IRAD_ENCOUNTERERROR_Filtered] EEF
where EEF.IRADS_DATA_Source =1


SELECT distinct EE.ENCOUNTER_KEY
FROM [IRADS].[dbo].[IRAD_ENCOUNTERERROR] EE
where EE.[ERROR_KEY] in (select ER.ERROR_KEY
                                          from [IRADS].[dbo].[IRAD_ERROR] ER
                                          where ER.ERROR_CD in ('X01','X02','X03','X04','X05')
                                                and ER.[IRADS_DATA_Source] =1 )
      and EE. [IRADS_DATA_Source] =1
      and EE.ENCOUNTER_KEY not in
            (select EEF.ENCOUNTER_KEY
            from [IRADS].[dbo].[IRAD_ENCOUNTERERROR_Filtered] EEF
            where EEF.IRADS_DATA_Source =1 )
0
 

Author Comment

by:SharonBernal
ID: 38751676
Each of these tables millions of records. Would a left outer join be beneficial?
0
 
LVL 12

Accepted Solution

by:
Jared_S earned 2000 total points
ID: 38751806
You don't want to do that, an outer join will affect the results of your query.

What kind of indexes do you have on the tables? If your tables don't use proper indexing, that can really make performance suffer.

There is a difference between a bad performing query, and a query that takes a long time to run. You are covering a lot of data, so I'd expect that you have the latter.

Scott had a good point that has been overlooked - is IRAD_ENCOUNTERERROR_Filtered a view? If you open SQL Server Management Studio can you find it as a table?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

824 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