Oracle 5.0 Reference Manual page 1127

Table of Contents

Advertisement

(SELECT * FROM t2 LIMIT 1);
• Replace a join with a subquery. For example, try this:
SELECT DISTINCT column1 FROM t1 WHERE t1.column1 IN (
SELECT column1 FROM t2);
Instead of this:
SELECT DISTINCT t1.column1 FROM t1, t2
WHERE t1.column1 = t2.column1;
• Some subqueries can be transformed to joins for compatibility with older versions of MySQL that
do not support subqueries. However, in some cases, converting a subquery to a join may improve
performance. See
Section 13.2.9.11, "Rewriting Subqueries as
• Move clauses from outside to inside the subquery. For example, use this query:
SELECT * FROM t1
WHERE s1 IN (SELECT s1 FROM t1 UNION ALL SELECT s1 FROM t2);
Instead of this query:
SELECT * FROM t1
WHERE s1 IN (SELECT s1 FROM t1) OR s1 IN (SELECT s1 FROM t2);
For another example, use this query:
SELECT (SELECT column1 + 5 FROM t1) FROM t2;
Instead of this query:
SELECT (SELECT column1 FROM t1) + 5 FROM t2;
• Use a row subquery instead of a correlated subquery. For example, use this query:
SELECT * FROM t1
WHERE (column1,column2) IN (SELECT column1,column2 FROM t2);
Instead of this query:
SELECT * FROM t1
WHERE EXISTS (SELECT * FROM t2 WHERE t2.column1=t1.column1
AND t2.column2=t1.column2);
• Use
NOT (a = ANY (...))
• Use
x = ANY (table containing (1,2))
• Use
rather than EXISTS.
= ANY
• For uncorrelated subqueries that always return one row,
use this query:
SELECT * FROM t1
WHERE
t1.col_name
Instead of this query:
SELECT * FROM t1
WHERE
t1.col_name
These tricks might cause programs to go faster or slower. Using MySQL facilities like the
[957]
function, you can get an idea about what helps in your own situation. See
BENCHMARK()
Section 12.13, "Information
Some optimizations that MySQL itself makes are:
Subquery Syntax
rather than
a <> ALL
= (SELECT a FROM t2 WHERE b = some_const);
IN (SELECT a FROM t2 WHERE b = some_const);
Functions".
1107
Joins".
(...).
rather than
x=1 OR
x=2.
is always slower than =. For example,
IN

Advertisement

Table of Contents
loading

This manual is also suitable for:

Mysql 5.0

Table of Contents