IN查询的奇怪行为

时间:2014-10-23 14:10:30

标签: postgresql postgres-xc

我在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

0 个答案:

没有答案