Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Query Rewrite

Posted on 2013-01-04
5
Medium Priority
?
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
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…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

715 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