This feature was introduced in MariaDB 5.5.21.
SELECT ... FROM ... WHERE ... [group_clause] [order_clause] LIMIT [[offset,] row_count] ROWS EXAMINED rows_limit;
Similar to the parameters of LIMIT
, rows_limit
can be both a prepared statement parameter, or a stored program parameter.
The purpose of this optimization is to provide the means to terminate the execution of SELECT
statements which examine too many rows, and thus use too many resources. This is achieved through an extension of the LIMIT
clause — LIMIT ROWS EXAMINED number_of_rows
. Whenever possible the semantics of LIMIT ROWS EXAMINED
is the same as that of normal LIMIT
(for instance for aggregate functions).
The LIMIT ROWS EXAMINED
clause is taken into account by the query engine only during query execution. Thus the clause is ignored in the following cases:
EXPLAIN
-ed. The clause is not applicable to DELETE
or UPDATE
statements, and if used in those statements produces a syntax error.
The effects of this clause are as follows:
The LIMIT ROWS EXAMINED
clause cannot be specified on a per-subquery basis. There can be only one LIMIT ROWS EXAMINED
clause for the whole SELECT
statement. If a SELECT
statement contains several subqueries with LIMIT ROWS EXAMINED
, the one that is parsed last is taken into account.
A simple example of the clause is:
SELECT * from t1, t2 LIMIT 10 ROWS EXAMINED 10000;
The LIMIT ROWS EXAMINED
clause is global for the whole statement.
If a composite query (such as UNION
, or query with derived tables or with subqueries) contains more than one LIMIT ROWS EXAMINED
, the last one parsed is taken into account. In this manner either the last or the outermost one is taken into account. For instance, in the query:
SELECT * FROM t1 WHERE c1 IN (SELECT * FROM t2 WHERE c2 > ' ' LIMIT ROWS EXAMINED 0) LIMIT ROWS EXAMINED 11;
The limit that is taken into account is 11, not 0.
© 2019 MariaDB
Licensed under the Creative Commons Attribution 3.0 Unported License and the GNU Free Documentation License.
https://mariadb.com/kb/en/limit-rows-examined/