2011-08-19 9 views
5

Tengo la siguiente estructura de tablas con datos en tiempo real en ella:convertir en una tabla con particiones

CREATE TABLE IF NOT EXISTS `userstatistics` (
    `user_id` int(10) unsigned NOT NULL, 
    `number_logons` int(7) unsigned NOT NULL DEFAULT '0', 
    `number_profileminiviews` int(7) unsigned NOT NULL DEFAULT '0', 
    `number_profilefullviews` int(7) unsigned NOT NULL DEFAULT '0', 
    `number_mailsreceived` int(7) unsigned NOT NULL DEFAULT '0', 
    `number_interestreceived` int(7) unsigned NOT NULL DEFAULT '0', 
    `number_favouratesreceived` int(7) unsigned NOT NULL DEFAULT '0', 
    `number_friendshiprequestreceived` int(7) unsigned NOT NULL DEFAULT '0', 
    `number_imchatrequestreceived` int(7) unsigned NOT NULL DEFAULT '0', 
    `yearweek` int(6) unsigned NOT NULL DEFAULT '0', 
    PRIMARY KEY (`user_id`,`yearweek`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

quiero convertir esto en una tabla con particiones con la siguiente estructura:

CREATE TABLE IF NOT EXISTS `userstatistics` (
    `user_id` int(10) unsigned NOT NULL, 
    `number_logons` int(7) unsigned NOT NULL DEFAULT '0', 
    `number_profileminiviews` int(7) unsigned NOT NULL DEFAULT '0', 
    `number_profilefullviews` int(7) unsigned NOT NULL DEFAULT '0', 
    `number_mailsreceived` int(7) unsigned NOT NULL DEFAULT '0', 
    `number_interestreceived` int(7) unsigned NOT NULL DEFAULT '0', 
    `number_favouratesreceived` int(7) unsigned NOT NULL DEFAULT '0', 
    `number_friendshiprequestreceived` int(7) unsigned NOT NULL DEFAULT '0', 
    `number_imchatrequestreceived` int(7) unsigned NOT NULL DEFAULT '0', 
    `yearweek` int(6) unsigned NOT NULL DEFAULT '0', 
    PRIMARY KEY (`user_id`,`yearweek`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1 
/*!50100 PARTITION BY RANGE (yearweek) 
(PARTITION userstats_201108 VALUES LESS THAN (201108) ENGINE = InnoDB, 
    PARTITION userstats_201109 VALUES LESS THAN (201109) ENGINE = InnoDB, 
    PARTITION userstats_201110 VALUES LESS THAN (201110) ENGINE = InnoDB, 
    PARTITION userstats_201111 VALUES LESS THAN (201111) ENGINE = InnoDB, 
    PARTITION userstats_201112 VALUES LESS THAN (201112) ENGINE = InnoDB, 
    PARTITION userstats_201113 VALUES LESS THAN (201113) ENGINE = InnoDB, 
    PARTITION userstats_201114 VALUES LESS THAN (201114) ENGINE = InnoDB, 
    PARTITION userstats_201115 VALUES LESS THAN (201115) ENGINE = InnoDB, 
    PARTITION userstats_201116 VALUES LESS THAN (201116) ENGINE = InnoDB, 
    PARTITION userstats_201117 VALUES LESS THAN (201117) ENGINE = InnoDB, 
    PARTITION userstats_201118 VALUES LESS THAN (201118) ENGINE = InnoDB, 
    PARTITION userstats_201119 VALUES LESS THAN (201119) ENGINE = InnoDB, 
    PARTITION userstats_201120 VALUES LESS THAN (201120) ENGINE = InnoDB, 
    PARTITION userstats_201121 VALUES LESS THAN (201121) ENGINE = InnoDB, 
    PARTITION userstats_max VALUES LESS THAN MAXVALUE ENGINE = InnoDB) */; 

¿Cómo puede Yo hago esta conversión?

Simplemente cambiando la primera línea de la segunda instrucción SQL para

ALTER TABLE 'userstatistics' (

¿Esto hacerlo?

Yendo de MySQL 5.0 a 5.1.

Respuesta

5

En primer lugar, debe ejecutar MySQL 5.1 o posterior. MySQL 5.0 no admite partición.

En segundo lugar, tenga en cuenta la diferencia entre las comillas simples (que delimitan las cadenas y las fechas) y las contraseñas (que delimitan los identificadores de tabla y columna en MySQL). Use el tipo correcto cuando sea apropiado. Menciono esto, porque su ejemplo se utiliza el tipo incorrecto de comillas:

ALTER TABLE 'userstatistics' (

Eso debería ser:

ALTER TABLE `userstatistics` (

Por último, sí, se puede reestructurar una tabla en particiones con ALTER TABLE. He aquí una copia exacta & pasta de una declaración que probado con MySQL 5.1.57:

ALTER TABLE userstatistics PARTITION BY RANGE (yearweek) (
PARTITION userstats_201108 VALUES LESS THAN (201108) ENGINE = InnoDB, 
PARTITION userstats_201109 VALUES LESS THAN (201109) ENGINE = InnoDB, 
PARTITION userstats_201110 VALUES LESS THAN (201110) ENGINE = InnoDB, 
PARTITION userstats_201111 VALUES LESS THAN (201111) ENGINE = InnoDB, 
PARTITION userstats_201112 VALUES LESS THAN (201112) ENGINE = InnoDB, 
PARTITION userstats_201113 VALUES LESS THAN (201113) ENGINE = InnoDB, 
PARTITION userstats_201114 VALUES LESS THAN (201114) ENGINE = InnoDB, 
PARTITION userstats_201115 VALUES LESS THAN (201115) ENGINE = InnoDB, 
PARTITION userstats_201116 VALUES LESS THAN (201116) ENGINE = InnoDB, 
PARTITION userstats_201117 VALUES LESS THAN (201117) ENGINE = InnoDB, 
PARTITION userstats_201118 VALUES LESS THAN (201118) ENGINE = InnoDB, 
PARTITION userstats_201119 VALUES LESS THAN (201119) ENGINE = InnoDB, 
PARTITION userstats_201120 VALUES LESS THAN (201120) ENGINE = InnoDB, 
PARTITION userstats_201121 VALUES LESS THAN (201121) ENGINE = InnoDB, 
PARTITION userstats_max VALUES LESS THAN MAXVALUE ENGINE = InnoDB); 

Tenga en cuenta que esto provoca una reestructuración mesa, así que si usted ya tiene una gran cantidad de datos en esta tabla, se tardará un tiempo para correr. El tiempo exacto depende de la cantidad de datos que tenga, la velocidad del hardware y otros factores. Tenga en cuenta que, aunque la tabla se está reestructurando, está bloqueada y no está disponible para lectura y escritura por otras consultas.

+0

funcionó perfectamente ... thx :) – Adam

+0

La documentación paso a paso de Bill es sin duda mejor que el menú de ayuda. :-) Y esta partición supone que harás más consultas de rango en un solo mes. –

+0

+1 para la información muy detallada –

0

mirar esto http://dev.mysql.com/doc/refman/5.1/en/alter-table.html acerca de la modificación de tabla.

Luego, en particular, la tabla alternativa .. ADD/DROP/COALESCE/REORGANIZE partition sql proporciona casi todas las funciones para administrar sus particiones.

tenga en cuenta que el hash solo puede usarse para enteros.

• ALTER TABLE ... ADD PARTITION no crea ninguna tabla temporal excepto cuando se usa con tablas NDB. Las operaciones ADD o DROP para las particiones RANGE o LIST son operaciones inmediatas o casi. Las operaciones ADD o COALESCE para particiones HASH o KEY copian datos entre particiones cambiadas; a menos que se usara LINEAR HASH o LINEAR KEY, esto es muy similar a crear una nueva tabla (aunque la operación se realiza partición por partición). Las operaciones REORGANIZAR solo copian las particiones modificadas y no tocan las que no han cambiado.

Cuestiones relacionadas