This is SQL code im running on SSMS 2008 R2. It's taking over 10 minutes to run (it runs against 90,000 records)..
I'm trying to update all unique records in #tmp_hic_final where [Claim Adjustment Type Code] is 0 and [Claim Type Code] is not 10. I'm also doing the update based on the select subquery which checks to make sure there isn't another record that has a [Claim Adjustment Type Code] of 1 in the table.
Although I don't know much on analyzing it, here's the execution plan: http://snag.gy/TLRsZ.jpg
is there a better way to optimize it?
update PAHT
set [Marked Final] = 'Y'
from #tmp_hic_final PAHT
join
(
select [HIC #],
[Claim Type Code] ,
[Provider Oscar #],
[Claim From Date] ,
[Claim Thru Date]
from #tmp_hic_final
where [Claim Adjustment Type Code] = 0
and [Claim Type Code] <> 10
group by [HIC #],
[Claim Type Code] ,
[Provider Oscar #],
[Claim From Date] ,
[Claim Thru Date]
--,[Claim Adjustment Type Code]
having count(*) = 1
) as PAHT_2
on PAHT.[HIC #] = PAHT_2.[HIC #] and
PAHT.[Claim Type Code] = PAHT_2.[Claim Type Code] and
PAHT.[Provider Oscar #] = PAHT_2.[Provider Oscar #] and
PAHT.[Claim From Date] = PAHT_2.[Claim From Date] and
PAHT.[Claim Thru Date] = PAHT_2.[Claim Thru Date]
where PAHT.[Claim Adjustment Type Code] = 0
and PAHT.[Claim Type Code] <> 10
and NOT EXISTS (select
[Claim Adjustment Type Code]
from [ACO].[dbo].[PA_Header_Temp]
where
[HIC #] = PAHT.[HIC #]
and [Provider Oscar #] = PAHT.[Provider Oscar #]
and [Claim Type Code] = PAHT.[Claim Type Code]
and [Claim From Date] = PAHT.[Claim From Date]
and [Claim Thru Date] = PAHT.[Claim Thru Date]
and [Claim Adjustment Type Code] = 1)
Table definition and index on PA_Header_Temp:
/****** Object: Table [dbo].['PA_Header'] Script Date: 06/02/2015 2:32:33 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[PA_Header_Temp](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Current ClaimID] [float] NULL,
[Provider OSCAR #] [nvarchar](255) NULL,
[HIC #] [nvarchar](255) NULL,
[Claim Type Code] [float] NULL,
[Claim From Date] [datetime] NULL,
[Claim Thru Date] [datetime] NULL,
[Claim Bill Facility Type Code] [float] NULL,
[Claim Bill Classification Code] [float] NULL,
[Principal Diagnosis Code] [nvarchar](255) NULL,
[Admitting Diagnosis Code] [nvarchar](255) NULL,
[Claim Medicare Non Payment Reason Code] [nvarchar](255) NULL,
[Claim Payment Amount] [float] NULL,
[Claim NCH Primary Payer Code] [nvarchar](255) NULL,
[FIPS state Code] [float] NULL,
[Bene Patient Status Code] [float] NULL,
[Diagnosis Related Group Code] [float] NULL,
[Claim Outpatient Service Type Code] [nvarchar](255) NULL,
[Facility Provider NPI #] [float] NULL,
[Operating Provider NPI #] [nvarchar](255) NULL,
[Attending provider NPI #] [float] NULL,
[Other Provider NPI #] [nvarchar](255) NULL,
[Claim Adjustment Type Code] [float] NULL,
[Claim Effective Date] [datetime] NULL,
[Claim IDR Load Date] [datetime] NULL,
[Bene Equitable BIC HICN #] [nvarchar](255) NULL,
[Claim Admission Type Code] [nvarchar](255) NULL,
[Claim Admission Source Code] [nvarchar](255) NULL,
[Claim Bill Frequency Code] [nvarchar](255) NULL,
[Claim Query Code] [float] NULL,
[Marked Final] [nvarchar](255) NULL,
[Load Date] [datetime] NULL,
PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
I suggest this approach from both a readability and performance standpoint.
where whatever
should be the same every time.