Solved

SQL Server 2005  - Partition Existing Table with 11 million Rows

Posted on 2010-08-18
3
377 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40

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 40

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
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…

623 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