Solved

SQL Server 2005  - Partition Existing Table with 11 million Rows

Posted on 2010-08-18
3
341 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 77

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Lets start to have a small explanation what is VAAI(vStorage API for Array Integration ) and what are the benefits using it. VAAI is an API framework in VMware that enable some Storage tasks. It first presented in ESXi 4.1, but only after 5.x sup…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…
This Micro Tutorial will teach you how to reformat your flash drive. Sometimes your flash drive may have issues carrying files so this will completely restore it to manufacturing settings. Make sure to backup all files before reformatting. This w…

863 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

25 Experts available now in Live!

Get 1:1 Help Now