问:如何提高查询的性能?
详细信息:
我有一张有 20 万条记录的表(Sales)
和一个函数getView_sls(@TON_orNPS)
以下查询需要10-12 秒
SELECT * FROM Sales
以下查询需要32-34 秒
SELECT * FROM getView_sls('TON')
下面是我的销售表结构:
USE [WaterfallDB]
GO
/****** Object: Table [dbo].[Sales] Script Date: 07/09/2013 11:39:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Sales](
[year] [varchar](4) NOT NULL,
[nslschnl] [varchar](2) NULL,
[distchnl] [varchar](2) NULL,
[chl6] [varchar](20) NOT NULL,
[sku] [varchar](15) NOT NULL,
[ton01] [float] NULL,
[ton02] [float] NULL,
[ton03] [float] NULL,
[ton04] [float] NULL,
[ton05] [float] NULL,
[ton06] [float] NULL,
[ton07] [float] NULL,
[ton08] [float] NULL,
[ton09] [float] NULL,
[ton10] [float] NULL,
[ton11] [float] NULL,
[ton12] [float] NULL,
[nps01] [float] NULL,
[nps02] [float] NULL,
[nps03] [float] NULL,
[nps04] [float] NULL,
[nps05] [float] NULL,
[nps06] [float] NULL,
[nps07] [float] NULL,
[nps08] [float] NULL,
[nps09] [float] NULL,
[nps10] [float] NULL,
[nps11] [float] NULL,
[nps12] [float] NULL,
CONSTRAINT [PK_Sales] PRIMARY KEY CLUSTERED
(
[year] ASC,
[chl6] ASC,
[sku] 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
下面是我的功能
USE [WaterfallDB]
GO
/****** Object: UserDefinedFunction [dbo].[getView_sls] Script Date: 07/09/2013 11:55:56 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[getView_sls] (@TON_or_NPS nvarchar(10))
RETURNS TABLE
AS
RETURN(
SELECT
dbo.Sales.year,
dbo.Sales.nslschnl,
dbo.Sales.distchnl,
SUBSTRING(dbo.Sales.chl6,12,7) AS chl6,
BI.dbo.View_ch.chl5,
BI.dbo.View_ch.chl4,
BI.dbo.View_ch.chl3,
dbo.Sales.sku,
ISNULL(BI.dbo.SKU.descr, ISNULL(dbo.Sales.SKU,'Undefined')) AS SKU_descr,
BI.dbo.SKU.bp,
BI.dbo.SKU.ccatg,
SUBSTRING(BI.dbo.SKU.phl5,0,3) AS phl1,
ISNULL(BI.dbo.phl1.descr, ISNULL(CAST(SUBSTRING(BI.dbo.SKU.phl5,0,3) AS VARCHAR(13)),'Uncategorized')) AS phl1_descr,
SUBSTRING(BI.dbo.SKU.phl5,0,4) AS phl2,
ISNULL(BI.dbo.phl2.descr, ISNULL(CAST(SUBSTRING(BI.dbo.SKU.phl5,0,4) AS VARCHAR(13)),'Uncategorized')) AS phl2_descr,
SUBSTRING(BI.dbo.SKU.phl5,0,6) AS phl3,
ISNULL(BI.dbo.phl3.descr, ISNULL(CAST(SUBSTRING(BI.dbo.SKU.phl5,0,6) AS VARCHAR(13)),'Uncategorized')) AS phl3_descr,
SUBSTRING(BI.dbo.SKU.phl5,0,10) AS phl4,
ISNULL(BI.dbo.phl4.descr, ISNULL(CAST(SUBSTRING(BI.dbo.SKU.phl5,0,10) AS VARCHAR(13)),'Uncategorized')) AS phl4_descr,
BI.dbo.SKU.phl5 AS phl5,
ISNULL(BI.dbo.phl5.descr, ISNULL(BI.dbo.SKU.phl5,'Uncategorized')) AS phl5_descr,
BI.dbo.SKU.crpbrd,
ISNULL(BI.dbo.crpbrd.descr, ISNULL(BI.dbo.SKU.crpbrd,'Uncategorized')) AS crpbrd_descr,
BI.dbo.SKU.rngbrd,
ISNULL(BI.dbo.rngbrd.descr, ISNULL(BI.dbo.SKU.rngbrd,'Uncategorized')) AS rngbrd_descr,
BI.dbo.SKU.brdden,
ISNULL(BI.dbo.brdden.descr, ISNULL(BI.dbo.SKU.brdden,'Uncategorized')) AS brdden_descr,
CASE @TON_or_NPS WHEN 'TON' THEN dbo.Sales.ton01 / 1000 ELSE dbo.Sales.nps01 / 1000000 END AS ton01,
CASE @TON_or_NPS WHEN 'TON' THEN dbo.Sales.ton02 / 1000 ELSE dbo.Sales.nps02 / 1000000 END AS ton02,
CASE @TON_or_NPS WHEN 'TON' THEN dbo.Sales.ton03 / 1000 ELSE dbo.Sales.nps03 / 1000000 END AS ton03,
CASE @TON_or_NPS WHEN 'TON' THEN dbo.Sales.ton04 / 1000 ELSE dbo.Sales.nps04 / 1000000 END AS ton04,
CASE @TON_or_NPS WHEN 'TON' THEN dbo.Sales.ton05 / 1000 ELSE dbo.Sales.nps05 / 1000000 END AS ton05,
CASE @TON_or_NPS WHEN 'TON' THEN dbo.Sales.ton06 / 1000 ELSE dbo.Sales.nps06 / 1000000 END AS ton06,
CASE @TON_or_NPS WHEN 'TON' THEN dbo.Sales.ton07 / 1000 ELSE dbo.Sales.nps07 / 1000000 END AS ton07,
CASE @TON_or_NPS WHEN 'TON' THEN dbo.Sales.ton08 / 1000 ELSE dbo.Sales.nps08 / 1000000 END AS ton08,
CASE @TON_or_NPS WHEN 'TON' THEN dbo.Sales.ton09 / 1000 ELSE dbo.Sales.nps09 / 1000000 END AS ton09,
CASE @TON_or_NPS WHEN 'TON' THEN dbo.Sales.ton10 / 1000 ELSE dbo.Sales.nps10 / 1000000 END AS ton10,
CASE @TON_or_NPS WHEN 'TON' THEN dbo.Sales.ton11 / 1000 ELSE dbo.Sales.nps11 / 1000000 END AS ton11,
CASE @TON_or_NPS WHEN 'TON' THEN dbo.Sales.ton12 / 1000 ELSE dbo.Sales.nps12 / 1000000 END AS ton12
FROM
dbo.Sales
LEFT OUTER JOIN
BI.dbo.SKU ON dbo.Sales.sku = BI.dbo.SKU.sku
LEFT OUTER JOIN
BI.dbo.View_ch ON dbo.Sales.distchnl = BI.dbo.View_ch.distchnl AND SUBSTRING(dbo.Sales.chl6,12,7) = BI.dbo.View_ch.chl6
LEFT OUTER JOIN
BI.dbo.crpbrd ON BI.dbo.SKU.crpbrd = BI.dbo.crpbrd.crpbrd
LEFT OUTER JOIN
BI.dbo.rngbrd ON BI.dbo.SKU.rngbrd = BI.dbo.rngbrd.rngbrd
LEFT OUTER JOIN
BI.dbo.brdden ON BI.dbo.SKU.brdden = BI.dbo.brdden.brdden
LEFT OUTER JOIN
BI.dbo.phl1 ON SUBSTRING(BI.dbo.SKU.phl5,0,3) = BI.dbo.phl1.phl1
LEFT OUTER JOIN
BI.dbo.phl2 ON SUBSTRING(BI.dbo.SKU.phl5,0,4) = BI.dbo.phl2.phl2
LEFT OUTER JOIN
BI.dbo.phl3 ON SUBSTRING(BI.dbo.SKU.phl5,0,6) = BI.dbo.phl3.phl3
LEFT OUTER JOIN
BI.dbo.phl4 ON SUBSTRING(BI.dbo.SKU.phl5,0,10) = BI.dbo.phl4.phl4
LEFT OUTER JOIN
BI.dbo.phl5 ON BI.dbo.SKU.phl5= BI.dbo.phl5.phl5
)