Insert statement running very slowly on SQL Server












0















I had been getting data from production and loading it into a staging DWH. And then loading it further into usable format later in another DWH from staging.



Both staging and the final DWH are on the same server. This process wasn't taking long before, but now it's taking ages to load the data from staging. It takes a few minutes to load data from production into staging, but it takes hours to load it further and I am not sure why.



FYI: I had been testing the loads, so I have truncated/deleted the table a few times and reloaded them



Also I had a non clustered index on one of the columns in actual DWH which I removed



CONSTRAINT [PK_EncounterTB_Encounter_id] 
PRIMARY KEY CLUSTERED ([Encounter_id] ASC),
CONSTRAINT [Uniq_EncounterTB_Encounter_table_id]
UNIQUE NONCLUSTERED ([Encounter_Table_id] ASC)


Below is the table structure for staging and I have removed a few of the columns:



SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Stg_Encounter]
(
[encntr_id] [float] NOT NULL,
[person_id] [float] NOT NULL,
[visit_id_stay_number] [varchar](1000) NULL,
[mrn] [varchar](1000) NULL,
[encntr_type_cd] [float] NULL,
[reg_dt_tm] [datetime2](7) NULL,
[disch_dt_tm] [datetime2](7) NULL,
[admit_cd] [float] NULL,
[visit_cd] [float] NULL,
[source_cd] [float] NULL,
[sepearation_cd] [float] NULL,
[medical_service_cd] [float] NULL,
[reason_problem] [varchar](1000) NULL,
) ON [PRIMARY]
GO


For the actual DWH, the table structure is as below :



SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Encounter]
(
[Encounter_Table_id] [int] NOT NULL,
[Encounter_id] [int] NOT NULL,
[Person_id] [int] NOT NULL,
[Visit_ID] [varchar](1000) NULL,
[MRN] [varchar](1000) NULL,
[Encounter_Type] [varchar](1000) NULL,
[Arrival_Dt_Tm] [datetime2](7) NULL,
[Departure_Dt_Tm] [datetime2](7) NULL,
[Mode_of_Arrival] [varchar](1000) NULL,
[Visit_Type] [varchar](1000) NULL,
[Admit_Source] [varchar](1000) NULL,
[Mode_of_Separation] [varchar](1000) NULL,
[Medical_Service] [varchar](1000) NULL,
[Presenting_Problem] [varchar](1000) NULL,
[LOAD_Dt_Tm] [datetime] NOT NULL,
[Data_Source] [varchar](1000) NOT NULL,

CONSTRAINT [PK_EncounterTB_Encounter_id]
PRIMARY KEY CLUSTERED ([Encounter_id] ASC)
) ON [PRIMARY]
GO


Below insert is being used for inserting data :



INSERT INTO [ACTUAL_DWH].[dbo].[Encounter] 
(
[Encounter_Table_id]
,[Encounter_id]
,[Person_id]
,[Visit_ID]
,[MRN]
,[Encounter_Type]
,[Arrival_Dt_Tm]
,[Departure_Dt_Tm]
,[Mode_of_Arrival]
,[Visit_Type]
,[Admit_Source]
,[Mode_of_Separation]
,[Medical_Service]
,[Presenting_Problem]
,[MSAU_LOAD_Dt_Tm]
,[Data_Source]
)
SELECT
[Encounter_Table_id]= CONVERT(INT,Stg_e.[encntr_id])
, [Encounter_id] = CONVERT(INT,Stg_e.[encntr_id])
,[Person_id] = CONVERT(INT,Stg_e.[person_id])
,[Visit_ID] = Stg_e.[visit_id_stay_number]
,[MRN] = Stg_e.[mrn]
,[Encounter_Type] = [ACTUAL_DWH].[dbo].[emr_get_code_Description](Stg_e.encntr_type_cd)
,[Arrival_Dt_Tm] = CONVERT(DATETIME,Stg_e.reg_dt_tm)
,[Departure_Dt_Tm] = CONVERT(DATETIME,Stg_e.disch_dt_tm)
,[Mode_of_Arrival] = [ACTUAL_DWH].[dbo].[Description](Stg_e.admit_cd)
,[Visit_Type] = [ACTUAL_DWH].[dbo].[Description](Stg_e.visit_cd)
,[Admit_Source] = [ACTUAL_DWH].[dbo].[Description](Stg_e.source_cd)
,[Mode_of_Separation] = [ACTUAL_DWH].[dbo].[Description](Stg_e.sepearation_cd)
,[Medical_Service] = [ACTUAL_DWH].[dbo].[Description](Stg_e.medical_service_cd)
,[Presenting_Problem] = Stg_e.reason_problem
,[MSAU_LOAD_Dt_Tm] = getdate()
,[Data_Source] = 'SourceName'



FROM [dbo].Stg_Encounter Stg_e
where NOT EXISTS ( SELECT 1 FROM [ACTUAL_DWH].[dbo].Encounter e
WHERE stg_e.encntr_id = e.encounter_id)


The function used is as per below :



USE [ACTUAL_DWH]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[Description](@cv int)
returns varchar(80)
as begin

declare @ret varchar(80)
select @ret = cv.DESCRIPTION
from ACTUAL_DWH.DBO.CODE_VALUE cv
where cv.code_value = @cv
and cv.active_ind = 1
return isnull(@ret, 0)

end;


I am just confused where I have missed stuff!!! And what can I change. the table has around 6 million rows and it was loading them in a minute.



After the suggestions provided, I got to know that issue is with the function that I am using.I have read about CROSS APPLY but is it a good idea to apply CROSS APPLY on 15 columns?










