mirror of
https://github.com/postgres/postgres.git
synced 2025-06-25 01:02:05 +03:00
Update IN/EXISTS item.
This commit is contained in:
15
doc/FAQ
15
doc/FAQ
@ -1,7 +1,7 @@
|
|||||||
|
|
||||||
Frequently Asked Questions (FAQ) for PostgreSQL
|
Frequently Asked Questions (FAQ) for PostgreSQL
|
||||||
|
|
||||||
Last updated: Mon Sep 30 23:28:35 EDT 2002
|
Last updated: Wed Oct 9 23:14:53 EDT 2002
|
||||||
|
|
||||||
Current maintainer: Bruce Momjian (pgman@candle.pha.pa.us)
|
Current maintainer: Bruce Momjian (pgman@candle.pha.pa.us)
|
||||||
|
|
||||||
@ -998,18 +998,21 @@ CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
|
|||||||
4.22) Why are my subqueries using IN so slow?
|
4.22) Why are my subqueries using IN so slow?
|
||||||
|
|
||||||
Currently, we join subqueries to outer queries by sequentially
|
Currently, we join subqueries to outer queries by sequentially
|
||||||
scanning the result of the subquery for each row of the outer query. A
|
scanning the result of the subquery for each row of the outer query.
|
||||||
workaround is to replace IN with EXISTS:
|
If the subquery returns only a few rows and the outer query returns
|
||||||
|
many rows, IN is fastest. To speed up other queries, replace IN with
|
||||||
|
EXISTS:
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM tab
|
FROM tab
|
||||||
WHERE col1 IN (SELECT col2 FROM TAB2)
|
WHERE col IN (SELECT subcol FROM subtab)
|
||||||
|
|
||||||
to:
|
to:
|
||||||
SELECT *
|
SELECT *
|
||||||
FROM tab
|
FROM tab
|
||||||
WHERE EXISTS (SELECT col2 FROM TAB2 WHERE col1 = col2)
|
WHERE EXISTS (SELECT subcol FROM subtab WHERE subcol = col)
|
||||||
|
|
||||||
We hope to fix this limitation in a future release.
|
For this to be fast, subcol should be an indexed column. We hope to
|
||||||
|
fix this limitation in a future release.
|
||||||
|
|
||||||
4.23) How do I perform an outer join?
|
4.23) How do I perform an outer join?
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@
|
|||||||
alink="#0000ff">
|
alink="#0000ff">
|
||||||
<H1>Frequently Asked Questions (FAQ) for PostgreSQL</H1>
|
<H1>Frequently Asked Questions (FAQ) for PostgreSQL</H1>
|
||||||
|
|
||||||
<P>Last updated: Mon Sep 30 23:28:35 EDT 2002</P>
|
<P>Last updated: Wed Oct 9 23:14:53 EDT 2002</P>
|
||||||
|
|
||||||
<P>Current maintainer: Bruce Momjian (<A href=
|
<P>Current maintainer: Bruce Momjian (<A href=
|
||||||
"mailto:pgman@candle.pha.pa.us">pgman@candle.pha.pa.us</A>)<BR>
|
"mailto:pgman@candle.pha.pa.us">pgman@candle.pha.pa.us</A>)<BR>
|
||||||
@ -1282,22 +1282,25 @@ BYTEA bytea variable-length byte array (null-byte safe)
|
|||||||
|
|
||||||
<P>Currently, we join subqueries to outer queries by sequentially
|
<P>Currently, we join subqueries to outer queries by sequentially
|
||||||
scanning the result of the subquery for each row of the outer
|
scanning the result of the subquery for each row of the outer
|
||||||
query. A workaround is to replace <CODE>IN</CODE> with
|
query. If the subquery returns only a few rows and the outer query
|
||||||
|
returns many rows, <CODE><SMALL>IN</SMALL></CODE> is fastest. To
|
||||||
|
speed up other queries, replace <CODE>IN</CODE> with
|
||||||
<CODE>EXISTS</CODE>:</P>
|
<CODE>EXISTS</CODE>:</P>
|
||||||
<PRE>
|
<PRE>
|
||||||
<CODE>SELECT *
|
<CODE>SELECT *
|
||||||
FROM tab
|
FROM tab
|
||||||
WHERE col1 IN (SELECT col2 FROM TAB2)
|
WHERE col IN (SELECT subcol FROM subtab)
|
||||||
</CODE>
|
</CODE>
|
||||||
</PRE>
|
</PRE>
|
||||||
to:
|
to:
|
||||||
<PRE>
|
<PRE>
|
||||||
<CODE>SELECT *
|
<CODE>SELECT *
|
||||||
FROM tab
|
FROM tab
|
||||||
WHERE EXISTS (SELECT col2 FROM TAB2 WHERE col1 = col2)
|
WHERE EXISTS (SELECT subcol FROM subtab WHERE subcol = col)
|
||||||
</CODE>
|
</CODE>
|
||||||
</PRE>
|
</PRE>
|
||||||
|
|
||||||
|
For this to be fast, <CODE>subcol</CODE> should be an indexed column.
|
||||||
We hope to fix this limitation in a future release.
|
We hope to fix this limitation in a future release.
|
||||||
|
|
||||||
<H4><A name="4.23">4.23</A>) How do I perform an outer join?</H4>
|
<H4><A name="4.23">4.23</A>) How do I perform an outer join?</H4>
|
||||||
|
Reference in New Issue
Block a user