supabase / postgres-meta

A RESTful API for managing your Postgres. Fetch tables, add roles, and run queries
https://supabase.com
Apache License 2.0
941 stars 126 forks source link

fix: replace CTEs with joins #586

Closed soedirgo closed 1 year ago

soedirgo commented 1 year ago

Benchmark on 1000 tables, 4 columns each:

Schema:

do $$
    DECLARE myvar integer;
begin
    for myvar in 1..1000 loop
        EXECUTE format('CREATE TABLE sbtest%s (
        id SERIAL NOT NULL,
        k INTEGER NOT NULL,
        c CHAR(120) NOT NULL,
        pad CHAR(60) NOT NULL,
        PRIMARY KEY (id))', myvar);
    end loop;
end; $$

Workload: GET /tables?include_columns=false

Before: https://explain.dalibo.com/plan/h044ad2ae694gfc7

  Time (mean ± σ):      8.886 s ±  0.220 s    [User: 0.100 s, System: 0.022 s]
  Range (min … max):    8.666 s …  9.201 s    10 runs

After: https://explain.dalibo.com/plan/0a506f9b827e1b4c

  Time (mean ± σ):     404.1 ms ± 103.5 ms    [User: 94.7 ms, System: 15.1 ms]
  Range (min … max):   352.2 ms … 697.2 ms    10 runs

Conclusion: 20x speedup by using group by instead of aggregate filters.

soedirgo commented 1 year ago

I've tested that the output is consistent on /tables and /generators/typescript between this and the one on prod, so I think we're good :+1: