File tree Expand file tree Collapse file tree 2 files changed +16
-10
lines changed Expand file tree Collapse file tree 2 files changed +16
-10
lines changed Original file line number Diff line number Diff line change 1
1
2
2
Frequently Asked Questions (FAQ) for PostgreSQL
3
3
4
- Last updated: Mon Sep 30 23:28:35 EDT 2002
4
+ Last updated: Wed Oct 9 23:14:53 EDT 2002
5
5
6
6
Current maintainer: Bruce Momjian (pgman@candle.pha.pa.us)
7
7
@@ -998,18 +998,21 @@ CREATE TABLE test (x int, modtime timestamp DEFAULT CURRENT_TIMESTAMP );
998
998
4.22) Why are my subqueries using IN so slow?
999
999
1000
1000
Currently, we join subqueries to outer queries by sequentially
1001
- scanning the result of the subquery for each row of the outer query. A
1002
- workaround is to replace IN with EXISTS:
1001
+ scanning the result of the subquery for each row of the outer query.
1002
+ If the subquery returns only a few rows and the outer query returns
1003
+ many rows, IN is fastest. To speed up other queries, replace IN with
1004
+ EXISTS:
1003
1005
SELECT *
1004
1006
FROM tab
1005
- WHERE col1 IN (SELECT col2 FROM TAB2 )
1007
+ WHERE col IN (SELECT subcol FROM subtab )
1006
1008
1007
1009
to:
1008
1010
SELECT *
1009
1011
FROM tab
1010
- WHERE EXISTS (SELECT col2 FROM TAB2 WHERE col1 = col2 )
1012
+ WHERE EXISTS (SELECT subcol FROM subtab WHERE subcol = col )
1011
1013
1012
- We hope to fix this limitation in a future release.
1014
+ For this to be fast, subcol should be an indexed column. We hope to
1015
+ fix this limitation in a future release.
1013
1016
1014
1017
4.23) How do I perform an outer join?
1015
1018
Original file line number Diff line number Diff line change 14
14
alink ="#0000ff ">
15
15
< H1 > Frequently Asked Questions (FAQ) for PostgreSQL</ H1 >
16
16
17
- < P > Last updated: Mon Sep 30 23:28:35 EDT 2002</ P >
17
+ < P > Last updated: Wed Oct 9 23:14:53 EDT 2002</ P >
18
18
19
19
< P > Current maintainer: Bruce Momjian (< A href =
20
20
"mailto:pgman@candle.pha.pa.us "> pgman@candle.pha.pa.us</ A > )< BR >
@@ -1282,22 +1282,25 @@ <H4><A name="4.22">4.22</A>) Why are my subqueries using
1282
1282
1283
1283
< P > Currently, we join subqueries to outer queries by sequentially
1284
1284
scanning the result of the subquery for each row of the outer
1285
- query. A workaround is to replace < CODE > IN</ CODE > with
1285
+ query. If the subquery returns only a few rows and the outer query
1286
+ returns many rows, < CODE > < SMALL > IN</ SMALL > </ CODE > is fastest. To
1287
+ speed up other queries, replace < CODE > IN</ CODE > with
1286
1288
< CODE > EXISTS</ CODE > :</ P >
1287
1289
< PRE >
1288
1290
< CODE > SELECT *
1289
1291
FROM tab
1290
- WHERE col1 IN (SELECT col2 FROM TAB2 )
1292
+ WHERE col IN (SELECT subcol FROM subtab )
1291
1293
</ CODE >
1292
1294
</ PRE >
1293
1295
to:
1294
1296
< PRE >
1295
1297
< CODE > SELECT *
1296
1298
FROM tab
1297
- WHERE EXISTS (SELECT col2 FROM TAB2 WHERE col1 = col2 )
1299
+ WHERE EXISTS (SELECT subcol FROM subtab WHERE subcol = col )
1298
1300
</ CODE >
1299
1301
</ PRE >
1300
1302
1303
+ For this to be fast, < CODE > subcol</ CODE > should be an indexed column.
1301
1304
We hope to fix this limitation in a future release.
1302
1305
1303
1306
< H4 > < A name ="4.23 "> 4.23</ A > ) How do I perform an outer join?</ H4 >
You can’t perform that action at this time.
0 commit comments