我在查询中遇到了嵌套查询的奇怪行为IN
。执行时间出乎意料地更长。我已将查询简化为最小化以进行调试。
这是SQL:
select * from test_table where id in (
select 1
)
而且EXPLAIN ANALYZE
是
"Hash Semi Join (cost=0.03..0.07 rows=1 width=2198) (actual time=0.615..10297.491 rows=1 loops=1)"
" Hash Cond: (test_table.id = (1))"
" -> Data Node Scan on test_table "_REMOTE_TABLE_QUERY_" (cost=0.00..0.00 rows=1000 width=2198) (actual time=0.593..7494.668 rows=3008167 loops=1)"
" Node/s: datanode"
" -> Hash (cost=0.02..0.02 rows=1 width=4) (actual time=0.006..0.006 rows=1 loops=1)"
" Buckets: 1024 Batches: 1 Memory Usage: 1kB"
" -> Result (cost=0.00..0.01 rows=1 width=0) (actual time=0.001..0.001 rows=1 loops=1)"
"Total runtime: 10628.786 ms"
因为我使用select 1
嵌套查询,它只返回 1,所以我修改我的 SQL 如下:
select * from test_table where id in (
1 -- NO SELECT
)
而且EXPLAIN ANALYZE
是
"Data Node Scan on "__REMOTE_FQS_QUERY__" (cost=0.00..0.00 rows=0 width=0) (actual time=1.109..1.112 rows=1 loops=1)"
" Node/s: datanode"
"Total runtime: 1.152 ms"
现在我的问题是,为什么这里的时差很大?我能做些什么来改善我在这里的情况?我的 psql 版本是9.3.1