Solved

SQL Server 2005  - Partition Existing Table with 11 million Rows

Posted on 2010-08-18
3
328 Views
Last Modified: 2012-05-10
Hello, I'd like to partition a large table containing products for a price comparison type search system.

Right now it takes a bit too much time to pull records from the table.  it's fairly acceptable, but I suspect it can be much faster with partioning.

I'd like to partition by the first letter of the store's name.  I've already setup a column which stores this data.

My main issue is, I understand how i'd like to break this up, but I don't understand how to implement.

If anyone could give me extremely detailed code or even point me to an existing 3rd party tool which can do this for me I'd appreciate it.

The table name is products, the column I created with the A-Z value is ProgramNameStart.

If I need to provide more details please do let me know,
0
Comment
Question by:susanys
  • 2
3 Comments
 
LVL 76

Expert Comment

by:arnold
ID: 33467494
0
 
LVL 39

Expert Comment

by:lcohan
ID: 37047405
Here's how it should be done and I took a simplified "Products" table like below but it should self explanatory:

--Products

--Add partition function
--drop PARTITION FUNCTION PF_Products
CREATE PARTITION FUNCTION PF_Products (varchar(256))
AS RANGE RIGHT FOR VALUES
(
      'b', -- < a
      'c', -- >= a and < b
      'd'  -- > c
);

--Adding Partition Scheme
--DROP PARTITION SCHEME PS_Products
CREATE PARTITION SCHEME PS_Products  AS PARTITION PF_Products  TO
(
OneShopMail_P1,
OneShopMail_P2,
OneShopMail_P3,
OneShopMail_P4
)

--DROP TABLE [Products]
CREATE TABLE [dbo].[Products_test]
(
      [ProductId] bigint NOT NULL,
      [ProductName] varchar(256) NOT NULL,
      [StoreName] varchar(256) NOT NULL,
      CreatedOn date DEFAULT(cast(getdate() as date))  NOT NULL
  CONSTRAINT PK_Products PRIMARY KEY CLUSTERED
(ProductId,StoreName
) ON PS_Products(StoreName)
) ON PS_Products(StoreName);
GO


insert  into dbo.Products_test (ProductId,ProductName,StoreName,CreatedOn)
select       1,      'p1',      'ala',      '20100101'
union
select       2,      'p2',      'bala',      '20101231'
union
select       3,      'p3',      'cala',      '20110101'
union
select       4,      'p4',      'ada',      '20111231'
union
select       5,      'p5',      'bada',      '20120101'
union
select       6,      'p6',      'cuda',      '20121231'
union
select       7,      'p7',      'doda',      '20130101'
union
select       8,      'p8',      'moda',      '20131231'

--delete from       Products_test

SELECT $PARTITION.PF_Products(StoreName) AS Partition,
            COUNT(*) AS [COUNT] FROM dbo.Products_test
GROUP BY $PARTITION.PF_Products(StoreName)
ORDER BY Partition ;
GO

SELECT $PARTITION.PF_Products(StoreName) AS Partition,
            * FROM dbo.Products_test
GO


0
 
LVL 39

Accepted Solution

by:
lcohan earned 500 total points
ID: 37047434
OneShopMail_P1,
OneShopMail_P2,
OneShopMail_P3,
OneShopMail_P4

are four different filegroups I have in one of my db's however you can replace them all with PRIMARY but obviously you should have a filegroup per partition and idealy one physical file per filegroup.

updated script below:

--Products

--Add partition function
--drop PARTITION FUNCTION PF_Products
CREATE PARTITION FUNCTION PF_Products (varchar(256))
AS RANGE RIGHT FOR VALUES
(
      'b', -- < a
      'c', -- >= a and < b
      'd'  -- > c
);

--Adding Partition Scheme
--DROP PARTITION SCHEME PS_Products
CREATE PARTITION SCHEME PS_Products  AS PARTITION PF_Products  TO
(
[primary],
[primary],
[primary],
[primary]
)

--DROP TABLE [Products_test]
CREATE TABLE [dbo].[Products_test]
(
      [ProductId] bigint NOT NULL,
      [ProductName] varchar(256) NOT NULL,
      [StoreName] varchar(256) NOT NULL,
      CreatedOn date DEFAULT(cast(getdate() as date))  NOT NULL
  CONSTRAINT PK_Products PRIMARY KEY CLUSTERED
(ProductId,StoreName
) ON PS_Products(StoreName)
) ON PS_Products(StoreName);
GO


insert  into dbo.Products_test (ProductId,ProductName,StoreName,CreatedOn)
select       1,      'p1',      'ala',      '20100101'
union
select       2,      'p2',      'bala',      '20101231'
union
select       3,      'p3',      'cala',      '20110101'
union
select       4,      'p4',      'ada',      '20111231'
union
select       5,      'p5',      'bada',      '20120101'
union
select       6,      'p6',      'cuda',      '20121231'
union
select       7,      'p7',      'doda',      '20130101'
union
select       8,      'p8',      'moda',      '20131231'

--delete from       Products_test

SELECT $PARTITION.PF_Products(StoreName) AS Partition,
            COUNT(*) AS [COUNT] FROM dbo.Products_test
GROUP BY $PARTITION.PF_Products(StoreName)
ORDER BY Partition ;
GO

SELECT $PARTITION.PF_Products(StoreName) AS Partition,
            * FROM dbo.Products_test
GO
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Suggested Solutions

We all have limited time to study long and complicated information about RAID theories, but you may be interested as to how RAID 5 works. We made it simple for you by providing the shortest and easiest explanation ever.   First we need to remind …
Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
This video teaches viewers how to encrypt an external drive that requires a password to read and edit the drive. All tasks are done in Disk Utility. Plug in the external drive you wish to encrypt: Make sure all previous data on the drive has been …
This tutorial will walk an individual through the process of installing the necessary services and then configuring a Windows Server 2012 system as an iSCSI target. To install the necessary roles, go to Server Manager, and select Add Roles and Featu…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now