I have an engineering practice of SQL Optimization problem, which I think is a typical case ,and will help a lot of guys.
SQL SERVER 2005,
Firstly, create the main table. This is a person info table.
CREATE TABLE [dbo].[OLAPAgentDim](
[RoleID] [varchar](50) NULL CONSTRAINT [DF_OLAPAgentDim_RoleID] DEFAULT ((1)),
[OLAPKey] [bigint] IDENTITY(1,1) NOT NULL,
[FatherKey] [bigint] NULL,
[FatherKeyValue] [nvarchar](100) NULL,
[System] [varchar](6) NULL,
[Level] [int] NULL,
[IfLeaf] [real] NULL,
[IfDel] [real] NULL CONSTRAINT [DF_OLAPAgentDim_IfDel] DEFAULT ((0)),
[SourceKey] [varchar](50) NULL,
[MainDemoName] [nvarchar](100) NULL,
[FastCode] [varchar](50) NULL,
[TagValue] [varchar](50) NULL,
[Script] [nvarchar](max) NULL,
[Birthday] [datetime] NULL,
[EarlyStartTime] [datetime] NULL,
[StartTime] [datetime] NULL,
[EndTime] [datetime] NULL,
[EditTime] [datetime] NULL,
[BecomesTime] [datetime] NULL,
[ContractTime] [datetime] NULL,
[ContractEndTime] [datetime] NULL,
[XMLIcon] [nvarchar](max) NULL,
[PassKey] [varchar](50) NULL CONSTRAINT [DF_OLAPAgentDim_PassKey] DEFAULT ('N3pkY3RHaeZXA9mGJdfm8A=='),
[Address] [nvarchar](100) NULL,
[HomeTel] [varchar](50) NULL,
[Mobile] [varchar](50) NULL,
[Email] [varchar](100) NULL,
[IDCard] [varchar](50) NULL,
[IDSecu] [varchar](50) NULL,
[IDEndowment] [varchar](50) NULL,
[IDAccumulation] [varchar](50) NULL,
[ContactPerson] [nvarchar](100) NULL,
[ContactPersonTel] [varchar](50) NULL,
[Others1] [varchar](50) NULL,
[SexKey] [varchar](2) NULL CONSTRAINT [DF_OLAPAgentDim_SexKey] DEFAULT ((1)),
[SexKeyValue] [nvarchar](100) NULL,
[MarrageKey] [varchar](2) NULL CONSTRAINT [DF_OLAPAgentDim_MarrageKey] DEFAULT ((1)),
[MarrageKeyValue] [nvarchar](100) NULL,
[Nation] [nvarchar](50) NULL,
[Race] [nvarchar](50) NULL,
[PartyMemberKey] [varchar](2) NULL CONSTRAINT [DF_OLAPAgentDim_PartyMemberKey] DEFAULT ((1)),
[PartyMemberKeyValue] [nvarchar](100) NULL,
[RegionKey] [bigint] NULL CONSTRAINT [DF_OLAPAgentDim_RegionKey] DEFAULT ((1)),
[RegionKeyValue] [nvarchar](100) NULL,
[LeaveResonKey] [bigint] NULL CONSTRAINT [DF_OLAPAgentDim_LeaveResonKey] DEFAULT ((1)),
[LeaveResonKeyValue] [nvarchar](100) NULL,
[RoleStr] [varchar](max) NULL,
[RoleStrValue] [nvarchar](max) NULL,
[LeaderKey] [bigint] NULL CONSTRAINT [DF_OLAPAgentDim_LeaderKey] DEFAULT ((1)),
[LeaderKeyValue] [nvarchar](100) NULL,
[FastCode2] [varchar](50) NULL,
[FastCode3] [varchar](50) NULL,
[FastCode4] [varchar](50) NULL,
[FastCode5] [varchar](50) NULL,
[OtherAddress] [nvarchar](100) NULL,
[ShowOrder] [int] NULL,
[RaceKey] [bigint] NULL DEFAULT ((1)),
[RaceKeyValue] [nvarchar](100) NULL,
[DepartLevelKey] [bigint] NULL DEFAULT ((1)),
[DepartLevelKeyValue] [nvarchar](100) NULL,
[forumname] [nvarchar](100) NULL,
[IfCloseKey] [bigint] NULL DEFAULT ((1)),
[IfCloseKeyValue] [nvarchar](100) NULL,
[InsureStartTime] [datetime] NULL,
[AccumulationStartTime] [datetime] NULL,
[Rate] [varchar](50) NULL,
[DirectLeaderKey] [bigint] NULL CONSTRAINT [DF_OLAPAgentDim_DirectLeaderKey] DEFAULT ((1)),
[DirectLeaderAttriKey] [bigint] NULL CONSTRAINT [DF_OLAPAgentDim_DirectLeaderAttriKey] DEFAULT ((1)),
[DirectLeaderKeyValue] [nvarchar](100) NULL,
[DirectLeaderSourceKey] [varchar](50) NULL,
[DirectLeaderPartName] [nvarchar](100) NULL,
[DirectLeaderPositionName] [nvarchar](100) NULL,
[NOTSync] [int] NULL,
[FatherPath] [nvarchar](max) NULL,
[SaleDiscount] [real] NULL,
CONSTRAINT [PK_OLAPAgent Dim] PRIMARY KEY CLUSTERED
(
[OLAPKey] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Secondly, insert abount 10,000 record into the table. I think 10,000 record is not a very big number to SQL SERVER. You can see this is a father and children dimention table in fact. The records with ifleaf=0 means the person's department structure node, the records with ifleaf=1 means the person. You can define father-children relationship using FahterKey column. For Example:
OLAPKey IfLeaf FatherKey DepartLevelKey MainDemoName
2 0 0 1 IBM Company
3 0 2 2 Sales Depart
4 0 2 2 Service Depart
5 0 3 3 Sales Team1
6 1 5 NULL John Smith
7 1 4 NULL Mary
......
DepartLevelKey Column means the depart node's level.
So in this table, we can save the whole HR tree info.
Thirdly, we see the problem SQL:
create table #t
(
TableID int IDENTITY(1,1),
OLAPKey bigint,
MainDemoName nvarchar(max)
)
declare #t4 table
(
TableID int IDENTITY(1,1),
MainDemoName nvarchar(max),
OLAPKeystr varchar(100)
)
declare #agentkey bigint
set #agentkey ='2'
--Part A
--DepartLevelKey=2, to get #agentkey node's all level=2 department
;WITH Result AS(
SELECT OLAPKey,DepartLevelKey,maindemoname FROM OLAPAgentDim WHERE OLAPKey =#agentkey
UNION ALL
SELECT a.OLAPKey,a.DepartLevelKey,a.maindemoname FROM OLAPAgentDim AS a,Result AS b WHERE a.FatherKey = b.OLAPKey
)
insert #t select OLAPKey,maindemoname from Result where DepartLevelKey=4
--Part B
;with One as
(
select *,convert(varchar(50),OLAPKey) as Re from #t
)
insert #t4 select maindemoname,stuff((select ','+Re from One where One.maindemoname=#t.maindemoname for xml path('')),1,1,'') as Two
from #t
group by maindemoname
drop table #t
The SQL above is divided into Part A and Part B.
Part A SQL get all the childrens below a root node(and filtered those belong to the specified DepartLevelKey). For example, to get all persons in Sales Department's child-department with level=3.
Part B SQL change the rows to column, For example:
Change:
TableID OLAPKey MainDemoName
1 6 Sales Team1
2 10 Sales Team1
3 12 Sales Team1
to:
TableID MainDemoName OLAPKeystr
1 Sales Team1 6,10,12
Thus we get each goal department's persons, for further processing(omited here).
The Problem:
The Part A is very slow, cost about 5 minutes. The Part B is slow too.
I wonder how to optimize it basing the table struc existed.
yours,
Ivan
Try:
(i) Adding this index to OLAPAgentDim:
create index IX_OLAPAgentDim_FatherKey on OLAPAgentDim (FatherKey) include (DepartLevelKey, MainDemoName)
(ii) Changing MainDemoName in #t from nvarchar(max) to nvarchar(100). This matches the column definition in OLAPAgentDim.
(iii) Between Part A and Part B, i.e. after Part A and before Part B, adding this index to #t:
create clustered index IX on #t (MainDemoName)
Related
I have an issue with one of my MSSQL tables. The table has 1.2 Trillion rows about 1TB of data and growing. The table is partitioned into 8000 partitions, only about 800 are used. The others were created for expanded growth.
Inserts and selects are < 1s
My problem is Updates are very slow. To updated 1 record varchar(100) using the exact partition key and Identity Column key, it is 3s.
If I place the update code in a Stored Procedure, goes in less than 1s. If I add option (recompile), about 1s. Is there a way to fix this without adding recomile?
Thank you.
CREATE TABLE [dbo].[_tabletest](
[data_id] [bigint] IDENTITY(10000,1) NOT NULL,
[idx1_id] [smallint] NOT NULL,
[idx2_id] [bigint] NOT NULL,
[idx3_id] [bigint] NOT NULL,
[template_id] [bigint] NOT NULL,
[reference_id] [varchar](200) NOT NULL,
[data] [nvarchar](400) NULL,
[data_1] [tinyint] NULL,
[data_2] [varchar](max) NULL,
[data_3] [real] NULL,
[data_4] [tinyint] NULL,
[data_5] [tinyint] NULL,
[data_6] [bit] NULL,
[data_7] [nvarchar](50) NULL,
[data_8] [datetime] NULL,
[data_9] [nvarchar](50) NULL,
[data_10] [varchar](100) NULL,
[data_11] [varchar](100) NULL,
[data_12] [varchar](100) NULL,
[data_13] [varchar](300) NULL,
[data_14] [varchar](200) NULL,
[data_15] [uniqueidentifier] NULL,
[data_16] [varchar](600) NULL,
[data_17] [varchar](100) NULL,
[data_18] [varchar](100) NULL,
[data_19] [decimal](10, 5) NULL,
[data_20] [decimal](10, 5) NULL,
[data_21] [decimal](10, 5) NULL,
[data_22] [decimal](10, 5) NULL,
[data_23] [decimal](10, 5) NULL,
[data_24] [int] NULL,
[data_25] [int] NULL,
[data_26] [int] NULL,
[data_27] [int] NULL,
[data_28] [decimal](10, 5) NULL,
[data_29] [decimal](10, 5) NULL,
[data_30] [decimal](10, 5) NULL,
[data_31] [datetime] NULL,
[data_32] [decimal](10, 5) NULL,
[data_3] [bit] NULL,
[data_34] [varchar](max) NULL,
[data_35] [smallint] NULL,
[data_36] [bigint] NULL,
[data_37] [int] NULL,
[data_38] [real] NULL,
[data_39] [datetime] NULL,
[data_40] [varchar](2500) NULL,
CONSTRAINT [PK_data_id] PRIMARY KEY CLUSTERED
(
[idx1_id] ASC,
[idx2_id] ASC,
[idx3_id] ASC,
[data_id] ASC
)WITH (PAD_INDEX = ON, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90)
)
GO
ALTER TABLE [dbo].[_tabletest] SET (LOCK_ESCALATION = AUTO)
GO
ALTER TABLE [dbo].[_tabletest] ADD CONSTRAINT [DF_1] DEFAULT ((0)) FOR [data_1]
GO
ALTER TABLE [dbo].[_tabletest] ADD CONSTRAINT [DF_2] DEFAULT (getutcdate()) FOR [data_39]
GO
ALTER TABLE [dbo].[_tabletest] ADD DEFAULT (newid()) FOR [data_15]
GO
UPDATE [_tabletest] SET [data_40] = 'test-data'
WHERE [idx1_id] = 1209 AND [idx2_id] = 113795 AND [idx3] = 41195716 AND [data_id] = 1329110156
;
I have the following query that will be used to fetch data from legacy tables. It's no surprise but the amount of data is huge and thus it takes a long time. The first select takes 40 minutes to run using an empty dbo.commodities_copy table as a starting point and yields around 26,000 rows. Keep in mind that there are separate databases: STAGING and PRESTAGING and that some joins are made using non-PK fields, which is most definately making an impact in its performance. This is something that I cannot fix, due to the way data was organized from the start. Also the transaction table has around 1 million rows, which also impacts heavily on performance. The entire script takes a total of 3.5 hours to execute when using an EMPTY dbo.commodities_copy table. I have not tested on insertion to a table with data.
The goal of the query is to get commodity information from the transaction table (if you guessed this was supposed to be noSQL data, you guessed right) and if the commodity code exists in the commodity table, do not insert a commodity in it.
The group bys are absolutely needed to get around duplicates, since a transactions may share the same commodity. The commodity code should be unique in the commodities table, but currently it is not - though if it helps, it's possible we could alter it.
What can I do to speed it up?
INSERT INTO STAGING.dbo.commodities_copy
(commodity_code,
short_description_sched_b,
short_description_sched_tsusa,
long_description_sched_b,
long_description_sched_tsusa,
measurement_unit_1_sched_b,
measurement_unit_1_sched_tsusa,
measurement_unit_2_sched_b,
measurement_unit_2_sched_tsusa,
end_use_sched_b,
end_use_sched_tsusa,
year,
created_by,
created_on,
taxable_sched_b,
taxable_sched_tsusa,
non_taxable_sched_b,
non_taxable_sched_tsusa,
fk_sic_sched_b,
fk_sic_sched_tsusa,
chapter,
header,
sub_header,
needs_validation)
SELECT
--Distinct
Commodity_Code,
iif(miob2.DESC_COMM is null, UPPER(socrata.Commodity_Short_Name), miob2.DESC_COMM) as short_commmodity_description_b,
iif(mio2tsusa.DESC_COMM is null, UPPER(socrata.Commodity_Short_Name), mio2tsusa.DESC_COMM) as short_commmodity_description_tsusa,
socrata.Commodity_description as long_commodity_description_b,
socrata.Commodity_description as long_commodity_description_tsusa,
iif(miob2.UNIDAD is null, socrata.unit_1, miob2.UNIDAD) as unit_1_b,
iif(mio2tsusa.UNIDAD is null, socrata.unit_1, mio2tsusa.UNIDAD) as unit_1_tsusa,
MAX(socrata.unit_2) as unit_2_b,
MAX(socrata.unit_2) as unit_2_tsusa,
socrata.end_use_e as end_use_b,
socrata.end_use_i as end_use_tsusa,
MAX(socrata.[year]),
'system' as created_by,
getdate() as created_on,
miob.TRIBUTA as taxable_b,
miotsusa.TRIBUTA as taxable_tsusa,
miob.NTRIBUTA as non_taxable_b,
miotsusa.NTRIBUTA as non_taxable_tsusa,
sicb.id as sic_id_b,
sictsusa.id as sic_id_tsusa,
SUBSTRING(Commodity_Code, 1, 2) as chapter,
SUBSTRING(Commodity_Code, 1, 4) as header,
SUBSTRING(Commodity_Code, 1, 6) as sub_header,
0 as needs_validation
FROM PRE_STAGING.dbo.TRANSACTIONS_FROM_SOCRATA socrata
Left join PRE_STAGING.DBO.MIOB_TBL miob ON miob.COMM=socrata.Commodity_Code
Left join PRE_STAGING.dbo.MSCHB_TBL miob2 ON miob2.COMM=socrata.Commodity_Code
Left join PRE_STAGING.dbo.MIOTSUSA_TBL miotsusa ON miotsusa.COMM=socrata.Commodity_Code
Left join PRE_STAGING.dbo.MTSUSA_TBL mio2tsusa ON mio2tsusa.COMM=socrata.Commodity_Code
Left join STAGING.dbo.sics_altered sicb ON sicb.sic_code = miob.SIC
Left join STAGING.dbo.sics_altered sictsusa ON sictsusa.sic_code = miotsusa.SIC
WHERE NOT EXISTS
(Select Distinct commodity_code from STAGING.dbo.commodities_copy)
group by
Commodity_Code,
iif(miob2.DESC_COMM is null, UPPER(socrata.Commodity_Short_Name), miob2.DESC_COMM),
iif(mio2tsusa.DESC_COMM is null, UPPER(socrata.Commodity_Short_Name), mio2tsusa.DESC_COMM),
socrata.Commodity_description,
socrata.Commodity_description,
iif(miob2.UNIDAD is null, socrata.unit_1, miob2.UNIDAD),
iif(mio2tsusa.UNIDAD is null, socrata.unit_1, mio2tsusa.UNIDAD),
socrata.end_use_e,
socrata.end_use_i,
miob.TRIBUTA,
miotsusa.TRIBUTA,
miob.NTRIBUTA,
miotsusa.NTRIBUTA,
sicb.id,
sictsusa.id,
SUBSTRING(Commodity_Code, 1, 2),
SUBSTRING(Commodity_Code, 1, 4),
SUBSTRING(Commodity_Code, 1, 6)
The tables used are the following:
STAGING.dbo.commodities_copy:
CREATE TABLE [dbo].[commodities_copy](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[chapter] [varchar](5) NULL,
[header] [varchar](5) NULL,
[sub_header] [varchar](10) NULL,
[commodity_code] [varchar](20) NULL,
[short_description_sched_b] [varchar](100) NULL,
[long_description_sched_b] [varchar](200) NULL,
[measurement_unit_1_sched_b] [varchar](5) NULL,
[measurement_unit_2_sched_b] [varchar](5) NULL,
[end_use_sched_b] [int] NULL,
[sitc_sched_b] [varchar](20) NULL,
[usda_sched_b] [int] NULL,
[hitech_sched_b] [int] NULL,
[naics_fk_id_sched_b] [bigint] NULL,
[short_description_sched_tsusa] [varchar](100) NULL,
[long_description_sched_tsusa] [varchar](200) NULL,
[measurement_unit_1_sched_tsusa] [varchar](5) NULL,
[measurement_unit_2_sched_tsusa] [varchar](5) NULL,
[end_use_sched_tsusa] [int] NULL,
[sitc_sched_tsusa] [varchar](20) NULL,
[usda_sched_tsusa] [int] NULL,
[hitech_sched_tsusa] [int] NULL,
[naics_fk_id_sched_tsusa] [bigint] NULL,
[year] [int] NOT NULL,
[created_on] [datetime] NOT NULL,
[created_by] [varchar](50) NULL,
[updated_on] [datetime] NULL,
[updated_by] [varchar](50) NULL,
[needs_validation] [bit] NOT NULL,
[taxable_sched_b] [nchar](3) NULL,
[non_taxable_sched_b] [nchar](3) NULL,
[taxable_sched_tsusa] [nchar](3) NULL,
[non_taxable_sched_tsusa] [nchar](3) NULL,
[fk_sic_sched_b] [bigint] NULL,
[fk_sic_sched_tsusa] [bigint] NULL
) ON [PRIMARY]
STAGING.dbo.sics_altered:
CREATE TABLE [dbo].[sics_altered](
[id] [bigint] IDENTITY(1,1) NOT NULL,
[sic_code] [varchar](4) NULL,
[sic_description] [varchar](max) NULL,
[created_on] [datetime] NOT NULL,
[created_by] [varchar](50) NOT 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] TEXTIMAGE_ON [PRIMARY]
The rest are in PRESTAGING:
PRESTAGING.dbo.TRANSACTIONS_FROM_SOCRATA:
This is the table with 1.3 million rows
CREATE TABLE [dbo].[TRANSACTIONS_FROM_SOCRATA](
[Trade] [varchar](255) NULL,
[Year] [varchar](255) NULL,
[Month] [varchar](50) NULL,
[Commodity_Code] [varchar](50) NULL,
[Commodity_Short_Name] [varchar](255) NULL,
[Commodity_description] [varchar](255) NULL,
[cty_code] [varchar](50) NULL,
[Country] [varchar](50) NULL,
[Subcountry_code] [varchar](50) NULL,
[district] [varchar](50) NULL,
[dist_name] [varchar](255) NULL,
[data] [varchar](50) NULL,
[sitc] [varchar](50) NULL,
[SITC_Short_Desc] [varchar](255) NULL,
[SITC_Long_Desc] [varchar](255) NULL,
[naics] [varchar](50) NULL,
[NAICS_description] [varchar](255) NULL,
[end_use_i] [varchar](50) NULL,
[end_use_e] [varchar](50) NULL,
[hts_desc] [varchar](255) NULL,
[unit_1] [varchar](50) NULL,
[qty_1] [varchar](50) NULL,
[unit_2] [varchar](50) NULL,
[qty_2] [varchar](50) NULL,
[ves_val_mo] [varchar](50) NULL,
[ves_wgt_mo] [varchar](50) NULL,
[cards_mo] [varchar](50) NULL,
[air_val_mo] [varchar](50) NULL,
[air_wgt_mo] [varchar](50) NULL,
[dut_val_mo] [varchar](50) NULL,
[cal_dut_mo] [varchar](50) NULL,
[con_cha_mo] [varchar](50) NULL,
[con_cif_mo] [varchar](50) NULL,
[gen_val_mo] [varchar](50) NULL,
[gen_cha_mo] [varchar](50) NULL,
[gen_cif_mo] [varchar](50) NULL,
[air_cha_mo] [varchar](50) NULL,
[ves_cha_mo] [varchar](50) NULL,
[cnt_cha_mo] [varchar](50) NULL,
[rev_data] [varchar](50) NULL
) ON [PRIMARY]
PRESTAGING.dbo.MIOB_TBL:
CREATE TABLE [dbo].[MIOB_TBL](
[id] [int] IDENTITY(1,1) NOT NULL,
[COMM] [nchar](10) NOT NULL,
[INSUMO] [nchar](3) NULL,
[PBTO] [nchar](4) NULL,
[SIC] [nchar](4) NULL,
[NAICS] [nchar](6) NULL,
[TRIBUTA] [nchar](3) NULL,
[NTRIBUTA] [nchar](3) NULL,
[LAST_UPDATE] [date] NULL,
[LAST_UPDATED_BY] [nchar](20) NULL,
[CREATION_DATE] [date] NULL,
[CREATED_BY] [nchar](15) NULL,
[migrated_on] [datetime] NOT 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]
PRESTAGING.dbo.MIOTSUSA_TBL:
CREATE TABLE [dbo].[MIOTSUSA_TBL](
[COMM] [nchar](10) NOT NULL,
[INSUMO] [nchar](3) NULL,
[PBTO] [nchar](4) NULL,
[SIC] [nchar](4) NULL,
[NAICS] [nchar](6) NULL,
[TRIBUTA] [nchar](3) NULL,
[NTRIBUTA] [nchar](3) NULL,
[id] [int] IDENTITY(1,1) NOT NULL,
[migrated_on] [datetime] NOT NULL
) ON [PRIMARY]
PRESTAGING.dbo.MSCHB_TBL:
CREATE TABLE [dbo].[MSCHB_TBL](
[id] [int] IDENTITY(1,1) NOT NULL,
[COMM] [nchar](10) NOT NULL,
[DESC_COMM] [nchar](50) NULL,
[UNIDAD] [nchar](3) NULL,
[LAST_UPDATE] [date] NULL,
[LAST_UPDATED_BY] [nchar](20) NULL,
[CREATION_DATE] [date] NULL,
[CREATED_BY] [nchar](15) NULL,
[migrated_on] [datetime] NOT 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]
PRESTAGING.dbo.MTSUSA_TBL
CREATE TABLE [dbo].[MTSUSA_TBL](
[COMM] [nchar](10) NOT NULL,
[DESC_COMM] [nchar](50) NULL,
[UNIDAD] [nchar](3) NULL,
[id] [int] IDENTITY(1,1) NOT NULL,
[migrated_on] [datetime] NOT NULL
) ON [PRIMARY]
Let me know if there's anything else I need to provide.
With all those left outer joins, the query optimizer has to start with TRANSACTION_FROM_SOCRATA, so I would start with that. The only filtering is the NOT IN clause--would that cut down the 1MM rows to something more reasonable? If not, you're pretty much doomed to running at least one table scan (and possibly several) on the entire table.
If filtering on Commodity_Code would significantly cut things down, that can only be done if the column is indexed, so that SQL can find and read only those rows. It can only do that if there is an index on column--otherwise you're back to a table scan. Similarly, having an index on commodity_code in table commodities_copy` would help as well, if that table is large.
As discussed in the comments, a NOT EXISTS check would be most efficient, written as a correlated subquery:
WHERE NOT EXISTS (select commodity_code
from STAGING.dbo.commodities_copy
where commodity_code = socrtata.Commodity_Code)
(I'd want to do a lot of testing on this, checking and double-checking everything. Improving performance is tricky, doubly so when done through SO.)
Try this,
create table #socrata(Commodity_Code varchar(100),unit_2_b varchar(50),unit_2_tsusa varchar(50),[year] varchar(50))
insert into #socrata
SELECT
Commodity_Code,
MAX(socrata.unit_2) as unit_2_b,
MAX(socrata.unit_2) as unit_2_tsusa,
MAX(socrata.[year]),
FROM PRE_STAGING.dbo.TRANSACTIONS_FROM_SOCRATA socrata
group by Commodity_Code
SELECT
--Distinct
Commodity_Code,
iif(miob2.DESC_COMM is null, UPPER(socrata.Commodity_Short_Name), miob2.DESC_COMM) as short_commmodity_description_b,
iif(mio2tsusa.DESC_COMM is null, UPPER(socrata.Commodity_Short_Name), mio2tsusa.DESC_COMM) as short_commmodity_description_tsusa,
socrata.Commodity_description as long_commodity_description_b,
socrata.Commodity_description as long_commodity_description_tsusa,
iif(miob2.UNIDAD is null, socrata.unit_1, miob2.UNIDAD) as unit_1_b,
iif(mio2tsusa.UNIDAD is null, socrata.unit_1, mio2tsusa.UNIDAD) as unit_1_tsusa,
unit_2_b,
unit_2_tsusa,
socrata.end_use_e as end_use_b,
socrata.end_use_i as end_use_tsusa,
[year],
'system' as created_by,
getdate() as created_on,
miob.TRIBUTA as taxable_b,
miotsusa.TRIBUTA as taxable_tsusa,
miob.NTRIBUTA as non_taxable_b,
miotsusa.NTRIBUTA as non_taxable_tsusa,
sicb.id as sic_id_b,
sictsusa.id as sic_id_tsusa,
SUBSTRING(Commodity_Code, 1, 2) as chapter,
SUBSTRING(Commodity_Code, 1, 4) as header,
SUBSTRING(Commodity_Code, 1, 6) as sub_header,
0 as needs_validation
FROM #socrata socrata
Left join PRE_STAGING.DBO.MIOB_TBL miob ON miob.COMM=socrata.Commodity_Code
Left join PRE_STAGING.dbo.MSCHB_TBL miob2 ON miob2.COMM=socrata.Commodity_Code
Left join PRE_STAGING.dbo.MIOTSUSA_TBL miotsusa ON miotsusa.COMM=socrata.Commodity_Code
Left join PRE_STAGING.dbo.MTSUSA_TBL mio2tsusa ON mio2tsusa.COMM=socrata.Commodity_Code
Left join STAGING.dbo.sics_altered sicb ON sicb.sic_code = miob.SIC
Left join STAGING.dbo.sics_altered sictsusa ON sictsusa.sic_code = miotsusa.SIC
WHERE NOT EXISTS
(Select commodity_code from STAGING.dbo.commodities_copy where commodity_code = socrtata.Commodity_Code)
if Read uncommitted data is not a concern then you can use with (nolock)
Also your exists clause was wrong and no need of distinct.check rest of the changes.
UPDATE MysterySpells SET SpellId = (SELECT Id FROM Spells
WHERE Name = 'unseen servant')
WHERE MysteryId = 1 And ClassLevel = 2
I know the subquery returns 589. Anyone have any clues why I would be getting this error:
The UPDATE statement conflicted with the FOREIGN KEY constraint
"FK_MysterySpells_Spells". The conflict occurred in database "x",
table "dbo.Spells", column 'Id'.
CODE UPDATE
CREATE TABLE [dbo].[Spells](
[Id] [int] IDENTITY(1,1) NOT NULL,
[OldId] [int] NULL,
[Name] [nvarchar](100) NOT NULL,
[School] [nvarchar](50) NULL,
[SubSchool] [nvarchar](50) NULL,
[SchoolID] [int] NOT NULL,
[SubSchoolID] [int] NULL,
[CastingTime] [nvarchar](250) NULL,
[Components] [nvarchar](250) NULL,
[IsCostly] [bit] NOT NULL,
[Cost] [int] NULL,
[RangeDescription] [nvarchar](250) NULL,
[Range] [int] NOT NULL,
[RangeIncrement] [int] NOT NULL,
[RangeGap] [int] NOT NULL,
[Area] [int] NOT NULL,
[AreaIncrement] [int] NOT NULL,
[AreaGap] [int] NOT NULL,
[AreaNote] [nvarchar](250) NULL,
[Description] [nvarchar](max) NULL,
[ShortDescription] [nvarchar](250) NULL,
[Targets] [nvarchar](250) NULL,
[Effect] [nvarchar](250) NULL,
[Duration] [nvarchar](250) NULL,
[SavingThrow] [nvarchar](250) NULL,
[SpellResistence] [nvarchar](250) NULL,
[HasVerbal] [bit] NOT NULL,
[HasSomatic] [bit] NOT NULL,
[HasMaterial] [bit] NOT NULL,
[HasFocus] [bit] NOT NULL,
[HasDivineFocus] [bit] NOT NULL,
[WizardLevel] [int] NULL,
[ClericLevel] [int] NULL,
[AdeptLevel] [int] NULL,
[AlchemistLevel] [int] NULL,
[AntipaladinLevel] [int] NULL,
[BardLevel] [int] NULL,
[BloodragerLevel] [int] NULL,
[DruidLevel] [int] NULL,
[InquisitorLevel] [int] NULL,
[MagusLevel] [int] NULL,
[OracleLevel] [int] NULL,
[PaladinLevel] [int] NULL,
[RangerLevel] [int] NULL,
[ShamanLevel] [int] NULL,
[SorcererLevel] [int] NULL,
[SummonerLevel] [int] NULL,
[WitchLevel] [int] NULL,
[SLALevel] [int] NULL,
[IsDismissible] [bit] NOT NULL,
[IsLanguageDependent] [bit] NOT NULL,
[IsShapeable] [bit] NOT NULL,
[IsAcid] [bit] NOT NULL,
[IsAir] [bit] NOT NULL,
[IsCold] [bit] NOT NULL,
[IsCurse] [bit] NOT NULL,
[IsDarkness] [bit] NOT NULL,
[IsDeath] [bit] NOT NULL,
[IsDisease] [bit] NOT NULL,
[IsEarth] [bit] NOT NULL,
[IsElectricity] [bit] NOT NULL,
[IsEmotion] [bit] NOT NULL,
[IsFear] [bit] NOT NULL,
[IsFire] [bit] NOT NULL,
[IsForce] [bit] NOT NULL,
[IsLight] [bit] NOT NULL,
[IsPain] [bit] NOT NULL,
[IsPoison] [bit] NOT NULL,
[IsShadow] [bit] NOT NULL,
[IsSonic] [bit] NOT NULL,
[IsWater] [bit] NOT NULL,
[IsChaotic] [bit] NOT NULL,
[IsEvil] [bit] NOT NULL,
[IsGood] [bit] NOT NULL,
[IsLawful] [bit] NOT NULL,
[IsMindAffecting] [bit] NOT NULL,
[IsMythic] [bit] NULL,
[MythicDescription] [nvarchar](max) NULL,
[Augmented] [nvarchar](max) NULL,
CONSTRAINT [PK_Spells] 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] TEXTIMAGE_ON [PRIMARY]
CREATE TABLE [dbo].[MysterySpells](
[Id] [int] IDENTITY(1,1) NOT NULL,
[MysteryId] [int] NOT NULL,
[ClassLevel] [int] NOT NULL,
[SpellId] [int] NOT NULL,
[SpellLevel] [int] NULL,
[Restrictions] [varchar](100) NULL,
CONSTRAINT [PK_MysterySpells] 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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[MysterySpells] WITH NOCHECK ADD CONSTRAINT [FK_MysterySpells_Mysteries] FOREIGN KEY([MysteryId])
REFERENCES [dbo].[Mysteries] ([Id])
GO
ALTER TABLE [dbo].[MysterySpells] CHECK CONSTRAINT [FK_MysterySpells_Mysteries]
GO
ALTER TABLE [dbo].[MysterySpells] WITH NOCHECK ADD CONSTRAINT [FK_MysterySpells_Spells] FOREIGN KEY([SpellId])
REFERENCES [dbo].[Spells] ([Id])
GO
ALTER TABLE [dbo].[MysterySpells] CHECK CONSTRAINT [FK_MysterySpells_Spells]
GO
If I were you, here is how I debug
If you expect
SELECT Id FROM Spells WHERE Name = 'unseen servant'
to return 589
I will hard code it to 1 first.
UPDATE MysterySpells SET SpellId = 589
WHERE MysteryId = 1 And ClassLevel = 2
If the error still there, I will check if table Spells contains value where Id = 589 and Name = 'unseen servant'.
I believe you will find the root cause somewhere in the process.
Note: It would be better if you show us the table structure and data inside.
The issue is that it does not know what value to grab. There is no mapping defined between spells and mystery spells in your query. The current update is actually trying to update each row in MysterySpells with 589 records.
If you simply wanted to map all of your mystery spells to a single spell you can do:
UPDATE MysterySpells SET SpellId = (SELECT TOP 1 Id FROM Spells
WHERE Name = 'unseen servant')
WHERE MysteryId = 1 And ClassLevel = 2
If there is a pre-existing mapping between these 2 tables and you are expecting to update MysterySpells based on a relationship with spells table:
UPDATE MysterySpells
SET SpellID = t2.ID
FROM MysterySpells t1
JOIN Spells t2 ON t2.relation = t1.relation
If there isn't any existing mapping and you are trying to do this to create this as the mapping you have few options.
1) If no other relationship exists map by hand
2) If their is a relationship that can be calculated, but is not available through an existing join, you can take advantage of the row and partition functions to relate the two. Then use an update similar to the 2nd code block I posted.
I'm using SQL server 2008 Express R2 with Advanced Services. I'm a DBA in training, so I'm learning as I get more exposure to SQL. I have a Base table that needs to be split into a Header and Details table. The two tables will be linked via HeaderID. The Header will have one record and the Details can have up to 64 Name/Value records, associated with the one record in the Header table. I need a Query to copy the data from the Base table into the Header and Details tables. Here is the design:
Base Table:
CREATE TABLE [dbo].[tblBase](
[ID] [int] IDENTITY(1,1) NOT NULL,
[PartType] [varchar](50) NULL,
[SerialNumber] [varchar](50) NOT NULL,
[Station] [varchar](50) NULL,
[Reject] [varchar](50) NULL,
[Generation] [varchar](50) NULL,
[Mod_25_1_Orifice] [varchar](50) NULL,
[Mod_25_1_LowerBody] [varchar](50) NULL,
[Mod_25_1_OrificeHolderLot] [varchar](50) NULL,
[Mod_25_1_Future1] [varchar](50) NULL,
[Mod_25_1_Future2] [varchar](50) NULL,
[Mod_30_2_Force] [varchar](50) NULL,
[Mod_30_2_Dist] [varchar](50) NULL,
[Mod_35_1_Pintle] [varchar](50) NULL,
[Mod_35_1_Spring] [varchar](50) NULL,
[Mod_35_1_CenterPole] [varchar](50) NULL,
[Mod_35_1_LowerBodyOuterLot] [varchar](50) NULL,
[Mod_40_1_Force] [varchar](50) NULL,
[Mod_40_1_Dist] [varchar](50) NULL,
[Mod_40_2_Force] [varchar](50) NULL,
[Mod_40_2_Dist] [varchar](50) NULL,
[Mod_40_2_DistToPress] [varchar](50) NULL,
[Mod_40_2_OffsetDist] [varchar](50) NULL,
[Mod_50_1_LastForce] [varchar](50) NULL,
[Mod_50_1_LastDist] [varchar](50) NULL,
[Mod_50_3_LeakRate] [varchar](50) NULL,
[Mod_50_3_PressureDrop] [varchar](50) NULL,
[Mod_70_1_SmOring] [varchar](50) NULL,
[Mod_70_1_RestrOrifice] [varchar](50) NULL,
[Mod_70_1_UpperFlBrOring] [varchar](50) NULL,
[Mod_70_1_Filter] [varchar](50) NULL,
[Mod_70_1_FluidConn] [varchar](50) NULL,
[Mod_70_2_LgOring] [varchar](50) NULL,
[Mod_70_2_Coil] [varchar](50) NULL,
[Mod_70_2_Filter] [varchar](50) NULL,
[Mod_100_2_Current] [varchar](50) NULL,
[Mod_100_2_Dist] [varchar](50) NULL,
[Mod_105_1_InletTube] [varchar](50) NULL,
[Mod_105_1_Filter] [varchar](50) NULL,
[Mod_105_1_RestrOrifice] [varchar](50) NULL,
[Mod_105_1_PressDist] [varchar](50) NULL,
[Mod_105_1_TimeStamp] [datetime] NULL,
[Mod_110_1_BarrelFilter] [varchar](50) NULL,
[Mod_110_1_OrificePlate] [varchar](50) NULL,
[Mod_110_1_PressDist] [varchar](50) NULL,
[Mod_115_1_Force] [varchar](50) NULL,
[Mod_115_1_Dist] [varchar](50) NULL,
[Mod_115_1_Force2] [varchar](50) NULL,
[Mod_115_1_Dist2] [varchar](50) NULL,
[Mod_120_1_FluxBrAssem] [varchar](50) NULL,
[Mod_120_1_Coil] [varchar](50) NULL,
[Mod_120_1_LowerFluxBr] [varchar](50) NULL,
[Mod_120_1_PressDist] [varchar](50) NULL,
[Mod_130_1_Nest] [varchar](50) NULL,
[Mod_130_1_UnEnergizedFlow] [varchar](50) NULL,
[Mod_130_1_EnergizedFlow] [varchar](50) NULL,
[Mod_140_1_TimeStamp] [datetime] NULL,
[Mod_140_1_LeakRate] [varchar](50) NULL,
[Mod_140_1_PressureDrop] [varchar](50) NULL,
[Mod_35_1_TimeStamp] [datetime] NULL,
[Mod_70_1_TimeStamp] [datetime] NULL,
[Mod_130_1_TimeStamp] [datetime] NULL,
[Mod_135_1_TimeStamp] [datetime] NULL,
[Mod_135_2_TimeStamp] [datetime] NULL,
[Mod_150_1_LeakRate] [varchar](50) NULL,
[Mod_150_1_PressureDrop] [varchar](50) NULL,
[Mod_150_1_TimeStamp] [datetime] NULL,
[LoopA_pallet] [varchar](10) NULL,
[LoopB_pallet] [varchar](10) NULL,
[LoopC_pallet] [varchar](10) NULL,
CONSTRAINT [PK_tblLanco] PRIMARY KEY CLUSTERED
(
[SerialNumber] ASC
) WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Header Table:
CREATE TABLE [dbo].[tblHeader](
[HeaderID] [bigint] IDENTITY(1,1) NOT NULL,
[SerialNumber] [varchar](50) NOT NULL,
[PartType] [varchar](50) NULL,
[Station] [varchar](50) NULL,
[Reject] [varchar](50) NULL,
[Generation] [varchar](50) NULL,
CONSTRAINT [IX_tblLancoHeader] UNIQUE NONCLUSTERED
(
[SerialNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Details Table:
CREATE TABLE [dbo].[tblLDetail](
[DetailID] [bigint] IDENTITY(1,1) NOT NULL,
[HeaderID] [bigint] NOT NULL,
[Name] [varchar](100) NOT NULL,
[Value] [varchar](100) NULL
) ON [PRIMARY]
Hopefully, this is clear enough, such that I can get some help writing the query to copy the data out of the Base table, into the Header & Details table.
Thank You,
Rock
INSERT tblHeader (
[SerialNumber], [PartType], [Station], [Reject], [Generation]
)
SELECT [PartType], [SerialNumber], [Station], [Reject], [Generation]
FROM tblBase;
INSERT tblDetail (
[HeaderID], [Name], [VALUE]
)
SELECT h.HeaderID, 'Mod_25_1_Orifice', b.Mod_25_1_Orifice
FROM tblBase b
JOIN tblHeader h ON b.[SerialNumber] = h.[SerialNumber];
INSERT tblDetail (
[HeaderID], [Name], [VALUE]
)
SELECT h.HeaderID, 'Mod_25_1_LowerBody', b.Mod_25_1_LowerBody
FROM tblBase b
JOIN tblHeader h ON b.[SerialNumber] = h.[SerialNumber];
etc... insert to tblDetail 62 more times for the other 62 detail columns
This is maddening! Code in question has been running for over 5 years.
Here's the scoop....
I am doing an INSERT...SELECT into a table with a primary key that is an identity column. I do not specify the key when I insert - SQL Server generates it as expected.
I am doing the insert in a stored procedure that I call in a loop (for loop in SSIS, actually). The stored procedure will insert rows in batches (configurable). It might insert 1000 rows at a time or it might insert 50,000 - doesn't matter. It will work for a random number of calls (inserting thousands of rows) and then it will fail, out of the blue, with a
Violation of primary key / duplicate
error. If I check the identity seed - it is correct. If I kick off the process again it will work fine, for a while.
The values being inserted are coming from 2 tables that I join together, as if that matters.
The bulk of my code is below:
WHILE #pk <= #max_pk
BEGIN
INSERT INTO tbl_claim_line (fk_batch_control_group, fk_claim, fk_provider, service_from_date, service_to_date, allowed, net_paid, COB, flex_1, flex_2, flex_3, flex_4)
SELECT
#fk_batch_control_group
, c.pk_claim
, p.pk_provider
, i.date_of_service_from
, i.date_of_service_to
, i.allowed_amount
, i.net_paid_amount
, i.cob_amount
, i.claimline_flex_1
, i.claimline_flex_2
, i.claimline_flex_3
, i.claimline_flex_4
FROM
tbl_import i
INNER JOIN
tbl_import__claim c ON i.claim_number = c.claim_number
LEFT JOIN
tbl_import__provider p ON ISNULL(i.provider_type,'') = ISNULL(p.provider_type,'')
AND ISNULL(i.provider_specialty,'') = ISNULL(p.provider_specialty,'')
AND ISNULL(i.provider_zip_code,'') = ISNULL(p.provider_zip_code,'')
WHERE
pk_import = #pk
UPDATE tbl_import
SET fk_claim_line = SCOPE_IDENTITY()
WHERE pk_import = #pk
SET #pk += 1
END
--TABLE DEFINITIONS...
CREATE TABLE [dbo].[tbl_claim_line](
[fk_batch_control_group] [int] NOT NULL,
[fk_claim] [int] NOT NULL,
[fk_provider] [int] NULL,
[service_from_date] [date] NULL,
[service_to_date] [date] NULL,
[allowed] [money] NULL,
[net_paid] [money] NULL,
[COB] [money] NULL,
[flex_1] [varchar](200) NULL,
[flex_2] [varchar](200) NULL,
[flex_3] [varchar](200) NULL,
[flex_4] [varchar](200) NULL,
[pk_claim_line] [int] IDENTITY(1,1) NOT NULL,
[insert_date] [datetime] NOT NULL,
CONSTRAINT [PK_tbl_claim_line] PRIMARY KEY NONCLUSTERED
(
[pk_claim_line] 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
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[tbl_claim_line] WITH CHECK
ADD CONSTRAINT [FK_tbl_claim_line_tbl_batch_control_group]
FOREIGN KEY([fk_batch_control_group])
REFERENCES [dbo].[tbl_batch_control_group] ([pk_batch_control_group])
GO
ALTER TABLE [dbo].[tbl_claim_line] CHECK CONSTRAINT [FK_tbl_claim_line_tbl_batch_control_group]
GO
ALTER TABLE [dbo].[tbl_claim_line] WITH CHECK
ADD CONSTRAINT [FK_tbl_claim_line_tbl_claim]
FOREIGN KEY([fk_claim])
REFERENCES [dbo].[tbl_claim] ([pk_claim])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[tbl_claim_line] CHECK CONSTRAINT [FK_tbl_claim_line_tbl_claim]
GO
ALTER TABLE [dbo].[tbl_claim_line] WITH CHECK
ADD CONSTRAINT [FK_tbl_claim_line_tbl_provider]
FOREIGN KEY([fk_provider])
REFERENCES [dbo].[tbl_provider] ([pk_provider])
GO
ALTER TABLE [dbo].[tbl_claim_line] CHECK CONSTRAINT [FK_tbl_claim_line_tbl_provider]
GO
ALTER TABLE [dbo].[tbl_claim_line] ADD CONSTRAINT [DF_tbl_claim_line__insert_date] DEFAULT (getdate()) FOR [insert_date]
GO
----second table
CREATE TABLE [dbo].[tbl_import](
[fk_claim_line] [int] NULL,
[member_id] [varchar](50) NULL,
[member_card_id] [varchar](50) NULL,
[member_first_name] [varchar](50) NULL,
[member_last_name] [varchar](50) NULL,
[member_dob] [varchar](50) NULL,
[member_gender] [varchar](50) NULL,
[member_subscriber_relationship_code] [varchar](50) NULL,
[member_address_line_1] [varchar](100) NULL,
[member_address_line_2] [varchar](100) NULL,
[member_city] [varchar](50) NULL,
[member_state] [varchar](50) NULL,
[member_zip] [varchar](50) NULL,
[member_phone] [varchar](50) NULL,
[member_email] [varchar](50) NULL,
[subscriber_id] [varchar](50) NULL,
[group_line_of_business] [varchar](50) NULL,
[group_product] [varchar](50) NULL,
[group_employer] [varchar](50) NULL,
[provider_first_name] [varchar](50) NULL,
[provider_last_or_full_name] [varchar](200) NULL,
[provider_type] [varchar](200) NULL,
[provider_specialty] [varchar](400) NULL,
[provider_zip_code] [varchar](50) NULL,
[provider_tax_id] [varchar](50) NULL,
[medical_code_1] [varchar](10) NULL,
[medical_code_1_description] [varchar](500) NULL,
[medical_code_2] [varchar](10) NULL,
[medical_code_2_description] [varchar](500) NULL,
[medical_code_3] [varchar](10) NULL,
[medical_code_3_description] [varchar](500) NULL,
[medical_code_4] [varchar](10) NULL,
[medical_code_4_description] [varchar](500) NULL,
[medical_code_5] [varchar](10) NULL,
[medical_code_5_description] [varchar](500) NULL,
[medical_code_6] [varchar](10) NULL,
[medical_code_6_description] [varchar](500) NULL,
[medical_code_7] [varchar](10) NULL,
[medical_code_7_description] [varchar](500) NULL,
[medical_code_8] [varchar](10) NULL,
[medical_code_8_description] [varchar](500) NULL,
[medical_code_9] [varchar](10) NULL,
[medical_code_9_description] [varchar](500) NULL,
[medical_code_10] [varchar](10) NULL,
[medical_code_10_description] [varchar](500) NULL,
[medical_code_11] [varchar](10) NULL,
[medical_code_11_description] [varchar](500) NULL,
[medical_code_12] [varchar](10) NULL,
[medical_code_12_description] [varchar](500) NULL,
[medical_code_13] [varchar](10) NULL,
[medical_code_13_description] [varchar](500) NULL,
[medical_code_14] [varchar](10) NULL,
[medical_code_14_description] [varchar](500) NULL,
[medical_code_15] [varchar](10) NULL,
[medical_code_15_description] [varchar](500) NULL,
[medical_code_16] [varchar](10) NULL,
[medical_code_16_description] [varchar](500) NULL,
[date_of_service_from] [varchar](50) NULL,
[date_of_service_to] [varchar](50) NULL,
[claim_number] [varchar](50) NULL,
[claim_line_number] [varchar](50) NULL,
[original_claim_number] [varchar](50) NULL,
[allowed_amount] [varchar](50) NULL,
[net_paid_amount] [varchar](50) NULL,
[cob_amount] [varchar](50) NULL,
[date_paid] [varchar](50) NULL,
[member_flex_1] [varchar](200) NULL,
[member_flex_2] [varchar](200) NULL,
[member_flex_3] [varchar](200) NULL,
[member_flex_4] [varchar](200) NULL,
[claim_flex_1] [varchar](200) NULL,
[claim_flex_2] [varchar](200) NULL,
[claim_flex_3] [varchar](200) NULL,
[claim_flex_4] [varchar](200) NULL,
[claimline_flex_1] [varchar](200) NULL,
[claimline_flex_2] [varchar](200) NULL,
[claimline_flex_3] [varchar](200) NULL,
[claimline_flex_4] [varchar](200) NULL,
[pk_import] [int] IDENTITY(1,1) NOT NULL,
CONSTRAINT [PK_tbl_import] PRIMARY KEY NONCLUSTERED
(
[pk_import] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
I ran into this and much like user3170349, it was a seed issue on the column. I'm adding some additional info, however.
First, you can run this to figure out if you have a seed problem:
DBCC CHECKIDENT ('TABLE_NAME_GOES_HERE', NORESEED);
This will give you information which will read something like this:
Checking identity information: current identity value 'XXXX', current column value 'YYYY'.
If YYYY is larger than XXXX, then you have a problem and need to RESEED the table to get things going again. You can do so with the following command:
DBCC CHECKIDENT ('TABLE_NAME_GOES_HERE', RESEED, ZZZZZ);
Where ZZZZ is the reseed value. That value should be at least one higher than YYYY. YMMV, so pick a value that is appropriate for your situation.
"Code in question has been running for over 5 years."
"It might insert 1000 records at a time or it might insert 50,000 "
Is it possible you have finally overflowed the integer type of the primary key?
Did it wrap around and is now starting over? That would cause duplicate primary keys.