Rechercher dans le manuel MySQL
23.4 Partition Pruning
The optimization known as partition
pruning is based on a relatively simple concept which
can be described as “Do not scan partitions where there can
be no matching values”. Suppose a partitioned table
t1
is created by this statement:
- )
- );
Suppose that you wish to obtain results from a
SELECT
statement such as this one:
It is easy to see that none of the rows which ought to be returned
are in either of the partitions p0
or
p3
; that is, we need search only in partitions
p1
and p2
to find matching
rows. By limiting the search, it is possible to expend much less
time and effort in finding matching rows than by scanning all
partitions in the table. This “cutting away” of
unneeded partitions is known as
pruning. When the optimizer
can make use of partition pruning in performing this query,
execution of the query can be an order of magnitude faster than
the same query against a nonpartitioned table containing the same
column definitions and data.
The optimizer can perform pruning whenever a
WHERE
condition can be reduced to either one of
the following two cases:
partition_column
=constant
partition_column
IN (constant1
,constant2
, ...,constantN
)
In the first case, the optimizer simply evaluates the partitioning
expression for the value given, determines which partition
contains that value, and scans only this partition. In many cases,
the equal sign can be replaced with another arithmetic comparison,
including <
, >
,
<=
, >=
, and
<>
. Some queries using
BETWEEN
in the WHERE
clause
can also take advantage of partition pruning. See the examples
later in this section.
In the second case, the optimizer evaluates the partitioning expression for each value in the list, creates a list of matching partitions, and then scans only the partitions in this partition list.
SELECT
,
DELETE
, and
UPDATE
statements support partition
pruning. An INSERT
statement also
accesses only one partition per inserted row; this is true even
for a table that is partitioned by HASH
or
KEY
although this is not currently shown in the
output of EXPLAIN
.
Pruning can also be applied to short ranges, which the optimizer
can convert into equivalent lists of values. For instance, in the
previous example, the WHERE
clause can be
converted to WHERE region_code IN (126, 127, 128,
129)
. Then the optimizer can determine that the first
two values in the list are found in partition
p1
, the remaining two values in partition
p2
, and that the other partitions contain no
relevant values and so do not need to be searched for matching
rows.
The optimizer can also perform pruning for
WHERE
conditions that involve comparisons of
the preceding types on multiple columns for tables that use
RANGE COLUMNS
or LIST
COLUMNS
partitioning.
This type of optimization can be applied whenever the partitioning
expression consists of an equality or a range which can be reduced
to a set of equalities, or when the partitioning expression
represents an increasing or decreasing relationship. Pruning can
also be applied for tables partitioned on a
DATE
or
DATETIME
column when the
partitioning expression uses the
YEAR()
or
TO_DAYS()
function. Pruning can
also be applied for such tables when the partitioning expression
uses the TO_SECONDS()
function.
Suppose that table t2
, partitioned on a
DATE
column, is created using the
statement shown here:
- )
- );
The following statements using t2
can make of
use partition pruning:
In the case of the last statement, the optimizer can also act as follows:
Find the partition containing the low end of the range.
YEAR('1984-06-21')
yields the value1984
, which is found in partitiond3
.Find the partition containing the high end of the range.
YEAR('1999-06-21')
evaluates to1999
, which is found in partitiond5
.Scan only these two partitions and any partitions that may lie between them.
In this case, this means that only partitions
d3
,d4
, andd5
are scanned. The remaining partitions may be safely ignored (and are ignored).
Invalid DATE
and DATETIME
values referenced in the WHERE
condition of a
statement against a partitioned table are treated as
NULL
. This means that a query such as
SELECT * FROM
does not return any values (see Bug
#40972).
partitioned_table
WHERE
date_column
<
'2008-12-00'
So far, we have looked only at examples using
RANGE
partitioning, but pruning can be applied
with other partitioning types as well.
Consider a table that is partitioned by LIST
,
where the partitioning expression is increasing or decreasing,
such as the table t3
shown here. (In this
example, we assume for the sake of brevity that the
region_code
column is limited to values between
1 and 10 inclusive.)
- )
- );
For a statement such as SELECT * FROM t3 WHERE
region_code BETWEEN 1 AND 3
, the optimizer determines in
which partitions the values 1, 2, and 3 are found
(r0
and r1
) and skips the
remaining ones (r2
and r3
).
For tables that are partitioned by HASH
or
[LINEAR] KEY
, partition pruning is also
possible in cases in which the WHERE
clause
uses a simple =
relation against a column used
in the partitioning expression. Consider a table created like
this:
- )
- PARTITIONS 8;
A statement that compares a column value with a constant can be pruned:
Pruning can also be employed for short ranges, because the
optimizer can turn such conditions into IN
relations. For example, using the same table t4
as defined previously, queries such as these can be pruned:
In both these cases, the WHERE
clause is
transformed by the optimizer into WHERE region_code IN
(3, 4, 5)
.
This optimization is used only if the range size is smaller than the number of partitions. Consider this statement:
The range in the WHERE
clause covers 9 values
(4, 5, 6, 7, 8, 9, 10, 11, 12), but t4
has
only 8 partitions. This means that the DELETE
cannot be pruned.
When a table is partitioned by HASH
or
[LINEAR] KEY
, pruning can be used only on
integer columns. For example, this statement cannot use pruning
because dob
is a
DATE
column:
However, if the table stores year values in an
INT
column, then a query having
WHERE year_col >= 2001 AND year_col <=
2005
can be pruned.
Tables using a storage engine that provides automatic
partitioning, such as the NDB
storage engine
used by MySQL Cluster can be pruned if they are explicitly
partitioned.
Nederlandse vertaling
U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.
Bij voorbaat dank.
Document heeft de 26/06/2006 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/mysql-rf-partitioning-pruning.html
De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.
Referenties
Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.