Subquery Syntax
1107
(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 Joins”
.
• 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 c 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 (...))
rather than
a <> ALL (...)
.
• Use
x = ANY (table containing (1,2))
rather than
x=1 OR x=2
.
• Use
= ANY
rather than
EXISTS
.
• For uncorrelated subqueries that always return one row,
IN
is always slower than
=
. For example,
use this query:
SELECT * FROM t1
WHERE t1.
col_name
= (SELECT a FROM t2 WHERE b =
some_const
);
Instead of this query:
SELECT * FROM t1
WHERE t1.
col_name
IN (SELECT a FROM t2 WHERE b =
some_const
);
These tricks might cause programs to go faster or slower. Using MySQL facilities like the
BENCHMARK()
[957]
function, you can get an idea about what helps in your own situation. See
Section 12.13, “Information Functions”
.
Some optimizations that MySQL itself makes are:
Содержание 5.0
Страница 1: ...MySQL 5 0 Reference Manual ...
Страница 18: ...xviii ...
Страница 60: ...40 ...
Страница 396: ...376 ...
Страница 578: ...558 ...
Страница 636: ...616 ...
Страница 844: ...824 ...
Страница 1234: ...1214 ...
Страница 1426: ...MySQL Proxy Scripting 1406 The following diagram shows an overview of the classes exposed by MySQL Proxy ...
Страница 1427: ...MySQL Proxy Scripting 1407 ...
Страница 1734: ...1714 ...
Страница 1752: ...1732 ...
Страница 1783: ...Configuring Connector ODBC 1763 ...
Страница 1793: ...Connector ODBC Examples 1773 ...
Страница 1839: ...Connector Net Installation 1819 2 You must choose the type of installation to perform ...
Страница 1842: ...Connector Net Installation 1822 5 Once the installation has been completed click Finish to exit the installer ...
Страница 1864: ...Connector Net Visual Studio Integration 1844 Figure 20 24 Debug Stepping Figure 20 25 Function Stepping 1 of 2 ...
Страница 2850: ...2830 ...
Страница 2854: ...2834 ...
Страница 2928: ...2908 ...
Страница 3000: ...2980 ...
Страница 3122: ...3102 ...
Страница 3126: ...3106 ...
Страница 3174: ...3154 ...
Страница 3232: ...3212 ...