On one of my chats with my fellow SQL Server PFE’s, we started talking about objects that have millions and millions of records, how accessing them can be really slow, and of course its administration can be a real pain. With these in mind we starts talking about Partitioned Tables, that in simple words is broke a millions records table on many parts.
This feature is enabled on SQL Server 2005, SQL Server 2008, SQL Server 2008 R2. To generate the partitioned tables you will need to identify the key (column) that you are going to use to partition the table. To create the partitioned table you need to execute these steps:
CREATE PARTITION FUNCTION pfAnualR(int) AS RANGE RIGHT FOR VALUES(2008,2009,2010,2011)
CREATE PARTITION FUNCTION pfAnualL(int) AS RANGE LEFT FOR VALUES(2008,2009,2010,2011)
Primera Partición < 2008
Primera Partición <= 2008
Segunda Partición >= 2008
Segunda Partición > 2008
Tercera Partición >= 2009
Tercera Particion > 2009
Cuarta Partición >= 2010
Cuarta Particion > 2010
Quinta Partición >= 2011
Quinta Particion > 2011
CREATE PARTITION SCHEME psAnualR AS PARTITION pfAnualR TO ([FG01], [FG02], [FG03], [FG04], [FG05])
CREATE PARTITION SCHEME psAnualL AS PARTITION pfAnualL TO ([FG01], [FG02], [FG03], [FG04], [FG05])
CREATE TABLE tblEstadosR (Ano int, Mes int, Dia int, Tipo varchar(30), Importe float) ON psAnualR(Ano)
CREATE TABLE tblEstadosL (Ano int, Mes int, Dia int, Tipo varchar(30), Importe float) ON psanualL(Ano)
INSERT INTO tblEstadosR (Ano, Mes, Dia, Tipo, Importe) VALUES (2007,08,22,'Cargo',13.00) INSERT INTO tblEstadosR (Ano, Mes, Dia, Tipo, Importe) VALUES (2008,08,22,'Cargo',34.00)INSERT INTO tblEstadosR (Ano, Mes, Dia, Tipo, Importe) VALUES (2009,08,22,'Cargo',23.00)INSERT INTO tblEstadosR (Ano, Mes, Dia, Tipo, Importe) VALUES (2010,08,22,'Cargo',78.00)INSERT INTO tblEstadosR (Ano, Mes, Dia, Tipo, Importe) VALUES (2011,08,22,'Cargo',98.00)INSERT INTO tblEstadosR (Ano, Mes, Dia, Tipo, Importe) VALUES (2012,08,22,'Cargo',14.00) GO
INSERT INTO tblEstadosL (Ano, Mes, Dia, Tipo, Importe) VALUES (2007,08,22,'Cargo',13.00)INSERT INTO tblEstadosL (Ano, Mes, Dia, Tipo, Importe) VALUES (2008,08,22,'Cargo',34.00)INSERT INTO tblEstadosL (Ano, Mes, Dia, Tipo, Importe) VALUES (2009,08,22,'Cargo',23.00)INSERT INTO tblEstadosL (Ano, Mes, Dia, Tipo, Importe) VALUES (2010,08,22,'Cargo',78.00)INSERT INTO tblEstadosL (Ano, Mes, Dia, Tipo, Importe) VALUES (2011,08,22,'Cargo',98.00)INSERT INTO tblEstadosL (Ano, Mes, Dia, Tipo, Importe) VALUES (2012,08,22,'Cargo',14.00) GO
If you want to know on which partition a piece of data reside, use the following TSQL Command $PARTITION, this command returns the partition number on which the data is placed. Review the following example:
SELECT Ano, BDPrueba.$Partition.pfAnualR(Ano) FROM tblEstadosR
SELECT Ano, BDPrueba.$Partition.pfAnualL(Ano) FROM tblEstadosL
This Query results enhanced the RIGHT and LEFT range explanation.
Primera Partición < 2008 Segunda Partición >= 2008 Tercera Partición >= 2009 Cuarta Partición >= 2010 Quinta Partición >= 2011 Quita Particion >= 2011
Primera Partición <= 2008 Primera Partición > 2008 Segunda Partición > 2009 Tercera Partición > 2010 Cuarta Partición > 2011 Quinta Particion > 2011
On this post you have learned the basic concept son table partitioning, some of its advantages are:
- Sliding Windows scenario
- Enhance Administrative activities, like:
Because of the parallelism some queries can perform slower using partitioned tables than using regular tables
On another posts we will discuss topics like:
- Advantage and disadvantage on partitioned tables.
- Considerations on implementing Sliding Windows.
“The opinions and views expressed in this blog are those of the author and do not necessarily state or reflect those of Microsoft”