Optimizing
SELECT
Statements
683
and converts it to this expression:
EXISTS (SELECT 1 FROM ... WHERE
subquery_where
AND trigcond(
oe_1
=
ie_1
)
AND ...
AND trigcond(
oe_N
=
ie_N
)
)
Each
trigcond(X)
is a special function that evaluates to the following values:
•
X
when the “linked” outer expression
oe_i
is not
NULL
•
TRUE
when the “linked” outer expression
oe_i
is
NULL
Note that trigger functions are not triggers of the kind that you create with
CREATE TRIGGER
.
Equalities that are wrapped into
trigcond()
functions are not first class predicates for the query
optimizer. Most optimizations cannot deal with predicates that may be turned on and off at query
execution time, so they assume any
trigcond(X)
to be an unknown function and ignore it. At the
moment, triggered equalities can be used by those optimizations:
• Reference optimizations:
trigcond(X=Y [OR Y IS NULL])
can be used to construct
ref
[646]
,
eq_ref
[646]
, or
ref_or_null
[647]
table accesses.
• Index lookup-based subquery execution engines:
trigcond(X=Y)
can be used to construct
unique_subquery
[647]
or
index_subquery
[647]
accesses.
• Table-condition generator: If the subquery is a join of several tables, the triggered condition will be
checked as soon as possible.
When the optimizer uses a triggered condition to create some kind of index lookup-based access
(as for the first two items of the preceding list), it must have a fallback strategy for the case when the
condition is turned off. This fallback strategy is always the same: Do a full table scan. In
EXPLAIN
output, the fallback shows up as
Full scan on NULL key
in the
Extra
column:
mysql>
EXPLAIN SELECT t1.col1,
->
t1.col1 IN (SELECT t2.key1 FROM t2 WHERE t2.col2=t1.col2) FROM t1\G
*************************** 1. row ***************************
id: 1
select_type: PRIMARY
table: t1
...
*************************** 2. row ***************************
id: 2
select_type: DEPENDENT SUBQUERY
table: t2
type: index_subquery
possible_keys: key1
key: key1
key_len: 5
ref: func
rows: 2
Extra: Using where; Full scan on NULL key
If you run
EXPLAIN EXTENDED
followed by
SHOW WARNINGS
, you can see the triggered condition:
*************************** 1. row ***************************
Level: Note
Code: 1003
Message: select `test`.`t1`.`col1` AS `col1`,
<in_optimizer>(`test`.`t1`.`col1`,
<exists>(<index_lookup>(<cache>(`test`.`t1`.`col1`) in t2
on key1 checking NULL
where (`test`.`t2`.`col2` = `test`.`t1`.`col2`) having
trigcond(<is_not_null_test>(`test`.`t2`.`key1`))))) AS
`t1.col1 IN (select t2.key1 from t2 where t2.col2=t1.col2)`
from `test`.`t1`
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 ...