简体   繁体   中英

How to partition table dynamically based on the number of rows in sql server?

I am doing some analysis to partition the tables.

Here are the tables

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[AnalysisMaster](
    [AnalysisMasterId] [bigint] NULL,
    [AnalysisDefinitionId] [bigint] NULL,
    [AnalysisDate] [date] NULL
) ON [PRIMARY]
GO



SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[AnalysisDefinition](
    [analysisid] [int] NOT NULL,
    [dataset] [int] NULL
) ON [PRIMARY]
GO

SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[OutputData](
    [analysisid] [int] NOT NULL,
    [derivativeId] [int] NOT NULL,
    [metric] [int] NOT NULL,
    [value] [decimal](18, 0) NOT NULL,
    [metricdate] [date] NULL
) ON [PRIMARY]
GO

The combination of analysisId and derivativeId is unique.

I am looking to partition an already existing table "OutputData" which has got 10 billion rows. There is a clustered index on analysisId, derivativeId and have a dozen stored procedures which use these two columns for querying the data.

Therefore one safest option I have is to use 'analysisId' to partition the table. All the Microsoft documentation, blogs I have come across have examples based on a date/year column. If I intend to take a strategy to partition the table based on number of rows say 10000000 (10 million), how can I do the same ? So the latest 10 million 'analysisId' are in the newest partition. What would a partitionFunction look like ? I intend to REBUILD INDEXES only on the newest partition. I want to have two filegroups, FG1(all other partitions) and FG2(newest partition).

Any issue with doing a simple partition function and setting the boundaries as your analysisID?

CREATE PARTITION FUNCTION PartFunc(INT)
AS RANGE LEFT
FOR VALUES(10000000, 20000000) ;

This would get you 3 partitions, your 1-10,000,000, then your 10-20mil, then 20mil+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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