Skip to content

Instantly share code, notes, and snippets.

@d
Created August 29, 2018 03:04
Show Gist options
  • Save d/675b275b07bc58e0c3a4acc7c1314ba5 to your computer and use it in GitHub Desktop.
Save d/675b275b07bc58e0c3a4acc7c1314ba5 to your computer and use it in GitHub Desktop.
n\time (ms) \branch 5X moving avg backport patch
16384 53.453 1694.091
32768 95.186 6640.734
65536 187.03 28848.276
131072 398.689 113056.203
262144 809.299 459619.552
524288 1813.492 1876957.178
1048576 4751.661 7464597.714
CREATE TEMP TABLE foo (a int, b int, c int, d int)
DISTRIBUTED BY (a);
CREATE OR REPLACE FUNCTION gen_data(scale int) RETURNS void LANGUAGE plpgSQL
AS $fn$
DECLARE
n CONSTANT int := 2 ^ scale;
partition_size CONSTANT int := n / 8;
BEGIN
TRUNCATE foo;
INSERT INTO foo (a, b, c, d)
SELECT a, b,
CASE
WHEN b % 2 = 0 THEN partition_size - b
ELSE (partition_size - b) / 2
END,
CASE
WHEN b % 2 = 0 THEN b / 2
ELSE b
END
FROM (SELECT * FROM generate_series(1, 8) a, generate_series(1, partition_size) b) yolo;
END
$fn$;
CREATE TEMP VIEW yolo AS
SELECT avg(b) OVER (w) AS avg_b
FROM foo
WINDOW w AS
(
PARTITION BY a
ORDER BY b
ROWS BETWEEN d PRECEDING AND c FOLLOWING
)
;
\timing on
\o /dev/null
SELECT gen_data(14);
SELECT * FROM yolo;
SELECT * FROM yolo;
SELECT * FROM yolo;
SELECT gen_data(15);
SELECT * FROM yolo;
SELECT * FROM yolo;
SELECT * FROM yolo;
SELECT gen_data(16);
SELECT * FROM yolo;
SELECT * FROM yolo;
SELECT * FROM yolo;
SELECT gen_data(17);
SELECT * FROM yolo;
SELECT * FROM yolo;
SELECT * FROM yolo;
SELECT gen_data(18);
SELECT * FROM yolo;
SELECT * FROM yolo;
SELECT * FROM yolo;
SELECT gen_data(19);
SELECT * FROM yolo;
SELECT * FROM yolo;
SELECT * FROM yolo;
SELECT gen_data(20);
SELECT * FROM yolo;
SELECT * FROM yolo;
SELECT * FROM yolo;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment