Solved

SQL Server 2005  - Partition Existing Table with 11 million Rows

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

I previously wrote an article addressing the use of UBCD4WIN and SARDU. All are great, but I have always been an advocate of SARDU. Recently it was suggested that I go back and take a look at Easy2Boot in comparison.
The question appears often enough, how do I transfer my data from my old server to the new server while preserving file shares, share permissions, and NTFS permisions.  Here are my tips for handling such a transfer.
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 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…

777 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