Search code examples
postgresqlgoogle-cloud-sqlpostgresql-9.6

Why is a union of two queries faster than a single one of the unioned queries?


I'm debugging query times in 2x Google Cloud SQL Postgres 9.6 instances with Autovacuuming. Staging (no traffic) 7.5gb + 2vCPU. And production: 37.5gb with 10vCPU. The results are the same and confusing.

Indexes:

  • trade_user1
  • trade_user2

Consistently 100-120ms:

SELECT * FROM "Trade" WHERE "user1" = 1
UNION
SELECT * FROM "Trade" WHERE "user2" = 1
LIMIT 24;
Limit  (cost=221.92..222.16 rows=24 width=1187) (actual time=0.115..0.124 rows=24 loops=1)
  ->  HashAggregate  (cost=221.92..222.46 rows=54 width=1187) (actual time=0.115..0.121 rows=24 loops=1)
        Group Key: id, status, user1, user2
        ->  Append  (cost=4.60..218.55 rows=54 width=1187) (actual time=0.024..0.076 rows=26 loops=1)
              ->  Bitmap Heap Scan on "Trade"  (cost=4.60..89.99 rows=22 width=155) (actual time=0.024..0.061 rows=23 loops=1)
                    Recheck Cond: (user1 = 1)
                    Heap Blocks: exact=20
                    ->  Bitmap Index Scan on trade_depositor_user_id  (cost=0.00..4.59 rows=22 width=0) (actual time=0.016..0.016 rows=23 loops=1)
                          Index Cond: (user1 = 1)
              ->  Bitmap Heap Scan on "Trade" "Trade_1"  (cost=4.67..128.02 rows=32 width=155) (actual time=0.011..0.014 rows=3 loops=1)
                    Recheck Cond: (user2 = 1)
                    Heap Blocks: exact=3
                    ->  Bitmap Index Scan on trade_withdrawer_user_id  (cost=0.00..4.67 rows=32 width=0) (actual time=0.009..0.009 rows=3 loops=1)
                          Index Cond: (user2 = 1)
Planning time: 0.224 ms
Execution time: 0.189 ms

Consistently 280-350ms:

SELECT * FROM "Trade" WHERE "user1" = 1
Bitmap Heap Scan on "Trade"  (cost=4.60..89.99 rows=22 width=155) (actual time=0.023..0.054 rows=23 loops=1)
  Recheck Cond: (user1 = 1)
  Heap Blocks: exact=20
  ->  Bitmap Index Scan on trade_user1  (cost=0.00..4.59 rows=22 width=0) (actual time=0.015..0.015 rows=23 loops=1)
        Index Cond: (user2 = 1)
Planning time: 0.077 ms
Execution time: 0.078 ms

Both queries return equal result set sizes. I've tried different variations of the simpler query like ordering by ID ASC/DESC.


Solution

  • It seems the timings that I was using were from PgAdmin and after SSHing into the actual database network/server I can see that the difference was negligible between the two variants and it was actually the same as appears in the EXPLAIN ANALYSE.

    So it's actually not quicker doing a UNION than the queries individually/