share|improve this question




















  • 4





    FYI: I would be very surprised if anyone can assist based on this information. Database performance beyond the basics is a very specialised area and frequently involves quite a lot of investigation. It could easily be that you've crossed an internal threshold which changes the entire query. If you look at the expected and actual execution plans you might get a hint of what is causing the poor performance.

    – Dale Burrell
    Nov 26 '18 at 2:07






  • 2





    Start with identifying the bottleneck (if not yet). For example, replace insert into with select sum(1), so you'll see if it's query calculation or insertion. I bet it's related to your function, but let us know about what you got.

    – Mike Twc
    Nov 26 '18 at 3:12






  • 1





    As already mentioned, you need to break your code down into the smallest possible piece that exhibits the issue (i.e. seperate the insert from the select). You can also attack it from the other direction - install sp_whoisactive and check the waits. But that will probably just take you back to optimising queries anyway

    – Nick.McDermaid
    Nov 26 '18 at 7:50











  • Thanks Everyone!!! I needed advice on what I should be looking into and wasn't sure what more details should I be posting. I'll try the suggestions provided and will be back with the results.

    – Doodle
    Nov 27 '18 at 21:45











  • I have been trying a few things, one of which being : if I remove all the functions and run the insert with the select statement, it returns almost in a minute which is other wise taking ages to run. I am using a very simple function and I am not sure what I should be changing to get it to run quicker.

    – Doodle
    Nov 28 '18 at 23:43


















0















I had been getting data from production and loading it into a staging DWH. And then loading it further into usable format later in another DWH from staging.



Both staging and the final DWH are on the same server. This process wasn't taking long before, but now it's taking ages to load the data from staging. It takes a few minutes to load data from production into staging, but it takes hours to load it further and I am not sure why.



FYI: I had been testing the loads, so I have truncated/deleted the table a few times and reloaded them



Also I had a non clustered index on one of the columns in actual DWH which I removed



CONSTRAINT [PK_EncounterTB_Encounter_id] 
PRIMARY KEY CLUSTERED ([Encounter_id] ASC),
CONSTRAINT [Uniq_EncounterTB_Encounter_table_id]
UNIQUE NONCLUSTERED ([Encounter_Table_id] ASC)


Below is the table structure for staging and I have removed a few of the columns:



SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Stg_Encounter]
(
[encntr_id] [float] NOT NULL,
[person_id] [float] NOT NULL,
[visit_id_stay_number] [varchar](1000) NULL,
[mrn] [varchar](1000) NULL,
[encntr_type_cd] [float] NULL,
[reg_dt_tm] [datetime2](7) NULL,
[disch_dt_tm] [datetime2](7) NULL,
[admit_cd] [float] NULL,
[visit_cd] [float] NULL,
[source_cd] [float] NULL,
[sepearation_cd] [float] NULL,
[medical_service_cd] [float] NULL,
[reason_problem] [varchar](1000) NULL,
) ON [PRIMARY]
GO


For the actual DWH, the table structure is as below :



SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Encounter]
(
[Encounter_Table_id] [int] NOT NULL,
[Encounter_id] [int] NOT NULL,
[Person_id] [int] NOT NULL,
[Visit_ID] [varchar](1000) NULL,
[MRN] [varchar](1000) NULL,
[Encounter_Type] [varchar](1000) NULL,
[Arrival_Dt_Tm] [datetime2](7) NULL,
[Departure_Dt_Tm] [datetime2](7) NULL,
[Mode_of_Arrival] [varchar](1000) NULL,
[Visit_Type] [varchar](1000) NULL,
[Admit_Source] [varchar](1000) NULL,
[Mode_of_Separation] [varchar](1000) NULL,
[Medical_Service] [varchar](1000) NULL,
[Presenting_Problem] [varchar](1000) NULL,
[LOAD_Dt_Tm] [datetime] NOT NULL,
[Data_Source] [varchar](1000) NOT NULL,

CONSTRAINT [PK_EncounterTB_Encounter_id]
PRIMARY KEY CLUSTERED ([Encounter_id] ASC)
) ON [PRIMARY]
GO


Below insert is being used for inserting data :



INSERT INTO [ACTUAL_DWH].[dbo].[Encounter] 
(
[Encounter_Table_id]
,[Encounter_id]
,[Person_id]
,[Visit_ID]
,[MRN]
,[Encounter_Type]
,[Arrival_Dt_Tm]
,[Departure_Dt_Tm]
,[Mode_of_Arrival]
,[Visit_Type]
,[Admit_Source]
,[Mode_of_Separation]
,[Medical_Service]
,[Presenting_Problem]
,[MSAU_LOAD_Dt_Tm]
,[Data_Source]
)
SELECT
[Encounter_Table_id]= CONVERT(INT,Stg_e.[encntr_id])
, [Encounter_id] = CONVERT(INT,Stg_e.[encntr_id])
,[Person_id] = CONVERT(INT,Stg_e.[person_id])
,[Visit_ID] = Stg_e.[visit_id_stay_number]
,[MRN] = Stg_e.[mrn]
,[Encounter_Type] = [ACTUAL_DWH].[dbo].[emr_get_code_Description](Stg_e.encntr_type_cd)
,[Arrival_Dt_Tm] = CONVERT(DATETIME,Stg_e.reg_dt_tm)
,[Departure_Dt_Tm] = CONVERT(DATETIME,Stg_e.disch_dt_tm)
,[Mode_of_Arrival] = [ACTUAL_DWH].[dbo].[Description](Stg_e.admit_cd)
,[Visit_Type] = [ACTUAL_DWH].[dbo].[Description](Stg_e.visit_cd)
,[Admit_Source] = [ACTUAL_DWH].[dbo].[Description](Stg_e.source_cd)
,[Mode_of_Separation] = [ACTUAL_DWH].[dbo].[Description](Stg_e.sepearation_cd)
,[Medical_Service] = [ACTUAL_DWH].[dbo].[Description](Stg_e.medical_service_cd)
,[Presenting_Problem] = Stg_e.reason_problem
,[MSAU_LOAD_Dt_Tm] = getdate()
,[Data_Source] = 'SourceName'



FROM [dbo].Stg_Encounter Stg_e
where NOT EXISTS ( SELECT 1 FROM [ACTUAL_DWH].[dbo].Encounter e
WHERE stg_e.encntr_id = e.encounter_id)


The function used is as per below :



USE [ACTUAL_DWH]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[Description](@cv int)
returns varchar(80)
as begin

declare @ret varchar(80)
select @ret = cv.DESCRIPTION
from ACTUAL_DWH.DBO.CODE_VALUE cv
where cv.code_value = @cv
and cv.active_ind = 1
return isnull(@ret, 0)

end;


I am just confused where I have missed stuff!!! And what can I change. the table has around 6 million rows and it was loading them in a minute.



After the suggestions provided, I got to know that issue is with the function that I am using.I have read about CROSS APPLY but is it a good idea to apply CROSS APPLY on 15 columns?










share|improve this question




















  • 4





    FYI: I would be very surprised if anyone can assist based on this information. Database performance beyond the basics is a very specialised area and frequently involves quite a lot of investigation. It could easily be that you've crossed an internal threshold which changes the entire query. If you look at the expected and actual execution plans you might get a hint of what is causing the poor performance.

    – Dale Burrell
    Nov 26 '18 at 2:07






  • 2





    Start with identifying the bottleneck (if not yet). For example, replace insert into with select sum(1), so you'll see if it's query calculation or insertion. I bet it's related to your function, but let us know about what you got.

    – Mike Twc
    Nov 26 '18 at 3:12






  • 1





    As already mentioned, you need to break your code down into the smallest possible piece that exhibits the issue (i.e. seperate the insert from the select). You can also attack it from the other direction - install sp_whoisactive and check the waits. But that will probably just take you back to optimising queries anyway

    – Nick.McDermaid
    Nov 26 '18 at 7:50











  • Thanks Everyone!!! I needed advice on what I should be looking into and wasn't sure what more details should I be posting. I'll try the suggestions provided and will be back with the results.

    – Doodle
    Nov 27 '18 at 21:45











  • I have been trying a few things, one of which being : if I remove all the functions and run the insert with the select statement, it returns almost in a minute which is other wise taking ages to run. I am using a very simple function and I am not sure what I should be changing to get it to run quicker.

    – Doodle
    Nov 28 '18 at 23:43
















0












0








0








I had been getting data from production and loading it into a staging DWH. And then loading it further into usable format later in another DWH from staging.



Both staging and the final DWH are on the same server. This process wasn't taking long before, but now it's taking ages to load the data from staging. It takes a few minutes to load data from production into staging, but it takes hours to load it further and I am not sure why.



FYI: I had been testing the loads, so I have truncated/deleted the table a few times and reloaded them



Also I had a non clustered index on one of the columns in actual DWH which I removed



CONSTRAINT [PK_EncounterTB_Encounter_id] 
PRIMARY KEY CLUSTERED ([Encounter_id] ASC),
CONSTRAINT [Uniq_EncounterTB_Encounter_table_id]
UNIQUE NONCLUSTERED ([Encounter_Table_id] ASC)


Below is the table structure for staging and I have removed a few of the columns:



SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Stg_Encounter]
(
[encntr_id] [float] NOT NULL,
[person_id] [float] NOT NULL,
[visit_id_stay_number] [varchar](1000) NULL,
[mrn] [varchar](1000) NULL,
[encntr_type_cd] [float] NULL,
[reg_dt_tm] [datetime2](7) NULL,
[disch_dt_tm] [datetime2](7) NULL,
[admit_cd] [float] NULL,
[visit_cd] [float] NULL,
[source_cd] [float] NULL,
[sepearation_cd] [float] NULL,
[medical_service_cd] [float] NULL,
[reason_problem] [varchar](1000) NULL,
) ON [PRIMARY]
GO


For the actual DWH, the table structure is as below :



SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Encounter]
(
[Encounter_Table_id] [int] NOT NULL,
[Encounter_id] [int] NOT NULL,
[Person_id] [int] NOT NULL,
[Visit_ID] [varchar](1000) NULL,
[MRN] [varchar](1000) NULL,
[Encounter_Type] [varchar](1000) NULL,
[Arrival_Dt_Tm] [datetime2](7) NULL,
[Departure_Dt_Tm] [datetime2](7) NULL,
[Mode_of_Arrival] [varchar](1000) NULL,
[Visit_Type] [varchar](1000) NULL,
[Admit_Source] [varchar](1000) NULL,
[Mode_of_Separation] [varchar](1000) NULL,
[Medical_Service] [varchar](1000) NULL,
[Presenting_Problem] [varchar](1000) NULL,
[LOAD_Dt_Tm] [datetime] NOT NULL,
[Data_Source] [varchar](1000) NOT NULL,

CONSTRAINT [PK_EncounterTB_Encounter_id]
PRIMARY KEY CLUSTERED ([Encounter_id] ASC)
) ON [PRIMARY]
GO


Below insert is being used for inserting data :



INSERT INTO [ACTUAL_DWH].[dbo].[Encounter] 
(
[Encounter_Table_id]
,[Encounter_id]
,[Person_id]
,[Visit_ID]
,[MRN]
,[Encounter_Type]
,[Arrival_Dt_Tm]
,[Departure_Dt_Tm]
,[Mode_of_Arrival]
,[Visit_Type]
,[Admit_Source]
,[Mode_of_Separation]
,[Medical_Service]
,[Presenting_Problem]
,[MSAU_LOAD_Dt_Tm]
,[Data_Source]
)
SELECT
[Encounter_Table_id]= CONVERT(INT,Stg_e.[encntr_id])
, [Encounter_id] = CONVERT(INT,Stg_e.[encntr_id])
,[Person_id] = CONVERT(INT,Stg_e.[person_id])
,[Visit_ID] = Stg_e.[visit_id_stay_number]
,[MRN] = Stg_e.[mrn]
,[Encounter_Type] = [ACTUAL_DWH].[dbo].[emr_get_code_Description](Stg_e.encntr_type_cd)
,[Arrival_Dt_Tm] = CONVERT(DATETIME,Stg_e.reg_dt_tm)
,[Departure_Dt_Tm] = CONVERT(DATETIME,Stg_e.disch_dt_tm)
,[Mode_of_Arrival] = [ACTUAL_DWH].[dbo].[Description](Stg_e.admit_cd)
,[Visit_Type] = [ACTUAL_DWH].[dbo].[Description](Stg_e.visit_cd)
,[Admit_Source] = [ACTUAL_DWH].[dbo].[Description](Stg_e.source_cd)
,[Mode_of_Separation] = [ACTUAL_DWH].[dbo].[Description](Stg_e.sepearation_cd)
,[Medical_Service] = [ACTUAL_DWH].[dbo].[Description](Stg_e.medical_service_cd)
,[Presenting_Problem] = Stg_e.reason_problem
,[MSAU_LOAD_Dt_Tm] = getdate()
,[Data_Source] = 'SourceName'



