Sabuj Hassan
Sabuj Hassan

Reputation: 39443

Strange behavior of IN query

I am experiencing a strange behavior with nested query inside the IN query. It is taking unexpectedly longer time to execute. I have simplified my query to minimal for debugging.

Here is the SQL:

select * from test_table where id in (
  select 1
)

And the EXPLAIN ANALYZE is

"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"

As I am using select 1 as nested query and it only returns 1, So I am modifying my SQL as below:

select * from test_table where id in (
    1  -- NO SELECT
)

And the EXPLAIN ANALYZE is

"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"

Now my question is, why the time difference is huge here? And what can I do to improve my situation here? My psql version is 9.3.1

Upvotes: 0

Views: 48

Answers (0)

Related Questions