我们正在使用包含约 50M 记录的聚集列存储索引表,与使用相同的数据库架构和数据(刚刚导出和导入 bak 文件)在本地运行相比,在GCP云 sql 上运行时会遇到很大的性能下降。
使用下面的查询,我们发现GCP云sql( https://www.brentozar.com/pastetheplan/?id=ByexjqpCF )上的执行计划没有在索引扫描上使用projectId谓词,而是仅将其应用于进一步的过滤步骤。在本地运行时(https://www.brentozar.com/pastetheplan/?id=rJLO59pRK),谓词被推入索引扫描,从而减少扫描行数并提高性能。
造成这种差异的原因是什么?
SELECT YEAR(ReferenceDate) RefDateYear, MONTH(ReferenceDate) RefDateMonth,sum(diffsum) DiffSum
into #res
FROM Journal jp
WHERE jp.projectId='582b02e2-add0-4b50-94f7-4e7e07497cf6' AND ReferenceDate < '20220101' AND batch != 9998
GROUP BY YEAR(ReferenceDate), MONTH(ReferenceDate)
Run Code Online (Sandbox Code Playgroud)
另请参阅表架构:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Journal](
[id] [int] IDENTITY(1,1) NOT NULL,
[projectId] [uniqueidentifier] NOT NULL,
[diffSum] [money] NOT NULL,
[batch] [int] NOT NULL,
[referenceDate] [datetime2](7) NOT NULL,
...
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Journal] …Run Code Online (Sandbox Code Playgroud)