FROM [dbo].Stg_Encounter Stg_e
where NOT EXISTS ( SELECT 1 FROM [ACTUAL_DWH].[dbo].Encounter e
WHERE stg_e.encntr_id = e.encounter_id)


The function used is as per below :



USE [ACTUAL_DWH]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[Description](@cv int)
returns varchar(80)
as begin

declare @ret varchar(80)
select @ret = cv.DESCRIPTION
from ACTUAL_DWH.DBO.CODE_VALUE cv
where cv.code_value = @cv
and cv.active_ind = 1
return isnull(@ret, 0)

end;


I am just confused where I have missed stuff!!! And what can I change. the table has around 6 million rows and it was loading them in a minute.



After the suggestions provided, I got to know that issue is with the function that I am using.I have read about CROSS APPLY but is it a good idea to apply CROSS APPLY on 15 columns?










share|improve this question
















I had been getting data from production and loading it into a staging DWH. And then loading it further into usable format later in another DWH from staging.



Both staging and the final DWH are on the same server. This process wasn't taking long before, but now it's taking ages to load the data from staging. It takes a few minutes to load data from production into staging, but it takes hours to load it further and I am not sure why.



FYI: I had been testing the loads, so I have truncated/deleted the table a few times and reloaded them



Also I had a non clustered index on one of the columns in actual DWH which I removed



CONSTRAINT [PK_EncounterTB_Encounter_id] 
PRIMARY KEY CLUSTERED ([Encounter_id] ASC),
CONSTRAINT [Uniq_EncounterTB_Encounter_table_id]
UNIQUE NONCLUSTERED ([Encounter_Table_id] ASC)


Below is the table structure for staging and I have removed a few of the columns:



SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Stg_Encounter]
(
[encntr_id] [float] NOT NULL,
[person_id] [float] NOT NULL,
[visit_id_stay_number] [varchar](1000) NULL,
[mrn] [varchar](1000) NULL,
[encntr_type_cd] [float] NULL,
[reg_dt_tm] [datetime2](7) NULL,
[disch_dt_tm] [datetime2](7) NULL,
[admit_cd] [float] NULL,
[visit_cd] [float] NULL,
[source_cd] [float] NULL,
[sepearation_cd] [float] NULL,
[medical_service_cd] [float] NULL,
[reason_problem] [varchar](1000) NULL,
) ON [PRIMARY]
GO


For the actual DWH, the table structure is as below :



SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[Encounter]
(
[Encounter_Table_id] [int] NOT NULL,
[Encounter_id] [int] NOT NULL,
[Person_id] [int] NOT NULL,
[Visit_ID] [varchar](1000) NULL,
[MRN] [varchar](1000) NULL,
[Encounter_Type] [varchar](1000) NULL,
[Arrival_Dt_Tm] [datetime2](7) NULL,
[Departure_Dt_Tm] [datetime2](7) NULL,
[Mode_of_Arrival] [varchar](1000) NULL,
[Visit_Type] [varchar](1000) NULL,
[Admit_Source] [varchar](1000) NULL,
[Mode_of_Separation] [varchar](1000) NULL,
[Medical_Service] [varchar](1000) NULL,
[Presenting_Problem] [varchar](1000) NULL,
[LOAD_Dt_Tm] [datetime] NOT NULL,
[Data_Source] [varchar](1000) NOT NULL,

CONSTRAINT [PK_EncounterTB_Encounter_id]
PRIMARY KEY CLUSTERED ([Encounter_id] ASC)
) ON [PRIMARY]
GO


Below insert is being used for inserting data :



INSERT INTO [ACTUAL_DWH].[dbo].[Encounter] 
(
[Encounter_Table_id]
,[Encounter_id]
,[Person_id]
,[Visit_ID]
,[MRN]
,[Encounter_Type]
,[Arrival_Dt_Tm]
,[Departure_Dt_Tm]
,[Mode_of_Arrival]
,[Visit_Type]
,[Admit_Source]
,[Mode_of_Separation]
,[Medical_Service]
,[Presenting_Problem]
,[MSAU_LOAD_Dt_Tm]
,[Data_Source]
)
SELECT
[Encounter_Table_id]= CONVERT(INT,Stg_e.[encntr_id])
, [Encounter_id] = CONVERT(INT,Stg_e.[encntr_id])
,[Person_id] = CONVERT(INT,Stg_e.[person_id])
,[Visit_ID] = Stg_e.[visit_id_stay_number]
,[MRN] = Stg_e.[mrn]
,[Encounter_Type] = [ACTUAL_DWH].[dbo].[emr_get_code_Description](Stg_e.encntr_type_cd)
,[Arrival_Dt_Tm] = CONVERT(DATETIME,Stg_e.reg_dt_tm)
,[Departure_Dt_Tm] = CONVERT(DATETIME,Stg_e.disch_dt_tm)
,[Mode_of_Arrival] = [ACTUAL_DWH].[dbo].[Description](Stg_e.admit_cd)
,[Visit_Type] = [ACTUAL_DWH].[dbo].[Description](Stg_e.visit_cd)
,[Admit_Source] = [ACTUAL_DWH].[dbo].[Description](Stg_e.source_cd)
,[Mode_of_Separation] = [ACTUAL_DWH].[dbo].[Description](Stg_e.sepearation_cd)
,[Medical_Service] = [ACTUAL_DWH].[dbo].[Description](Stg_e.medical_service_cd)
,[Presenting_Problem] = Stg_e.reason_problem
,[MSAU_LOAD_Dt_Tm] = getdate()
,[Data_Source] = 'SourceName'



