【发布时间】:2011-05-04 22:55:58
【问题描述】:
我有以下疑问:
SELECT t1.c1 FROM t1,t2 WHERE t1.c2 = 'X' AND t1.id = t2.id AND t2.c3 = 'Y';
Postgres 为这个查询生成两个计划,类似于:
Nested Loop (rows=1 width=7) (actual rows=1 loops=1)
-> Index Scan using idx1 on t1 (rows=1 width=7) (actual rows=4 loops=1)
-> Index Scan using idx2 on t2 (rows=1 width=7) (actual rows=0 loops=7)
或:
Nested Loop (rows=1 width=7) (actual rows=1 loops=1)
-> Index Scan using idx2 on t2 (rows=4 width=7) (actual rows=1000000 loops=1)
-> Index Scan using idx1 on t1 (rows=1 width=7) (actual rows=0 loops=1000000)
因此,有时选择 t1 作为外循环,有时选择 t2。如果选择第二个计划,性能绝对是糟糕的。
我的问题是,如何强制 Postgres 始终使用第一个查询计划,t1 在外循环?
【问题讨论】:
标签: postgresql inner-join