— time-wasted, mysql — 2 min read
Partitioning is a way of splitting up big tables into small pieces, so it can be managed much efficiently.
My only big problem was, deletes were are too slow. That actually got me to other problems thinking about alternatives.
When initially designing this table, i assumed, partitioning is not required as i didn't have any usecase for it.
Both the new requirements were very slow with existing design. Both the activities took more than 4hrs and rollbacks took equal amount of time.
How partition could have helped,
Mass loading. Couple of options,
Archiving large amount of data
Partitioning!
Following Rick's RoTs - Rules of Thumb for MySQL here,
1ALTER TABLE bse_daily_part 2PARTITION BY RANGE (TO_DAYS(ts)) (3 PARTITION past VALUES LESS THAN (TO_DAYS('2021-01-01')),4 PARTITION jan21 VALUES LESS THAN (TO_DAYS('2021-02-01')),5 PARTITION future VALUES LESS THAN (MAXVALUE) );
Note : future partition is to catch the overflows
1ALTER TABLE bse_daily_part2 REORGANIZE PARTITION future INTO (3 PARTITION feb21 VALUES LESS THAN (TO_DAYS('2021-03-01')),4 PARTITION future VALUES LESS THAN (MAXVALUE)5 );6
7mysql> SELECT PARTITION_NAME, TABLE_ROWS, PARTITION_EXPRESSION, PARTITION_DESCRIPTION8 -> FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'bse_daily_part'9 -> ;10+----------------+------------+----------------------+-----------------------+11| PARTITION_NAME | TABLE_ROWS | PARTITION_EXPRESSION | PARTITION_DESCRIPTION |12+----------------+------------+----------------------+-----------------------+13| feb21 | 0 | to_days(`ts`) | 738215 |14| future | 0 | to_days(`ts`) | MAXVALUE |15| jan21 | 69247 | to_days(`ts`) | 738187 |16| past | 0 | to_days(`ts`) | 738156 |17+----------------+------------+----------------------+-----------------------+184 rows in set (0.00 sec)
This reads and stores the key distributions for partitions.
1ALTER TABLE bse_daily_part ANALYZE PARTITION feb21;2+---------------------+---------+----------+----------+3| Table | Op | Msg_type | Msg_text |4+---------------------+---------+----------+----------+5| test.bse_daily_part | analyze | status | OK |6+---------------------+---------+----------+----------+71 row in set (0.25 sec)8
9mysql> SELECT PARTITION_NAME, TABLE_ROWS, PARTITION_EXPRESSION, PARTITION_DESCRIPTION10 -> FROM INFORMATION_SCHEMA.PARTITIONS WHERE TABLE_NAME = 'bse_daily_part'11 -> ;12+----------------+------------+----------------------+-----------------------+13| PARTITION_NAME | TABLE_ROWS | PARTITION_EXPRESSION | PARTITION_DESCRIPTION |14+----------------+------------+----------------------+-----------------------+15| feb21 | 68347 | to_days(`ts`) | 738215 |16| future | 0 | to_days(`ts`) | MAXVALUE |17| jan21 | 69247 | to_days(`ts`) | 738187 |18| past | 0 | to_days(`ts`) | 738156 |19+----------------+------------+----------------------+-----------------------+204 rows in set (0.01 sec)
1ALTER TABLE bse_daily_part TRUNCATE PARTITION future;
1ALTER TABLE bse_daily_part DROP PARTITION feb21;
If you have deleted a large number of rows from a partition or if you have made many changes to a partitioned table with variable-length rows (that is, having VARCHAR, BLOB, or TEXT columns), you can use ALTER TABLE ... OPTIMIZE PARTITION to reclaim any unused space and to defragment the partition data file.
1ALTER TABLE bse_daily_part OPTIMIZE PARTITION past, jan21;
OPTIMIZE PARTITION = Equivalent to running CHECK PARTITION + ANALYZE PARTITION + REPAIR PARTITION on those specific partitions
1select * from bse_daily_part partition(feb21) limit 10;
Below query accesses the specific partition jan21
1explain select * from bse_daily_part where ts = '2021-01-05' limit 5;2+----+-------------+----------------+------------+------+---------------+------+---------+-------+------+----------+-------+3| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |4+----+-------------+----------------+------------+------+---------------+------+---------+-------+------+----------+-------+5| 1 | SIMPLE | bse_daily_part | jan21 | ref | uix1 | uix1 | 5 | const | 7120 | 100.00 | NULL |6+----+-------------+----------------+------------+------+---------------+------+---------+-------+------+----------+-------+71 row in set, 1 warning (0.27 sec)