我有一个存储过程,虽然工作正常,但使用了很多临时表,因此性能下降。是否可以改进/重写它(可能使用 CTE 或其他方式来提高性能)
CREATE PROCEDURE [dbo].[PORT_GetFutureOpportunities]
-- Add the parameters for the stored procedure here
(
@SiebelAccId VARCHAR(50),
@FromDate DATETIME,
@ToDate DATETIME,
@FilterCriteria INT,
@AutoRenewalChk INT
)
AS
BEGIN
DECLARE @FDate DATETIME, @TDate DATETIME
SET @FDate = DATEADD(day, DATEDIFF(day, 0, @FromDate), 0)
SET @TDate = DATEADD(day, DATEDIFF(day, 0, @ToDate), 0)
SELECT DISTINCT e.nai_grant_number
INTO #temp
FROM smbecommerce..SalesItem si
INNER JOIN smbecommerce..sales s ON s.sales_id = si.sales_id
INNER JOIN siebelextract..entitlement e ON SUBSTRING(e.nai_grant_number, 1, NULLIF(CHARINDEX('-', e.nai_grant_number) - 1, -1)) = CAST (s.sales_order_id AS VARCHAR)
WHERE si.auto_renewal_flag = (CASE WHEN @AutoRenewalChk = 1 THEN 1 END )
OR si.auto_renewal_flag <= ( CASE WHEN @AutoRenewalChk = 0 THEN 1 END)
--Creating Main query
SELECT
e.nai_agreement_account_name AS [CompanyName]
,c.first_name + ' ' + c.last_name AS [ContactName]
,c.work_phone AS [ContactPhone]
,c.email_address AS [EmailAddress]
,e.entitlement_end_date AS [ExpirationDate]
,e.nai_grant_number AS [GrantNumber]
,e.nai_quantity AS [Quantity]
,e.product_name AS [SkuDescription]
,(CASE WHEN LEN(e.nai_superceded_id) > 0 THEN 1 ELSE 0 END) AS [IsRenewed]
INTO #temp1
FROM siebelextract..entitlement e
INNER JOIN siebelextract..account a ON a.row_id = e.nai_reseller_id
INNER JOIN SiebelExtract.[dbo].contact c WITH (NOLOCK) ON e.nai_primary_contact_id = c.row_id
WHERE a.parent_account_id = @SiebelAccId
AND LEN(E.nai_reason_code) = 0
AND EXISTS (SELECT 1 FROM smbecommerce..renewalskus rs WHERE RS.sku = E.product_name)
AND e.entitlement_end_date
BETWEEN @FDate
AND @TDate
IF (@AutoRenewalChk = 0 OR @AutoRenewalChk = 1)
BEGIN
CREATE TABLE #temp2(
CompanyName VARCHAR(200)
,ContactName VARCHAR(200)
,ContactPhone VARCHAR(200)
,EmailAddress VARCHAR(200)
,ExpirationDate DATETIME
,GrantNumber VARCHAR(200)
,Quantity INT
,SkuDescription VARCHAR(200)
,IsRenewed INT
)
IF @AutoRenewalChk = 0
BEGIN
INSERT INTO #temp2 select #temp1.* FROM #temp1
END
IF @AutoRenewalChk = 1
BEGIN
INSERT INTO #temp2 SELECT * FROM #temp1 WHERE #temp1.[GrantNumber] NOT IN(SELECT t1.nai_grant_number FROM #temp t1)
END
END
IF @FilterCriteria = 0
BEGIN
SELECT te.[CompanyName],te.[ContactName],te.[EmailAddress],te.[ContactPhone],te.[SkuDescription],te.[Quantity],te.[GrantNumber],te.[ExpirationDate],te.[IsRenewed] FROM #temp2 te
END
IF @FilterCriteria = 1
BEGIN
SELECT te.[CompanyName],te.[ContactName],te.[EmailAddress],te.[ContactPhone],te.[SkuDescription],te.[Quantity],te.[GrantNumber],te.[ExpirationDate],te.[IsRenewed] FROM #temp2 te
WHERE te.[IsRenewed] > 0
END
IF @FilterCriteria = 2
BEGIN
SELECT te.[CompanyName],te.[ContactName],te.[EmailAddress],te.[ContactPhone],te.[SkuDescription],te.[Quantity],te.[GrantNumber],te.[ExpirationDate],te.[IsRenewed] FROM #temp2 te
WHERE te.[IsRenewed] = 0
END
DROP TABLE #temp2
DROP TABLE #temp1
DROP TABLE #temp
END
GO
谢谢