database table partition

Some database tables grow and become a super large tables. Usually we either archive or partition the large tables. There are pros and cons to everything.

Here I am showing the partition details:

There are 2 different ways to partition the table.

One way is to create a brand new partitioned table and then copy the data from your existing table into the new table and do a table rename. This is not practical for the existing database since the table maybe too large.

Another way is to partition the existing table in place by rebuilding or creating a clustered index on the table, this is commonly used to improve the performance on the existing program/database, here is the details:

  1. Generate a sample table:

CREATE TABLE [dbo].[tblPartition](

[ID] [int] IDENTITY(1,1) NOT NULL,

[Name] [varchar](50) NOT NULL,

[dtCreated] [datetime] NOT NULL,

CONSTRAINT [PK_Partition] PRIMARY KEY CLUSTERED

(

[ID] ASC

)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

) ON [PRIMARY]

GO

ALTER TABLE [dbo].[tblPartition] ADD CONSTRAINT [DF_Partition_dtCreated] DEFAULT (getdate()) FOR [dtCreated]

GO

2. load testing data:

DECLARE @val INT

SELECT @val=1

WHILE @val < 1000

BEGIN

INSERT INTO dbo.tblPartition([name],[dtCreated])

VALUES ('Daniel TEST' + CONVERT(varchar(10), @val),getdate()-1500 +@val)

SELECT @val=@val+1

END

go

3. Search the sys.partitions system view we can see how only 1 partition on the target table(i.e. tblPartition).

SELECT o.name objectname,i.name indexname, partition_id, partition_number, [rows]

FROM sys.partitions p

INNER JOIN sys.objects o ON o.object_id=p.object_id

INNER JOIN sys.indexes i ON i.object_id=p.object_id and p.index_id=i.index_id

WHERE o.name LIKE '%tblPartition%'

4. In order to create a partitioned table we'll need to first create a partition function and partition scheme. For our example we are going to partition the table based on the datetime column. Here is the code to create these objects and check some of their metadata in the system views.

CREATE PARTITION FUNCTION PartitionFunctionDT (datetime)

AS RANGE RIGHT FOR VALUES ('20240301', '20240601','20240901','20250101')

GO

CREATE PARTITION SCHEME PartitionSchemeDT

AS PARTITION PartitionFunctionDT ALL TO ([PRIMARY])

GO

We can view the Partition Functions and Schemes by running following query to verify:

SELECT * FROM sys.partition_schemes;

SELECT * FROM sys.partition_functions;

go

5. we are going to partition the table using a clustered index. if the table already has a clustered index defined we'll need to drop this index first and recreate the constraint using a non-clustered index.

ALTER TABLE dbo.tblPartition DROP CONSTRAINT PK_Partition

GO

ALTER TABLE dbo.tblPartition ADD CONSTRAINT PK_Partition PRIMARY KEY NONCLUSTERED (ID)

WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,

ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]

GO

CREATE CLUSTERED INDEX IX_TABLE1_partitioncol ON dbo.tblPartition (dtCreated)

WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,

ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)

ON PartitionSchemeDT(dtCreated)

GO

6. Now we can verify 5 partitions created on this table with following query:

SELECT o.name objectname,i.name indexname, partition_id, partition_number, [rows]

FROM sys.partitions p

INNER JOIN sys.objects o ON o.object_id=p.object_id

INNER JOIN sys.indexes i ON i.object_id=p.object_id and p.index_id=i.index_id

WHERE o.name LIKE '%tblPartition%'

GO

We had partitioned a table now. We may need to re-partition the table if the table grows quickly. I can share how to re-partition the existing table later.

要查看或添加评论,请登录

Daniel Liu的更多文章

  • unable to access to SQL server database diagram

    unable to access to SQL server database diagram

    I login as sa to a SQL server, trying to create a new database diagram, but unable to access it. Got following error…

  • SQL group by sample query

    SQL group by sample query

    query 1: --use group by rollup() select isNull(CONVERT(VARCHAR(4), FILED_RECEIVED_DATE, 126), case when…

  • Validate XML against a schema

    Validate XML against a schema

    private static string ValidateXML(string xsd, string strXml,string emailfrom,string emailto) { string ret = ""; string…

  • C# get first friday in any month, any year

    C# get first friday in any month, any year

    public string GetFirstFriday(int year,int month) { string friday=null; for (int i = 1; i 8; i++) { var dtmDate = new…

  • Blob data and PDF conversion in C#

    Blob data and PDF conversion in C#

    save Blob data from oracle to PDF file: private static void SaveBlobToPDF(string constr, string filing_id) { using…

  • Replace Apostrophe in SQL linked server query

    Replace Apostrophe in SQL linked server query

    I need to search employee's last name from SQL server to DB2 database, one lastname like O'NEIL cause the error, here…

  • send email from c#

    send email from c#

    using System.Net.

  • read email from active directory

    read email from active directory

    private static string GetEmailAddressLDAP(string strEnv, string empno) { string strLDAPConnection; string strLDAPId;…

  • Multi-tenant Database design

    Multi-tenant Database design

    There are 4 options to design Multi-tenant database: 1. One big database, one schema(dbo): In this approach, database…

  • Create Excel from List<T>

    Create Excel from List<T>

    I need to generate the excel file by querying database everyday. All the data returned as List.

社区洞察

其他会员也浏览了