Restrictions on Subqueries
2984
Cursors are nonholdable (not held open after a commit).
Cursors are asensitive.
Cursors are nonscrollable.
Cursors are not named. The statement handler acts as the cursor ID.
You can have open only a single cursor per prepared statement. If you need several cursors, you must
prepare several statements.
You cannot use a cursor for a statement that generates a result set if the statement is not supported
in prepared mode. This includes statements such as
CHECK TABLE
,
HANDLER READ
, and
SHOW
BINLOG EVENTS
.
E.3. Restrictions on Subqueries
• In MySQL 5.0 before 5.0.36, if you compare a
NULL
value to a subquery using
ALL
,
ANY
, or
SOME
,
and the subquery returns an empty result, the comparison might evaluate to the nonstandard result
of
NULL
rather than to
TRUE
or
FALSE
.
• Subquery optimization for
IN
is not as effective as for the
=
operator or for the
IN(value_list)
[878]
operator.
A typical case for poor
IN
subquery performance is when the subquery returns a small number of
rows but the outer query returns a large number of rows to be compared to the subquery result.
The problem is that, for a statement that uses an
IN
subquery, the optimizer rewrites it as a
correlated subquery. Consider the following statement that uses an uncorrelated subquery:
SELECT ... FROM t1 WHERE t1.a IN (SELECT b FROM t2);
The optimizer rewrites the statement to a correlated subquery:
SELECT ... FROM t1 WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.b = t1.a);
If the inner and outer queries return
M
and
N
rows, respectively, the execution time becomes on the
order of O(
M
×
N
), rather than O(
M
+
N
) as it would be for an uncorrelated subquery.
An implication is that an
IN
subquery can be much slower than a query written using an
IN(value_list)
[878]
operator that lists the same values that the subquery would return.
• In general, you cannot modify a table and select from the same table in a subquery. For example,
this limitation applies to statements of the following forms:
DELETE FROM t WHERE ... (SELECT ... FROM t ...);
UPDATE t ... WHERE col = (SELECT ... FROM t ...);
{INSERT|REPLACE} INTO t (SELECT ... FROM t ...);
Exception: The preceding prohibition does not apply if you are using a subquery for the modified
table in the
FROM
clause. Example:
UPDATE t ... WHERE col = (SELECT * FROM (SELECT ... FROM t...) AS _t ...);
Here the result from the subquery in the
FROM
clause is stored as a temporary table, so the relevant
rows in
t
have already been selected by the time the update to
t
takes place.
• Row comparison operations are only partially supported:
• For
expr [NOT] IN subquery
,
expr
can be an
n
-tuple (specified using row constructor
syntax) and the subquery can return rows of
n
-tuples. The permitted syntax is therefore more
specifically expressed as
row_constructor [NOT] IN table_subquery
Summary of Contents for 5.0
Page 1: ...MySQL 5 0 Reference Manual ...
Page 18: ...xviii ...
Page 60: ...40 ...
Page 396: ...376 ...
Page 578: ...558 ...
Page 636: ...616 ...
Page 844: ...824 ...
Page 1234: ...1214 ...
Page 1427: ...MySQL Proxy Scripting 1407 ...
Page 1734: ...1714 ...
Page 1752: ...1732 ...
Page 1783: ...Configuring Connector ODBC 1763 ...
Page 1793: ...Connector ODBC Examples 1773 ...
Page 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Page 2850: ...2830 ...
Page 2854: ...2834 ...
Page 2928: ...2908 ...
Page 3000: ...2980 ...
Page 3122: ...3102 ...
Page 3126: ...3106 ...
Page 3174: ...3154 ...
Page 3232: ...3212 ...