Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

SQL Server 2005  - Partition Existing Table with 11 million Rows

Posted on 2010-08-18
3
Medium Priority
?
386 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 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 2000 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Each year, investment in cloud platforms grows more than 20% (https://www.immun.io/hubfs/Immunio_2016/Content/Marketing/Cloud-Security-Report-2016.pdf?submissionGuid=a8d80a00-6fee-4b85-81db-a4e28f681762) as an increasing number of companies begin to…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
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…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

971 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