Anda di halaman 1dari 6

Particin en mysqy

http://dev.mysql.com/doc/refman/5.6/en/partitioning-managementexchange.html
Particionar tablas en MySQL nos permite rotar la informacin de nuestras tablas en diferentes particiones,
consiguiendo as realizar consultas ms rpidas y recuperar espacio en disco al borrar los registros. El uso
ms comn de particionado es segn fecha (date).

Para ver si nuestra base de datos soporta particionado simplemente ejecutamos:

SHOW VARIABLES LIKE '%partition%';

Crear particiones

CREATE TABLE e (
id INT NOT NULL,
fname VARCHAR(30),
lname VARCHAR(30)
)
PARTITION BY RANGE (id) (
PARTITION p0 VALUES LESS THAN (50),
PARTITION p1 VALUES LESS THAN (100),
PARTITION p2 VALUES LESS THAN (150),
PARTITION p3 VALUES LESS THAN (MAXVALUE)
);

INSERTAR DATOS:
INSERT INTO e VALUES
(1669, "Jim", "Smith"),
(337, "Mary", "Jones"),
(16, "Frank", "White"),

(2005, "Linda", "Black");

VISUALIZAR LAS PARTICIONES

mysql> SELECT PARTITION_NAME, TABLE_ROWS


->

FROM INFORMATION_SCHEMA.PARTITIONS

->

WHERE TABLE_NAME = 'e';

+----------------+------------+
| PARTITION_NAME | TABLE_ROWS |
+----------------+------------+
| p0

1 |

| p1

0 |

| p2

0 |

| p3

3 |

+----------------+------------+
4 rows in set (0.00 sec)

mysql> INSERT INTO e VALUES (51, "Ellen", "McDonald");

ALTER TABLE reports DROP PARTITION p0;

Realizar las ejemplos siguientes:

Crear particiones
1.- Creamos la tabla reports:

CREATE TABLE reports (


id int(10) NOT NULL AUTO_INCREMENT,
date datetime NOT NULL,

report TEXT,
PRIMARY KEY (id,date)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

Como se puede ver hemos aadido como ndice de tabla el campo date, esto es necesario si luego queremos
particionar por fecha.

2.- Ahora que tenemos la tabla creada vamos a particionar por mes:

ALTER TABLE reports PARTITION BY RANGE(TO_DAYS(date))(


PARTITION p201111 VALUES LESS THAN (TO_DAYS("2011-12-01")),
PARTITION p201112 VALUES LESS THAN (TO_DAYS("2012-01-01")),
PARTITION p201201 VALUES LESS THAN (TO_DAYS("2012-02-01")),
PARTITION p201202 VALUES LESS THAN (TO_DAYS("2012-03-01")),
PARTITION p201203 VALUES LESS THAN (TO_DAYS("2012-04-01")),
PARTITION p201204 VALUES LESS THAN (TO_DAYS("2012-05-01")),
PARTITION p201205 VALUES LESS THAN (TO_DAYS("2012-06-01")),
PARTITION pDefault VALUES LESS THAN MAXVALUE
);

La ltima particin (pDefault) tendr todos los registros que no entren en las particiones anteriores. De esta
manera nos aseguramos que la informacin nunca dejar de insertarse en la tabla.

Borrar particiones
Lo bueno de trabajar con particiones es que podemos borrar rpidamente registros sin tener que recorrer toda
la tabla e inmediatamente recuperar el espacio en disco utilizado por la tabla.
Por ejemplo si queremos borrar la particin ms antigua simplemente ejecutamos:

ALTER TABLE reports DROP PARTITION p201111;

Aadir particiones
En el ejemplo anterior las 2 ltimas particiones creadas han sido:

PARTITION p201205 VALUES LESS THAN (TO_DAYS("2012-06-01")),


PARTITION pDefault VALUES LESS THAN MAXVALUE

El problema es que todos los INSERTs que se hagan despus de mayo de 2012 se insertarn enpDefault. La
solucin sera aadir particiones nuevas para cubrir los prximos meses:

ALTER TABLE reports REORGANIZE PARTITION pDefault INTO (


PARTITION p201206 VALUES LESS THAN (TO_DAYS("2012-07-01")),
PARTITION pDefault VALUES LESS THAN MAXVALUE);

En el caso que no tuvieramos una particin del tipo pDefault simplemente ejecutamos:

ALTER TABLE reports ADD PARTITION (PARTITION p201206 VALUES LESS THAN
(TO_DAYS("2012-07-01")));

Consultar particiones
Para consultar informacin de particiones creadas en una tabla as como tambin los registros que contiene
cada una ejecutamos:

SELECT PARTITION_NAME,TABLE_ROWS FROM information_schema.PARTITIONS WHERE


TABLE_NAME='reports';

http://dev.mysql.com/doc/refman/5.6/en/partitioning-managementexchange.html

http://translate.google.com.mx/translate?hl=es419&sl=en&u=https://dev.mysql.com/doc/refman/5.6/en/alter-table-partitionoperations.html&prev=search
http://dev.mysql.com/doc/refman/5.6/en/partitioning-management-rangelist.html
http://dev.mysql.com/doc/refman/5.6/en/alter-table-partition-operations.html

http://translate.googleusercontent.com/translate_c?
depth=1&hl=es&prev=search&rurl=translate.google.com.mx&sl=en&u=http://
stackoverflow.com/questions/1579930/what-is-mysqlpartitioning&usg=ALkJrhjv1u-A3bhiBfsj4oLyKILWqKjEBg
http://www.mysql.com/downloads/

Anda mungkin juga menyukai