提高SQL Server中大型表的SELECT查询性能

时间:2013-07-09 09:04:09

标签: sql performance tsql sql-server-2005 query-performance

问:如何改善查询的效果?

详细信息: 我有一个包含200k记录(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  
)

2 个答案:

答案 0 :(得分:0)

由于200k记录不是很大而且查询需要很长时间,因此您很可能错过了一个重要的索引。在您经常选择的列上具有聚簇索引和标识列或非聚集索引,即year

还尝试明确说明列名,实际上并不需要SELECT *,每次运行查询时是否打算带回200k行?

要创建非聚集索引,您可以执行以下操作:

CREATE NONCLUSTERED INDEX IX_SALES_Year 
    ON dbo.Sales (Year); 

您可能希望应用其中的一些并运行查询以查看哪一个最佳。我还建议查看数据库调优向导,这可能会为您提供更多指南。

答案 1 :(得分:0)

首先,如果这是我的表,我将创建一个数字主键而不是varchar composite clustered index。请记住,非数字索引总是慢于数字列索引。

其次,尝试在查询中使用更多的列上创建非聚集索引,因此良好的公式是根据需要使用主数字索引和更多非聚簇(数字或非数字)索引。

第三个总是避免在连接中使用部分列值,这样可以工作,但会极大地降低选择时的性能。为此,您可以为这些值创建更多列,并在更新或插入时保存。这将在更新或插入时花费数秒,但在使用完整列名称或与这些列连接时节省了很多。

这些东西在我14年的数据库工作中总是帮助我,包括Tereadata和SQL Server中非常庞大的表。我希望这些技巧可以帮助你和其他人。