FROM [dbo].Stg_Encounter Stg_e
where NOT EXISTS ( SELECT 1 FROM [ACTUAL_DWH].[dbo].Encounter e
WHERE stg_e.encntr_id = e.encounter_id)


The function used is as per below :



USE [ACTUAL_DWH]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER function [dbo].[Description](@cv int)
returns varchar(80)
as begin

declare @ret varchar(80)
select @ret = cv.DESCRIPTION
from ACTUAL_DWH.DBO.CODE_VALUE cv
where cv.code_value = @cv
and cv.active_ind = 1
return isnull(@ret, 0)

end;


I am just confused where I have missed stuff!!! And what can I change. the table has around 6 million rows and it was loading them in a minute.



After the suggestions provided, I got to know that issue is with the function that I am using.I have read about CROSS APPLY but is it a good idea to apply CROSS APPLY on 15 columns?







sql sql-server sql-insert






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '18 at 0:05







Doodle

















asked Nov 26 '18 at 0:40









DoodleDoodle

11710




11710








  • 4





    FYI: I would be very surprised if anyone can assist based on this information. Database performance beyond the basics is a very specialised area and frequently involves quite a lot of investigation. It could easily be that you've crossed an internal threshold which changes the entire query. If you look at the expected and actual execution plans you might get a hint of what is causing the poor performance.

    – Dale Burrell
    Nov 26 '18 at 2:07






  • 2





    Start with identifying the bottleneck (if not yet). For example, replace insert into with select sum(1), so you'll see if it's query calculation or insertion. I bet it's related to your function, but let us know about what you got.

    – Mike Twc
    Nov 26 '18 at 3:12






  • 1





    As already mentioned, you need to break your code down into the smallest possible piece that exhibits the issue (i.e. seperate the insert from the select). You can also attack it from the other direction - install sp_whoisactive and check the waits. But that will probably just take you back to optimising queries anyway

    – Nick.McDermaid
    Nov 26 '18 at 7:50











  • Thanks Everyone!!! I needed advice on what I should be looking into and wasn't sure what more details should I be posting. I'll try the suggestions provided and will be back with the results.

    – Doodle
    Nov 27 '18 at 21:45











  • I have been trying a few things, one of which being : if I remove all the functions and run the insert with the select statement, it returns almost in a minute which is other wise taking ages to run. I am using a very simple function and I am not sure what I should be changing to get it to run quicker.

    – Doodle
    Nov 28 '18 at 23:43
















  • 4





    FYI: I would be very surprised if anyone can assist based on this information. Database performance beyond the basics is a very specialised area and frequently involves quite a lot of investigation. It could easily be that you've crossed an internal threshold which changes the entire query. If you look at the expected and actual execution plans you might get a hint of what is causing the poor performance.

    – Dale Burrell
    Nov 26 '18 at 2:07






  • 2





    Start with identifying the bottleneck (if not yet). For example, replace insert into with select sum(1), so you'll see if it's query calculation or insertion. I bet it's related to your function, but let us know about what you got.

    – Mike Twc
    Nov 26 '18 at 3:12






  • 1





    As already mentioned, you need to break your code down into the smallest possible piece that exhibits the issue (i.e. seperate the insert from the select). You can also attack it from the other direction - install sp_whoisactive and check the waits. But that will probably just take you back to optimising queries anyway

    – Nick.McDermaid
    Nov 26 '18 at 7:50











  • Thanks Everyone!!! I needed advice on what I should be looking into and wasn't sure what more details should I be posting. I'll try the suggestions provided and will be back with the results.

    – Doodle
    Nov 27 '18 at 21:45











  • I have been trying a few things, one of which being : if I remove all the functions and run the insert with the select statement, it returns almost in a minute which is other wise taking ages to run. I am using a very simple function and I am not sure what I should be changing to get it to run quicker.

    – Doodle
    Nov 28 '18 at 23:43










4




4





FYI: I would be very surprised if anyone can assist based on this information. Database performance beyond the basics is a very specialised area and frequently involves quite a lot of investigation. It could easily be that you've crossed an internal threshold which changes the entire query. If you look at the expected and actual execution plans you might get a hint of what is causing the poor performance.

– Dale Burrell
Nov 26 '18 at 2:07





FYI: I would be very surprised if anyone can assist based on this information. Database performance beyond the basics is a very specialised area and frequently involves quite a lot of investigation. It could easily be that you've crossed an internal threshold which changes the entire query. If you look at the expected and actual execution plans you might get a hint of what is causing the poor performance.

– Dale Burrell
Nov 26 '18 at 2:07




2




2





Start with identifying the bottleneck (if not yet). For example, replace insert into with select sum(1), so you'll see if it's query calculation or insertion. I bet it's related to your function, but let us know about what you got.

– Mike Twc
Nov 26 '18 at 3:12





Start with identifying the bottleneck (if not yet). For example, replace insert into with select sum(1), so you'll see if it's query calculation or insertion. I bet it's related to your function, but let us know about what you got.

– Mike Twc
Nov 26 '18 at 3:12




1




1





As already mentioned, you need to break your code down into the smallest possible piece that exhibits the issue (i.e. seperate the insert from the select). You can also attack it from the other direction - install sp_whoisactive and check the waits. But that will probably just take you back to optimising queries anyway

– Nick.McDermaid
Nov 26 '18 at 7:50





As already mentioned, you need to break your code down into the smallest possible piece that exhibits the issue (i.e. seperate the insert from the select). You can also attack it from the other direction - install sp_whoisactive and check the waits. But that will probably just take you back to optimising queries anyway

– Nick.McDermaid
Nov 26 '18 at 7:50













Thanks Everyone!!! I needed advice on what I should be looking into and wasn't sure what more details should I be posting. I'll try the suggestions provided and will be back with the results.

– Doodle
Nov 27 '18 at 21:45





Thanks Everyone!!! I needed advice on what I should be looking into and wasn't sure what more details should I be posting. I'll try the suggestions provided and will be back with the results.

– Doodle
Nov 27 '18 at 21:45













I have been trying a few things, one of which being : if I remove all the functions and run the insert with the select statement, it returns almost in a minute which is other wise taking ages to run. I am using a very simple function and I am not sure what I should be changing to get it to run quicker.

– Doodle
Nov 28 '18 at 23:43







I have been trying a few things, one of which being : if I remove all the functions and run the insert with the select statement, it returns almost in a minute which is other wise taking ages to run. I am using a very simple function and I am not sure what I should be changing to get it to run quicker.

– Doodle
Nov 28 '18 at 23:43














2 Answers
2






active

oldest

votes


















0














You can use SQL CREATE INDEX Statement to retrieve data from the database very fast.



    CREATE INDEX IX_Encounter
ON [ACTUAL_DWH].[dbo].[Encounter](Encounter_Table_id) ON [PRIMARY]

INSERT INTO [ACTUAL_DWH].[dbo].[Encounter]
(
[Encounter_Table_id]
,[Encounter_id]
,[Person_id]
,[Visit_ID]
,[MRN]
,[Encounter_Type]
,[Arrival_Dt_Tm]
,[Departure_Dt_Tm]
,[Mode_of_Arrival]
,[Visit_Type]
,[Admit_Source]
,[Mode_of_Separation]
,[Medical_Service]
,[Presenting_Problem]
,[MSAU_LOAD_Dt_Tm]
,[Data_Source]
)
SELECT
[Encounter_Table_id]= CONVERT(INT,Stg_e.[encntr_id])
, [Encounter_id] = CONVERT(INT,Stg_e.[encntr_id])
,[Person_id] = CONVERT(INT,Stg_e.[person_id])
,[Visit_ID] = Stg_e.[visit_id_stay_number]
,[MRN] = Stg_e.[mrn]
,[Encounter_Type] = [ACTUAL_DWH].[dbo].[emr_get_code_Description](Stg_e.encntr_type_cd)
,[Arrival_Dt_Tm] = CONVERT(DATETIME,Stg_e.reg_dt_tm)
,[Departure_Dt_Tm] = CONVERT(DATETIME,Stg_e.disch_dt_tm)
,[Mode_of_Arrival] = [ACTUAL_DWH].[dbo].[Description](Stg_e.admit_cd)
,[Visit_Type] = [ACTUAL_DWH].[dbo].[Description](Stg_e.visit_cd)
,[Admit_Source] = [ACTUAL_DWH].[dbo].[Description](Stg_e.source_cd)
,[Mode_of_Separation] = [ACTUAL_DWH].[dbo].[Description](Stg_e.sepearation_cd)
,[Medical_Service] = [ACTUAL_DWH].[dbo].[Description](Stg_e.medical_service_cd)
,[Presenting_Problem] = Stg_e.reason_problem
,[MSAU_LOAD_Dt_Tm] = getdate()
,[Data_Source] = 'SourceName'



FROM [dbo].Stg_Encounter Stg_e
where NOT EXISTS ( SELECT 1 FROM [ACTUAL_DWH].[dbo].Encounter e
WHERE stg_e.encntr_id = e.encounter_id)


You can check more info about index here.INDEX






share|improve this answer
























  • Hi Unknown. I am asking a completely different question. :) I know about the indexes.

    – Doodle
    Nov 29 '18 at 2:39













  • Ooops sorry my mistake. Wrong understanding.

    – Unknown
    Nov 29 '18 at 2:40



















0














Just to close this post. As suggested I had tried to break down the query and found that the function was the culprit. I am exploring it further for the resolution.



