加速Group By和count SQL查询



我用下面的语句计数/分组大约130万条记录。查询工作,但需要大约一分半钟,这对我的应用程序来说太长了。

目标是得到列出的每个字段一个(没有重复)。当前查询返回大约846行。到目前为止,我还没有任何索引,也不知道如何添加它们。

SELECT 
[OfferId]
,[Name] 
,COUNT([Name]) AS 'Count'
,[Offer]
,[Title]
,[Text]
,[Amount]
,[Start]
,[End]
,[Image]
,[ImageText]
,[Type]
,[Disclaimer]
,[Link]
,[Status]
FROM 
ClientDB.[dbo].[Offers]
GROUP BY 
[OfferId]
,[Name]
,[Offer]
,[Title]
,[Text]
,[Amount]
,[Start]
,[End]
,[Image]
,[ImageText]
,[Type]
,[Disclaimer]
,[Link]
,[Status]

表结构(不确定如何索引以使其更快):

CREATE TABLE [dbo].[Offers]
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[Company] [nvarchar](max) NULL,
[Property] [nvarchar](max) NULL,
[Account] [int] NULL,
[OfferID] [nvarchar](50) NULL,
[Offer] [nvarchar](50) NULL,
[Title] [nvarchar](30) NULL,
[Text] [nvarchar](max) NULL,
[AwardCode] [nvarchar](150) NULL,
[Amount] [decimal](18, 2) NULL,
[Start] [datetime] NULL,
[End] [datetime] NULL,
[Image] [nvarchar](max) NULL,
[ImageText] [nvarchar](250) NULL,
[Type] [nvarchar](50) NULL,
[CampaignTier] [nvarchar](50) NULL,
[Name] [nvarchar](max) NULL,
[Disclaimer] [nvarchar](max) NULL,
[Status] [nvarchar](100) NULL,
CONSTRAINT [PK_Offers] 
PRIMARY KEY CLUSTERED ([ID] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, 
IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, 
ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

我为分组的字段添加了索引,这将时间从一分半减少到1秒。

相关内容

  • 没有找到相关文章

最新更新