大家好,我有一张包含以下代码的表格
CREATE TABLE [dbo].[PTableMediumPart](
[PK] [int] NOT NULL,
[Col1] [int] NOT NULL,
[Col2] [int] NULL,
[Col3] [money] NOT NULL,
[Col4] [money] NULL,
[Col5] [nvarchar](60) NOT NULL,
[Col6] [nvarchar](60) NULL,
[Col7] [varchar](255) NOT NULL,
[Col8] [varchar](255) NULL,
[Col9] [smallint] NOT NULL,
[Col10] [smallint] NULL,
[Col11] [decimal](20, 3) NOT NULL,
[Col12] [decimal](20, 3) NULL,
[Col13] [char](8) NOT NULL,
[Col14] [datetime2](7) NULL,
[PartitionKey] [tinyint] NOT NULL
)
GO
partitionkey列填充了CAST((PK%3) AS TINYINT)
和
我已经将分区功能和方案定义如下
CREATE PARTITION FUNCTION [PFTest](tinyint) AS RANGE LEFT FOR VALUES (0, 1)
GO
CREATE PARTITION SCHEME [PSTEST] AS PARTITION [PFTest] TO ([FGTest1], [FGTest2], [FGTest3])
GO
然后我创建了像
这样的聚簇索引CREATE UNIQUE CLUSTERED INDEX [CI_PArt] ON [dbo].[PTableMediumPart]
(
[PK] ASC,
[PartitionKey] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PSTEST]([PartitionKey])
GO
但是当我使用相同的未分区表运行查询时,我获得了更好的性能或相同的查询性能,例如,当我运行以下查询时,我得到50-50%的排除计划,而在其他情况下,未分区的表获得更好的执行计划,为什么会这样?我在这里错了什么?
SELECT *
FROM dbo.PTableMedium
WHERE PK = 789
SELECT *
FROM dbo.PTableMediumPart
WHERE PK = 789 AND PartitionKey = CAST((789 % 3) AS TINYINT)
答案 0 :(得分:2)
在我的测试(SQL Server 2008)中,两个执行计划都有不同的成本:第一个查询为75%,第二个查询为25%:
另外,在我的测试中
1..3
)和789 % 3
),第二个查询将只访问一个分区(0
= AND PartitionKey = expression
=>是加入分区[0..1))键/列(CREATE TABLE ... ON [PSTEST]([PartitionKey])
)。此谓词/条件允许partition elimination。因此,SQL Server将仅在一个分区中寻找具有PK = 789
的行。此外,请参阅此博客:http://blog.kejser.org/2014/01/15/curious-partition-function-behaviour/