The function is taking parameter and running a SQL on another table. Which is slowing down the query. If I do the insert without the function it actually takes a few seconds to load 6 million rows.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473457%2finsert-statement-running-very-slowly-on-sql-server%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You can use SQL CREATE INDEX Statement to retrieve data from the database very fast.



        CREATE INDEX IX_Encounter
    ON [ACTUAL_DWH].[dbo].[Encounter](Encounter_Table_id) ON [PRIMARY]

    INSERT INTO [ACTUAL_DWH].[dbo].[Encounter]
    (
    [Encounter_Table_id]
    ,[Encounter_id]
    ,[Person_id]
    ,[Visit_ID]
    ,[MRN]
    ,[Encounter_Type]
    ,[Arrival_Dt_Tm]
    ,[Departure_Dt_Tm]
    ,[Mode_of_Arrival]
    ,[Visit_Type]
    ,[Admit_Source]
    ,[Mode_of_Separation]
    ,[Medical_Service]
    ,[Presenting_Problem]
    ,[MSAU_LOAD_Dt_Tm]
    ,[Data_Source]
    )
    SELECT
    [Encounter_Table_id]= CONVERT(INT,Stg_e.[encntr_id])
    , [Encounter_id] = CONVERT(INT,Stg_e.[encntr_id])
    ,[Person_id] = CONVERT(INT,Stg_e.[person_id])
    ,[Visit_ID] = Stg_e.[visit_id_stay_number]
    ,[MRN] = Stg_e.[mrn]
    ,[Encounter_Type] = [ACTUAL_DWH].[dbo].[emr_get_code_Description](Stg_e.encntr_type_cd)
    ,[Arrival_Dt_Tm] = CONVERT(DATETIME,Stg_e.reg_dt_tm)
    ,[Departure_Dt_Tm] = CONVERT(DATETIME,Stg_e.disch_dt_tm)
    ,[Mode_of_Arrival] = [ACTUAL_DWH].[dbo].[Description](Stg_e.admit_cd)
    ,[Visit_Type] = [ACTUAL_DWH].[dbo].[Description](Stg_e.visit_cd)
    ,[Admit_Source] = [ACTUAL_DWH].[dbo].[Description](Stg_e.source_cd)
    ,[Mode_of_Separation] = [ACTUAL_DWH].[dbo].[Description](Stg_e.sepearation_cd)
    ,[Medical_Service] = [ACTUAL_DWH].[dbo].[Description](Stg_e.medical_service_cd)
    ,[Presenting_Problem] = Stg_e.reason_problem
    ,[MSAU_LOAD_Dt_Tm] = getdate()
    ,[Data_Source] = 'SourceName'



    FROM [dbo].Stg_Encounter Stg_e
    where NOT EXISTS ( SELECT 1 FROM [ACTUAL_DWH].[dbo].Encounter e
    WHERE stg_e.encntr_id = e.encounter_id)


    You can check more info about index here.INDEX






    share|improve this answer
























    • Hi Unknown. I am asking a completely different question. :) I know about the indexes.

      – Doodle
      Nov 29 '18 at 2:39













    • Ooops sorry my mistake. Wrong understanding.

      – Unknown
      Nov 29 '18 at 2:40
















    0














    You can use SQL CREATE INDEX Statement to retrieve data from the database very fast.



        CREATE INDEX IX_Encounter
    ON [ACTUAL_DWH].[dbo].[Encounter](Encounter_Table_id) ON [PRIMARY]

    INSERT INTO [ACTUAL_DWH].[dbo].[Encounter]
    (
    [Encounter_Table_id]
    ,[Encounter_id]
    ,[Person_id]
    ,[Visit_ID]
    ,[MRN]
    ,[Encounter_Type]
    ,[Arrival_Dt_Tm]
    ,[Departure_Dt_Tm]
    ,[Mode_of_Arrival]
    ,[Visit_Type]
    ,[Admit_Source]
    ,[Mode_of_Separation]
    ,[Medical_Service]
    ,[Presenting_Problem]
    ,[MSAU_LOAD_Dt_Tm]
    ,[Data_Source]
    )
    SELECT
    [Encounter_Table_id]= CONVERT(INT,Stg_e.[encntr_id])
    , [Encounter_id] = CONVERT(INT,Stg_e.[encntr_id])
    ,[Person_id] = CONVERT(INT,Stg_e.[person_id])
    ,[Visit_ID] = Stg_e.[visit_id_stay_number]
    ,[MRN] = Stg_e.[mrn]
    ,[Encounter_Type] = [ACTUAL_DWH].[dbo].[emr_get_code_Description](Stg_e.encntr_type_cd)
    ,[Arrival_Dt_Tm] = CONVERT(DATETIME,Stg_e.reg_dt_tm)
    ,[Departure_Dt_Tm] = CONVERT(DATETIME,Stg_e.disch_dt_tm)
    ,[Mode_of_Arrival] = [ACTUAL_DWH].[dbo].[Description](Stg_e.admit_cd)
    ,[Visit_Type] = [ACTUAL_DWH].[dbo].[Description](Stg_e.visit_cd)
    ,[Admit_Source] = [ACTUAL_DWH].[dbo].[Description](Stg_e.source_cd)
    ,[Mode_of_Separation] = [ACTUAL_DWH].[dbo].[Description](Stg_e.sepearation_cd)
    ,[Medical_Service] = [ACTUAL_DWH].[dbo].[Description](Stg_e.medical_service_cd)
    ,[Presenting_Problem] = Stg_e.reason_problem
    ,[MSAU_LOAD_Dt_Tm] = getdate()
    ,[Data_Source] = 'SourceName'



    FROM [dbo].Stg_Encounter Stg_e
    where NOT EXISTS ( SELECT 1 FROM [ACTUAL_DWH].[dbo].Encounter e
    WHERE stg_e.encntr_id = e.encounter_id)


    You can check more info about index here.INDEX






    share|improve this answer
























    • Hi Unknown. I am asking a completely different question. :) I know about the indexes.

      – Doodle
      Nov 29 '18 at 2:39













    • Ooops sorry my mistake. Wrong understanding.

      – Unknown
      Nov 29 '18 at 2:40














    0












    0








    0







    You can use SQL CREATE INDEX Statement to retrieve data from the database very fast.



        CREATE INDEX IX_Encounter
    ON [ACTUAL_DWH].[dbo].[Encounter](Encounter_Table_id) ON [PRIMARY]

    INSERT INTO [ACTUAL_DWH].[dbo].[Encounter]
    (
    [Encounter_Table_id]
    ,[Encounter_id]
    ,[Person_id]
    ,[Visit_ID]
    ,[MRN]
    ,[Encounter_Type]
    ,[Arrival_Dt_Tm]
    ,[Departure_Dt_Tm]
    ,[Mode_of_Arrival]
    ,[Visit_Type]
    ,[Admit_Source]
    ,[Mode_of_Separation]
    ,[Medical_Service]
    ,[Presenting_Problem]
    ,[MSAU_LOAD_Dt_Tm]
    ,[Data_Source]
    )
    SELECT
    [Encounter_Table_id]= CONVERT(INT,Stg_e.[encntr_id])
    , [Encounter_id] = CONVERT(INT,Stg_e.[encntr_id])
    ,[Person_id] = CONVERT(INT,Stg_e.[person_id])
    ,[Visit_ID] = Stg_e.[visit_id_stay_number]
    ,[MRN] = Stg_e.[mrn]
    ,[Encounter_Type] = [ACTUAL_DWH].[dbo].[emr_get_code_Description](Stg_e.encntr_type_cd)
    ,[Arrival_Dt_Tm] = CONVERT(DATETIME,Stg_e.reg_dt_tm)
    ,[Departure_Dt_Tm] = CONVERT(DATETIME,Stg_e.disch_dt_tm)
    ,[Mode_of_Arrival] = [ACTUAL_DWH].[dbo].[Description](Stg_e.admit_cd)
    ,[Visit_Type] = [ACTUAL_DWH].[dbo].[Description](Stg_e.visit_cd)
    ,[Admit_Source] = [ACTUAL_DWH].[dbo].[Description](Stg_e.source_cd)
    ,[Mode_of_Separation] = [ACTUAL_DWH].[dbo].[Description](Stg_e.sepearation_cd)
    ,[Medical_Service] = [ACTUAL_DWH].[dbo].[Description](Stg_e.medical_service_cd)
    ,[Presenting_Problem] = Stg_e.reason_problem
    ,[MSAU_LOAD_Dt_Tm] = getdate()
    ,[Data_Source] = 'SourceName'



    FROM [dbo].Stg_Encounter Stg_e
    where NOT EXISTS ( SELECT 1 FROM [ACTUAL_DWH].[dbo].Encounter e
    WHERE stg_e.encntr_id = e.encounter_id)


    You can check more info about index here.INDEX






    share|improve this answer













    You can use SQL CREATE INDEX Statement to retrieve data from the database very fast.



        CREATE INDEX IX_Encounter
    ON [ACTUAL_DWH].[dbo].[Encounter](Encounter_Table_id) ON [PRIMARY]

    INSERT INTO [ACTUAL_DWH].[dbo].[Encounter]
    (
    [Encounter_Table_id]
    ,[Encounter_id]
    ,[Person_id]
    ,[Visit_ID]
    ,[MRN]
    ,[Encounter_Type]
    ,[Arrival_Dt_Tm]
    ,[Departure_Dt_Tm]
    ,[Mode_of_Arrival]
    ,[Visit_Type]
    ,[Admit_Source]
    ,[Mode_of_Separation]
    ,[Medical_Service]
    ,[Presenting_Problem]
    ,[MSAU_LOAD_Dt_Tm]
    ,[Data_Source]
    )
    SELECT
    [Encounter_Table_id]= CONVERT(INT,Stg_e.[encntr_id])
    , [Encounter_id] = CONVERT(INT,Stg_e.[encntr_id])
    ,[Person_id] = CONVERT(INT,Stg_e.[person_id])
    ,[Visit_ID] = Stg_e.[visit_id_stay_number]
    ,[MRN] = Stg_e.[mrn]
    ,[Encounter_Type] = [ACTUAL_DWH].[dbo].[emr_get_code_Description](Stg_e.encntr_type_cd)
    ,[Arrival_Dt_Tm] = CONVERT(DATETIME,Stg_e.reg_dt_tm)
    ,[Departure_Dt_Tm] = CONVERT(DATETIME,Stg_e.disch_dt_tm)
    ,[Mode_of_Arrival] = [ACTUAL_DWH].[dbo].[Description](Stg_e.admit_cd)
    ,[Visit_Type] = [ACTUAL_DWH].[dbo].[Description](Stg_e.visit_cd)
    ,[Admit_Source] = [ACTUAL_DWH].[dbo].[Description](Stg_e.source_cd)
    ,[Mode_of_Separation] = [ACTUAL_DWH].[dbo].[Description](Stg_e.sepearation_cd)
    ,[Medical_Service] = [ACTUAL_DWH].[dbo].[Description](Stg_e.medical_service_cd)
    ,[Presenting_Problem] = Stg_e.reason_problem
    ,[MSAU_LOAD_Dt_Tm] = getdate()
    ,[Data_Source] = 'SourceName'



    FROM [dbo].Stg_Encounter Stg_e
    where NOT EXISTS ( SELECT 1 FROM [ACTUAL_DWH].[dbo].Encounter e
    WHERE stg_e.encntr_id = e.encounter_id)


    You can check more info about index here.INDEX







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Nov 29 '18 at 2:35









    UnknownUnknown

    639




    639













    • Hi Unknown. I am asking a completely different question. :) I know about the indexes.

      – Doodle
      Nov 29 '18 at 2:39













    • Ooops sorry my mistake. Wrong understanding.

      – Unknown
      Nov 29 '18 at 2:40



















    • Hi Unknown. I am asking a completely different question. :) I know about the indexes.

      – Doodle
      Nov 29 '18 at 2:39













    • Ooops sorry my mistake. Wrong understanding.

      – Unknown
      Nov 29 '18 at 2:40

















    Hi Unknown. I am asking a completely different question. :) I know about the indexes.

    – Doodle
    Nov 29 '18 at 2:39







    Hi Unknown. I am asking a completely different question. :) I know about the indexes.

    – Doodle
    Nov 29 '18 at 2:39















    Ooops sorry my mistake. Wrong understanding.

    – Unknown
    Nov 29 '18 at 2:40





    Ooops sorry my mistake. Wrong understanding.

    – Unknown
    Nov 29 '18 at 2:40













    0














    Just to close this post. As suggested I had tried to break down the query and found that the function was the culprit. I am exploring it further for the resolution.



    The function is taking parameter and running a SQL on another table. Which is slowing down the query. If I do the insert without the function it actually takes a few seconds to load 6 million rows.






    share|improve this answer




























      0














      Just to close this post. As suggested I had tried to break down the query and found that the function was the culprit. I am exploring it further for the resolution.



      The function is taking parameter and running a SQL on another table. Which is slowing down the query. If I do the insert without the function it actually takes a few seconds to load 6 million rows.






      share|improve this answer


























        0












        0








        0







        Just to close this post. As suggested I had tried to break down the query and found that the function was the culprit. I am exploring it further for the resolution.



        The function is taking parameter and running a SQL on another table. Which is slowing down the query. If I do the insert without the function it actually takes a few seconds to load 6 million rows.






        share|improve this answer













        Just to close this post. As suggested I had tried to break down the query and found that the function was the culprit. I am exploring it further for the resolution.



        The function is taking parameter and running a SQL on another table. Which is slowing down the query. If I do the insert without the function it actually takes a few seconds to load 6 million rows.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 29 '18 at 23:30









        DoodleDoodle

        11710




        11710






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53473457%2finsert-statement-running-very-slowly-on-sql-server%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'