Oracle 5.0 Reference Manual page 700

Table of Contents

Advertisement

Because
DISTINCT
or
ORDER BY
Extensions to
In most cases, a
the following two queries are equivalent:
SELECT DISTINCT c1, c2, c3 FROM t1
WHERE c1 > const;
SELECT c1, c2, c3 FROM t1
WHERE c1 >
Due to this equivalence, the optimizations applicable to
queries with a
queries, see
When combining
unique rows.
If you do not use columns from all tables named in a query, MySQL stops scanning any unused tables
as soon as it finds the first match. In the following case, assuming that
can check with EXPLAIN), MySQL stops reading from
first row in t2:
SELECT DISTINCT t1.a FROM t1, t2 where t1.a=t2.a;
8.3.1.14. Optimizing Subqueries with
Certain optimizations are applicable to comparisons that use the
(or that use =ANY, which is equivalent). This section discusses these optimizations, particularly with
regard to the challenges that
optimizer are given at the end of the discussion.
Consider the following subquery comparison:
outer_expr
MySQL evaluates queries "from outside to inside." That is, it first obtains the value of the outer
expression outer_expr, and then runs the subquery and captures the rows that it produces.
A very useful optimization is to "inform" the subquery that the only rows of interest are those where the
inner expression
equality into the subquery's
EXISTS (SELECT 1 FROM ... WHERE
After the conversion, MySQL can use the pushed-down equality to limit the number of rows that it must
examine when evaluating the subquery.
More generally, a comparison of
same conversion. If
subquery comparison:
(oe_1, ..., oe_N) IN
(SELECT ie_1, ...,
Becomes:
EXISTS (SELECT 1 FROM ... WHERE
The following discussion assumes a single pair of outer and inner expression values for simplicity.
Optimizing
may use
GROUP
clauses that are not part of the selected columns. See
HAVING
GROUP
BY".
clause can be considered as a special case of
DISTINCT
const
GROUP BY c1, c2, c3;
clause. Thus, for more details on the optimization possibilities for
DISTINCT
Section 8.3.1.12,
"GROUP BY
LIMIT row_count
NULL
IN (SELECT
inner_expr
is equal to outer_expr. This is done by pushing down an appropriate
inner_expr
WHERE
N
and
oe_i
ie_i
ie_N
FROM ... WHERE subquery_where)
AND
oe_1
AND ...
AND
oe_N
Statements
SELECT
BY, you should be aware of how MySQL works with columns in
GROUP BY
Optimization".
with DISTINCT, MySQL stops as soon as it finds
t2
Strategy
EXISTS
values present. Suggestions on what you can do to help the
FROM ... WHERE subquery_where)
clause. That is, the comparison is converted to this:
subquery_where
AND outer_expr=inner_expr)
values to a subquery that returns N-value rows is subject to the
represent corresponding outer and inner expression values, this
subquery_where
=
ie_1
= ie_N)
680
Section 12.15.3, "MySQL
GROUP
queries can be also applied to
is used before
t1
(for any particular row in t1) when it finds the
operator to test subquery results
IN
BY. For example,
DISTINCT
row_count
(which you
t2

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents