Solved

Query Rewrite

Posted on 2013-01-04
5
260 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 69

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 500 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Getting same value for every field in SQL 2 46
sql help 8 55
MS SQL + Insert Into Table - If Doesnt Exist 9 36
Present Absent from working date rage 11 21
This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

832 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