Skip to content

Instantly share code, notes, and snippets.

@onderkalaci
Created February 16, 2021 11:34
Show Gist options
  • Save onderkalaci/5e71a43ed260dad44670629065a11eae to your computer and use it in GitHub Desktop.
Save onderkalaci/5e71a43ed260dad44670629065a11eae to your computer and use it in GitHub Desktop.
Alias same with column name
CREATE TABLE test (a int, b text);
EXPLAIN (ANALYZE, VERBOSE)
SELECT
a,
case when b = '1' then '-'
when b is null then '-'
else b end as b,
count(*)
FROM
test
GROUP BY a, b;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------
HashAggregate (cost=32.23..34.73 rows=200 width=76) (actual time=0.006..0.006 rows=0 loops=1)
Output: a, CASE WHEN (b = '1'::text) THEN '-'::text WHEN (b IS NULL) THEN '-'::text ELSE b END, count(*), b
Group Key: test.a, test.b
-> Seq Scan on public.test (cost=0.00..22.70 rows=1270 width=36) (actual time=0.004..0.004 rows=0 loops=1)
Output: a, b
Planning Time: 0.726 ms
Execution Time: 0.065 ms
(7 rows)
Time: 2.074 ms
-- now, same query on distributed tables
SELECT create_distributed_table('test', 'a');
EXPLAIN (ANALYZE, VERBOSE)
SELECT
a,
case when b = '1' then '-'
when b is null then '-'
else b end as b,
count(*)
FROM
test
GROUP BY a, b;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------
Custom Scan (Citus Adaptive) (cost=0.00..0.00 rows=100000 width=76) (actual time=29.075..29.079 rows=0 loops=1)
Output: remote_scan.a, remote_scan.b, remote_scan.count, remote_scan.worker_column_4
Task Count: 32
Tasks Shown: One of 32
-> Task
Node: host=localhost port=9700 dbname=postgres
-> HashAggregate (cost=32.23..34.73 rows=200 width=76) (actual time=0.008..0.008 rows=0 loops=1)
Output: a, CASE WHEN (b = '1'::text) THEN '-'::text WHEN (b IS NULL) THEN '-'::text ELSE b END, count(*), b
Group Key: test.a, test.b
-> Seq Scan on public.test_102074 test (cost=0.00..22.70 rows=1270 width=36) (actual time=0.007..0.007 rows=0 loops=1)
Output: a, b
Planning Time: 0.100 ms
Execution Time: 0.045 ms
Planning Time: 1.822 ms
Execution Time: 29.103 ms
(15 rows)
Time: 35.042 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment