|
3 | 3 |
|
4 | 4 | This tool partitions and manages MariaDB tables by sequential IDs.
|
5 | 5 |
|
6 |
| -Note that reorganizing partitions is not a fast operation on ext4 filesystems; it is fast on xfs and zfs, but only when the partition being edited contains no rows. Adding partitions in the first place with InnoDB requires a full table copy. |
| 6 | +This is primarily a mechanism for dropping large numbers of rows of data without using `DELETE` statements. |
| 7 | + |
| 8 | +Adding partitions in the first place with InnoDB requires a full table copy. Otherwise, the `REORGANIZE PARTITION` command is fast only if operating on a partition that is empty, e.g., has no rows. |
7 | 9 |
|
8 | 10 | Similar tools:
|
9 | 11 | * https://github.com/davidburger/gomypartition, intended for tables with date-based partitions
|
@@ -48,9 +50,26 @@ partitionmanager:
|
48 | 50 | days: 14
|
49 | 51 | ```
|
50 | 52 |
|
| 53 | +For tables which are either partitioned but not yet using this tool's schema, or which have no empty partitions, the `bootstrap` command can be useful for proposing alterations to run manually. Note that `bootstrap` proposes commands that are likely to require partial copies of each table, so likely they will require a maintenance period. |
| 54 | + |
| 55 | +```sh |
| 56 | +partition-manager --mariadb ~/bin/rootsql-dev-primary bootstrap --out /tmp/bootstrap.yml --table orders |
| 57 | +INFO:write_state_info:Writing current state information |
| 58 | +INFO:write_state_info:(Table("orders"): {'id': 9236}), |
| 59 | +
|
| 60 | +# wait some time |
| 61 | +partition-manager --mariadb ~/bin/rootsql-dev-primary bootstrap --in /tmp/bootstrap.yml --table orders |
| 62 | +INFO:calculate_sql_alters:Reading prior state information |
| 63 | +INFO:calculate_sql_alters:Table orders, 24.0 hours, [9236] - [29236], [20000] pos_change, [832.706363653845]/hour |
| 64 | +orders: |
| 65 | + - ALTER TABLE `orders` REORGANIZE PARTITION `p_20210405` INTO (PARTITION `p_20210416` VALUES LESS THAN (30901), PARTITION `p_20210516` VALUES LESS THAN (630449), PARTITION `p_20210615` VALUES LESS THAN MAXVALUE); |
| 66 | + |
| 67 | +``` |
51 | 68 |
|
52 | 69 | # Algorithm
|
53 | 70 |
|
| 71 | +The core algorithm is implemented in a method `plan_partition_changes` in `table_append_partition.py`. That algorithm is: |
| 72 | + |
54 | 73 | For a given table and that table's intended partition period, desired end-state is to have:
|
55 | 74 | - All the existing partitions containing data,
|
56 | 75 | - A configurable number of trailing partitions which contain no data, and
|
@@ -79,16 +98,16 @@ Procedure:
|
79 | 98 | - Create a new list of intended empty partitions.
|
80 | 99 | - For each empty partition:
|
81 | 100 | - Predict the start-of-fill date using the partition's position relative to the current active partition, the current active partition's date, the partition period, and the future partition fill rate.
|
82 |
| - - Predict the end-of-fill value using the start-of-fill date and the future partition fill rate. |
83 | 101 | - If the start-of-fill date is different than the partition's name, rename the partition.
|
84 |
| - - If the end-of-fill value is different than the partition's current value, change that value. |
85 | 102 | - Append the changed partition to the intended empty partition list.
|
86 | 103 | - While the number of empty partitions is less than the intended number of trailing partitions to keep:
|
87 | 104 | - Predict the start-of-fill date for a new partition using the previous partition's date and the partition period.
|
88 |
| - - Predict the end-of-fill value using the start-of-fill date and the future partition fill rate. |
89 | 105 | - Append the new partition to the intended empty partition list.
|
90 | 106 | - Return the lists of non-empty partitions, the current empty partitions, and the post-algorithm intended empty partitions.
|
91 | 107 |
|
92 | 108 | # TODOs
|
93 | 109 |
|
94 |
| -Lots. A drop mechanism, for one. Yet more tests, particularly live integration tests with a test DB, for another. |
| 110 | +Lots: |
| 111 | +[X] Support for tables with partitions across multiple columns. |
| 112 | +[ ] A drop mechanism, for one. Initially it should take a retention period and log proposed `DROP` statements, not perform them. |
| 113 | +[ ] Yet more tests, particularly live integration tests with a test DB. |
0 commit comments