Для добавления партицирования в существующую таблицу MySQL необходимо пересмотреть первичный ключ и все уникальные индексы. DROP TABLE IF EXISTS reports; CREATE TABLE reports ( rep_id INTEGER PRIMARY KEY AUTO_INCREMENT, rep_year INTEGER NOT NULL, rep_name VARCHAR(100) NOT NULL ) ENGINE = InnoDB; ALTER TABLE reports ADD UNIQUE INDEX idx_rep_name (rep_name); ALTER TABLE reports CHANGE rep_id rep_id INTEGER NOT NULL; ALTER TABLE reports DROP PRIMARY KEY; ALTER TABLE reports ADD CONSTRAINT pk_rep_id PRIMARY KEY (rep_id,rep_year); ALTER TABLE reports MODIFY rep_id INTEGER NOT NULL AUTO_INCREMENT; ALTER TABLE reports DROP INDEX idx_rep_name; ALTER TABLE reports ADD UNIQUE INDEX idx_rep_name (rep_name, rep_year); ALTER TABLE reports PARTITION BY RANGE(rep_year) ( PARTITION y2011 VALUES LESS THAN (2012), PARTITION y2012 VALUES LESS THAN (2013), PARTITION yXXXX VALUES LESS THAN MAXVALUE ); INSERT INTO reports (rep_year, rep_name) VALUES (2010, '2010a'), (2010, '2010b'), (2010, '2010c'), (2012, '2012a'), (2012, '2012b'), (2012, '2012c'); EXPLAIN PARTITIONS SELECT * FROM reports WHERE rep_year = 2010 AND rep_name='2010a'; EXPLAIN PARTITIONS SELECT * FROM reports WHERE rep_year = 2012;
SQL Справочник v0.05 © 2007-2025 Igor Salnikov aka SunDoctor