'Insert of records into SQL Server is slow whereas select is quicker
I am trying to improve the performance of insert in SQL Server.
INSERT INTO [dbo].[GroupCustomerAccountProductRebates]([GroupId],[GroupName],[ProductId],[ManufacturerProductCode],[ProductDescription],[CustomerAccountId],[CustomerAccountNo],[CustomerAccountName],[RebatePercentage],[DatePeriodId])
SELECT GroupId, GroupName, ProductId, ManufacturerProductCode, ProductDescription, CustomerAccountId, CustomerAccountNo, CustomerAccountName, SUM(CAST(RebatePercentage AS decimal(18,2))) AS RebatePercentage, @DatePeriod
FROM
(SELECT *
FROM [dbo].[fnGetCustomerAccountFlatRateProductBrandRebatesByProduct] (NULL)
UNION
SELECT *
FROM [dbo].[fnGetCustomerAccountGroupInheritedFlatRateProductBrandRebatesByProduct] (NULL)
UNION
SELECT *
FROM [dbo].[fnGetCustomerAccountFlatRateProductRebatesByProduct] (NULL)
UNION
SELECT *
FROM [dbo].[fnGetCustomerAccountGroupInheritedFlatRateProductRebatesByProduct] (NULL)
UNION
SELECT *
FROM [dbo].[fnGetCustomerAccountTieredRebatesByProduct2] (NULL,@DatePeriod)
UNION
SELECT *
FROM [dbo].[fnGetCustomerAccountGroupInheritedTieredRebatesByProduct2] (NULL,@DatePeriod)
) x
GROUP BY
GroupId, GroupName, ProductId, ManufacturerProductCode, ProductDescription, CustomerAccountId, CustomerAccountNo, CustomerAccountName
ORDER BY
CustomerAccountName, ProductDescription
This is my query.
The SELECT
query takes around 36 seconds for those 820,000 rows, but running with insert query takes 3 minutes for the execution.
Below link is the execution plan : https://www.brentozar.com/pastetheplan/?id=B1zcWas8c
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|