I have a database with a date column having datatype as date. I am using mysql 5.6
The table is expected to have 10k rows each day. I want to create partition on the table on year range and then sub partitions on months of that particular year.
I have tried is,
ALTER TABLE nav_master
PARTITION BY RANGE( YEAR(nav_date) )
SUBPARTITION BY HASH( MONTH(nav_date) )
SUBPARTITIONS 12 (
PARTITION january VALUES LESS THAN (2),
PARTITION february VALUES LESS THAN (3),
PARTITION march VALUES LESS THAN (4),
PARTITION april VALUES LESS THAN (5),
PARTITION may VALUES LESS THAN (6),
PARTITION june VALUES LESS THAN (7),
PARTITION july VALUES LESS THAN (8),
PARTITION august VALUES LESS THAN (9),
PARTITION september VALUES LESS THAN (10),
PARTITION october VALUES LESS THAN (11),
PARTITION november VALUES LESS THAN (12),
PARTITION december VALUES LESS THAN (13)
);
I am able to execute the above query on empty table. But when I insert or when I execute the above query in same table with some data I get the below error.
#1526 - Table has no partition for value 2014
Not able to understand what the exact error is.
Also let me know if sub partitions help in better optimizing the tables or I should just go with partitions?
Update: I was indeed able to create the partition. The issue now is that there is a NULL partition created. When I had ensured there is no null dates and also all the date conditions were handled while making partitions.
Question is how can I see what data has gone to NULL partition? So that I can get an idea on what the problem is.
I am getting a null partition name using this query.
SELECT PARTITION_NAME, TABLE_ROWS, PARTITION_EXPRESSION, PARTITION_DESCRIPTION FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'nav_master' limit 0,180
Using this query to get all rows in partition, question is what to pass as partition name for null?
SELECT count(*) FROM nav_master PARTITION(??);
Best Answer
I had to do the same thing and solved slightly differently. Basically as far as I understand by reading the docs MySQL subpartitioning does not support partition types besides HASH and KEY.
This means that we can't determine in what subpartition a record will end up. It's up to MySQL. So I don't think it's wise giving such names to your subpartitions (i.e. january) because no way you're going to know if stuff created on that month will end up there. This because you can't subpartition by
MONTH(nav_date)
but only byHASH(MONTH(nav_date))
orKEY(MONTH(nav_date))
.So to solve the problem I decided to create a new
month
column in my table and then I added an index to it. Then I subpartioned byKEY(month)
without caring about the subpartition names. This way in MySQL 5.6 I could select the main partition from the FROM clause and the subpartition by specifying the month in the WHERE clause.Follows the full example: