简体   繁体   English

SQL Group By with Count很慢

[英]SQL Group By with Count is slow

I have a table with close to 3 million rows that has 5-10 updates/inserts every second. 我有一个近300万行的表,每秒有5-10个更新/插入。 Each row is assigned a category, and I want to group by the category to count the total number of rows for each category. 每行都分配了一个类别,我想按类别进行分组,以计算每个类别的总行数。

Select CategoryId
     , COUNT(*) as TotalRows
  FROM Table1
 WHERE SaleTypeId = 2 AND CategoryId > 1
 GROUP BY CategoryId

Table Schema: 表格架构:

CREATE TABLE [dbo].[Table1](
[SaleId]      INT IDENTITY (1, 1) NOT NULL,
[SaleTypeId]  INT                 NOT NULL,
[CategoryId]  INT                 NULL)

Primary Key: 首要的关键:

 ADD CONSTRAINT [PK_Table1] 
 PRIMARY KEY CLUSTERED ([SaleId] ASC) 
 WITH (ALLOW_PAGE_LOCKS = ON, ALLOW_ROW_LOCKS = ON, PAD_INDEX = OFF, 
       IGNORE_DUP_KEY = OFF, STATISTICS_NORECOMPUTE = OFF);

I have a non-clustered index on the table: 我在表上有一个非聚集索引:

CREATE NONCLUSTERED INDEX [Index1] ON [dbo].[Table1]
(
    [SaleTypeId] ASC,
    [CategoryId] ASC    
)

Query Plan: 查询计划:

在此输入图像描述

The query takes 40 to 60 seconds to run, and it looks like a lot of data is being read in the index seek operation. 查询需要40到60秒才能运行,看起来在索引查找操作中正在读取大量数据。 Is there any way to speed up this query? 有没有办法加快这个查询? I have read that count gets slower on bigger data sets and that there are quicker ways to get the count of an entire table, but I need to get the count by the category. 我已经读过,在较大的数据集上计数变慢,并且有更快的方法来获取整个表的计数,但我需要按类别计算。

Reverse the columns order in the nonclustered index, like this: 反转非聚集索引中的列顺序,如下所示:

CREATE NONCLUSTERED INDEX [Index1] ON [dbo].[Table1]
(
    [CategoryId] ASC,    
    [SaleTypeId] ASC
)

Try running this, I would also put index as @dean suggested 尝试运行这个,我也会把索引作为@dean建议

Select CategoryId, COUNT(CategoryId) as TotalRows
 FROM Table1 WITH (NOLOCK)
 WHERE SaleTypeId = 2 AND CategoryId > 1
 GROUP BY CategoryId

I ended up running this as a nightly aggregate job and storing the result in an aggregate table. 我最终将其作为夜间聚合作业运行并将结果存储在聚合表中。 It doesn't provide up to date results (which, after deliberation, we can live with) nor is the nightly query any faster, but reading from the aggregate table is a lot faster. 它没有提供最新的结果(经过审议,我们可以忍受),夜间查询也不会更快,但从汇总表中读取要快得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM