Partition pruning and selection
When a WHERE clause is related to the partitioning expression, the optimizer knows which partitions are relevant for the query. Other partitions will not be read. This optimization is called partition pruning).
EXPLAIN PARTITIONS can be used to know which partitions will be read for a given query. A column called partitions
will contain a comma separated list of the accessed partitions. For example:
EXPLAIN PARTITIONS SELECT * FROM orders WHERE id < 15000000; +------+-------------+--------+------------+-------+---------------+---------+---------+------+------+-------------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | Extra | +------+-------------+--------+------------+-------+---------------+---------+---------+------+------+-------------+ | 1 | SIMPLE | orders | p0,p1 | range | PRIMARY | PRIMARY | 4 | NULL | 2 | Using where | +------+-------------+--------+------------+-------+---------------+---------+---------+------+------+-------------+
Sometimes the WHERE clause does not contain the necessary information to use partition pruning, or the optimizer cannot infer this information. However, we may know which partitions are relevant for the query. We can force MariaDB to only access the specified partitions by adding a PARTITION clause:
SELECT * FROM orders PARTITION (p3) WHERE user_id = 50;
The PARTITION clause is supported for all DML statements: