سؤال

I've a 30M rows table and I want to partition it by dates.

mysql > SHOW CREATE TABLE `parameters`

CREATE TABLE `parameters` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `add_time` datetime DEFAULT NULL,
  ...(etc)
) ENGINE=MyISAM AUTO_INCREMENT=28929477 DEFAULT CHARSET=utf8 

Table stores data for last 5 years and rows count increases dramatically. I want partition it by years(2009, 2010, 2011, 2012, 2013).

    ALTER TABLE parameters DROP PRIMARY KEY, ADD INDEX(id);

ALTER TABLE parameters PARTITION BY RANGE (TO_DAYS(id)) (
    PARTITION y2009 VALUES LESS THAN (TO_DAYS('2010-01-01')),
    PARTITION y2010 VALUES LESS THAN (TO_DAYS('2011-01-01')),
    PARTITION y2011 VALUES LESS THAN (TO_DAYS('2012-03-01')),
    PARTITION y2012 VALUES LESS THAN (TO_DAYS('2013-01-01')),
    PARTITION y2013 VALUES LESS THAN MAXVALUE
);

Everyting works on dev-server, but there is a problem on production-server. The problem: almost all of the rows moved to the first partition(y2009). But data is uniformly distributed by years. Physically there is large y2009.myd file in DATA folder and others partitions have much less size. Also I tried to reorganize first partition in order to exclude Null dates:

alter table raw
reorganize partition y2012 into (
    PARTITION y0 VALUES LESS THAN (0),
    PARTITION y2012 VALUES LESS THAN (TO_DAYS('2013-01-01')),
);

P.S.: production and dev servers have same version of MySQL 5.1.37

هل كانت مفيدة؟

المحلول

You need to use date column in RANGE not id for partition.
I have changed TO_DAYS(id) to TO_DAYS(add_time)

Try below:

ALTER TABLE parameters PARTITION BY RANGE (TO_DAYS(add_time)) (
    PARTITION y0 VALUES LESS THAN (TO_DAYS('2009-01-01')),
    PARTITION y2009 VALUES LESS THAN (TO_DAYS('2010-01-01')),
    PARTITION y2010 VALUES LESS THAN (TO_DAYS('2011-01-01')),
    PARTITION y2011 VALUES LESS THAN (TO_DAYS('2012-03-01')),
    PARTITION y2012 VALUES LESS THAN (TO_DAYS('2013-01-01')),
    PARTITION y2013 VALUES LESS THAN MAXVALUE

);

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top