Ich habe eine partitionierte Tabelle (wie unten gezeigt) erstellt und 480 Millionen Zeilen gesetzt - ungefähr 181 Zeilen pro Konto.
Ich führe Basisabfragen aus, bevor ich Indizes hinzufüge. Ich war überrascht zu sehen, dass das Durchführen von Datumssuchen in der Partitionsspalte auch nach dem Hinzufügen nicht zur Eliminierung der Partition führte option(recompile)
. Ist das bei partitionierten Tabellen so? Mir scheint, dass dies eher dem wirklichen Leben gleicht als der harten Codierung der Partitionsspaltenwerte des Prädikats.
Tatsächlicher Ausführungsplan der Abfrage, bei dem die Partitionseliminierung zu erfolgen scheint.
Tatsächlicher Ausführungsplan der Abfrage, bei dem Partitionen nicht entfernt werden.
Schließlich werde ich Index (e) hinzufügen und hier posten, wenn ich Fragen dazu habe. Ich möchte nicht fortfahren, bis ich mit den Antworten in diesem Beitrag zufrieden bin.
--step 2 (after creating db)
ALTER DATABASE partitionresearch
ADD FILEGROUP January
GO
ALTER DATABASE partitionresearch
ADD FILEGROUP February
GO
ALTER DATABASE partitionresearch
ADD FILEGROUP March
GO
ALTER DATABASE partitionresearch
ADD FILEGROUP April
GO
ALTER DATABASE partitionresearch
ADD FILEGROUP May
GO
ALTER DATABASE partitionresearch
ADD FILEGROUP June
GO
ALTER DATABASE partitionresearch
ADD FILEGROUP July
GO
ALTER DATABASE partitionresearch
ADD FILEGROUP August
GO
ALTER DATABASE partitionresearch
ADD FILEGROUP September
GO
ALTER DATABASE partitionresearch
ADD FILEGROUP October
GO
ALTER DATABASE partitionresearch
ADD FILEGROUP November
GO
ALTER DATABASE partitionresearch
ADD FILEGROUP December
GO
--step 3
-- Table Partitioning in SQL Server
ALTER DATABASE [Partitionresearch]
ADD FILE
(
NAME = [PartJan],
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL14.mycompany2\MSSQL\DATA\PartJan.ndf',
SIZE = 5080 KB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 2040 KB
) TO FILEGROUP [January]
ALTER DATABASE [Partitionresearch]
ADD FILE
(
NAME = [PartFeb],
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL14.mycompany2\MSSQL\DATA\PartFeb.ndf',
SIZE = 5080 KB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 2040 KB
) TO FILEGROUP [February]
ALTER DATABASE [Partitionresearch]
ADD FILE
(
NAME = [PartMar],
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL14.mycompany2\MSSQL\DATA\PartMar.ndf',
SIZE = 5080 KB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 2040 KB
) TO FILEGROUP [March]
ALTER DATABASE [Partitionresearch]
ADD FILE
(
NAME = [PartApr],
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL14.mycompany2\MSSQL\DATA\PartApr.ndf',
SIZE = 5080 KB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 2040 KB
) TO FILEGROUP [April]
ALTER DATABASE [Partitionresearch]
ADD FILE
(
NAME = [PartMay],
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL14.mycompany2\MSSQL\DATA\PartMay.ndf',
SIZE = 5080 KB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 2040 KB
) TO FILEGROUP [May]
ALTER DATABASE [Partitionresearch]
ADD FILE
(
NAME = [PartJun],
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL14.mycompany2\MSSQL\DATA\PartJun.ndf',
SIZE = 5080 KB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 2040 KB
) TO FILEGROUP [June]
ALTER DATABASE [Partitionresearch]
ADD FILE
(
NAME = [PartJul],
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL14.mycompany2\MSSQL\DATA\PartJul.ndf',
SIZE = 5080 KB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 2040 KB
) TO FILEGROUP [July]
ALTER DATABASE [Partitionresearch]
ADD FILE
(
NAME = [PartAug],
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL14.mycompany2\MSSQL\DATA\PartAug.ndf',
SIZE = 5080 KB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 2040 KB
) TO FILEGROUP [August]
ALTER DATABASE [Partitionresearch]
ADD FILE
(
NAME = [PartSep],
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL14.mycompany2\MSSQL\DATA\PartSep.ndf',
SIZE = 5080 KB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 2040 KB
) TO FILEGROUP [September]
ALTER DATABASE [Partitionresearch]
ADD FILE
(
NAME = [PartOct],
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL14.mycompany2\MSSQL\DATA\PartOct.ndf',
SIZE = 5080 KB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 2040 KB
) TO FILEGROUP [October]
ALTER DATABASE [Partitionresearch]
ADD FILE
(
NAME = [PartNov],
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL14.mycompany2\MSSQL\DATA\PartNov.ndf',
SIZE = 5080 KB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 2040 KB
) TO FILEGROUP [November]
ALTER DATABASE [Partitionresearch]
ADD FILE
(
NAME = [PartDec],
FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL14.mycompany2\MSSQL\DATA\PartDec.ndf',
SIZE = 5080 KB,
MAXSIZE = UNLIMITED,
FILEGROWTH = 2040 KB
) TO FILEGROUP [December]
--step 4
-- Table Partitioning in SQL Server
USE Partitionresearch
GO
CREATE PARTITION FUNCTION [MonthlyPartition] (date)
AS RANGE RIGHT FOR VALUES ('20190201', '20190301', '20190401',
'20190501', '20190601', '20190701', '20190801',
'20190901', '20191001', '20191101', '20191201');
--step 5
-- Table Partitioning in SQL Server
USE Partitionresearch
GO
CREATE PARTITION SCHEME MonthWisePartition
AS PARTITION MonthlyPartition
TO (January, February, March, April, May, June, July,
August, September, October, November, December
);
--step 6
create table dbo.partitionresearch
(
tranid int identity(1,1),
[Date] date,
Account int,
SeqNumber tinyint,
AlertType int,
IsFirst tinyint,
Indicator1 int,
[time] time
)
on monthwisepartition([date])
--with partitioning help - 40 seconds (as opposed to 3 min 46 sec) , hovered over table scan and didnt see partition count, but clearly partitions (elimination) were used
--did see scalar operators with values 5 and 10 which happens to be where these accounts are partition wise (may and october)
use partitionresearch
select * from dbo.partitionresearch --hoverd over and closest thing to partn help i saw were scalar operators 5 and 10
where (date between '5/1/2019' and '5/31/2019' or date between '10/1/2019' and '10/31/2019') and
account in (1000000,2000000)
------------------------------------------------------------------------------------------------------------------------
--with "partition help" from a lookup table--3 minutes 33 seconds
use partitionresearch
select a.* from dbo.partitionresearch a--hovered over and believe partns wont be used
join [dbo].[monthlookup] b
on a.date=b.date
where account in (1000000,2000000)
------------------------------------------------------------------------------------------------------------------------
--this is the date lookup table which isnt partitioned, thus not aligned
USE [partitionresearch]
GO
/****** Object: Table [dbo].[monthlookup] Script Date: 7/12/2019 6:24:35 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[monthlookup](
[monthid] [int] IDENTITY(1,1) NOT NULL,
[Date] [date] NOT NULL
) ON [PRIMARY]
GO
quelle