Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Shortcut performance can be low because of to_jsonb and jsonb_agg #194

@remidewitte

Description

@remidewitte

Shortcuts generates queries that have a performance penalty.
Both tables in the example below have a column that is a jsonb that can be large (few kb).

What do you think ?

select

With jsonb_agg

EXPLAIN (ANALYZE, BUFFERS, MEMORY)
SELECT coalesce(jsonb_agg(result), '[]') AS result FROM (SELECT to_jsonb("cache_article".*) AS result FROM "cache_article" LIMIT 10000) AS "sq_cache_article";
Aggregate (cost=2673.82..2673.83 rows=1 width=32) (actual time=411.650..411.654 rows=1 loops=1)
Buffers: shared hit=2481
-> Limit (cost=0.00..2648.81 rows=10000 width=32) (actual time=0.145..105.108 rows=10000 loops=1)
Buffers: shared hit=2481
-> Seq Scan on cache_article (cost=0.00..25499.34 rows=96267 width=32) (actual time=0.144..103.799 rows=10000 loops=1)
Buffers: shared hit=2481
Planning:
Buffers: shared hit=103
Memory: used=19kB allocated=64kB
Planning Time: 0.828 ms
Execution Time: 424.653 ms

With to_jsonb

EXPLAIN (ANALYZE, BUFFERS, MEMORY)
SELECT to_jsonb("cache_article".*) AS result FROM "cache_article" LIMIT 10000;
Limit (cost=0.00..2648.81 rows=10000 width=32) (actual time=0.172..144.909 rows=10000 loops=1)
Buffers: shared hit=2481
-> Seq Scan on cache_article (cost=0.00..25499.34 rows=96267 width=32) (actual time=0.171..143.755 rows=10000 loops=1)
Buffers: shared hit=2481
Planning:
Buffers: shared hit=94
Memory: used=8kB allocated=40kB
Planning Time: 0.759 ms
Execution Time: 145.645 ms
Execution Time: 94.797 ms

Without

EXPLAIN (ANALYZE, BUFFERS, MEMORY)
SELECT "cache_article".* AS result FROM "cache_article" LIMIT 10000;
Limit (cost=0.00..2623.81 rows=10000 width=998) (actual time=0.033..14.960 rows=10000 loops=1)
Buffers: shared hit=2079
-> Seq Scan on cache_article (cost=0.00..25258.67 rows=96267 width=998) (actual time=0.031..14.139 rows=10000 loops=1)
Buffers: shared hit=2079
Planning:
Buffers: shared hit=94
Memory: used=9kB allocated=64kB
Planning Time: 0.758 ms
Execution Time: 15.479 ms

selectOne

With to_jsonb

EXPLAIN (ANALYSE, BUFFERS) SELECT to_jsonb("cache".*) AS result
FROM "cache"
WHERE (
 "full_path" = '/doc/20002463' AND
 "operation_id" = 'get-doc'
) LIMIT 1
Limit (cost=0.55..8.57 rows=1 width=32) (actual time=109.022..109.024 rows=1 loops=1)
Buffers: shared hit=74
-> Index Scan using cache_pkey on cache (cost=0.55..8.57 rows=1 width=32) (actual time=109.020..109.020 rows=1 loops=1)
Index Cond: (((operation_id)::text = 'get-doc'::text) AND (full_path = '/doc/20002463'::text))
Buffers: shared hit=74
Planning:
Buffers: shared hit=180
Planning Time: 1.731 ms
Execution Time: 113.602 ms

Without

EXPLAIN (ANALYSE, BUFFERS) SELECT *
FROM "cache"
WHERE (
 "full_path" = '/doc/20002463' AND
 "operation_id" = 'get-doc'
) LIMIT 1
Limit (cost=0.55..8.57 rows=1 width=1085) (actual time=0.076..0.077 rows=1 loops=1)
Buffers: shared hit=5
-> Index Scan using cache_pkey on cache (cost=0.55..8.57 rows=1 width=1085) (actual time=0.074..0.075 rows=1 loops=1)
Index Cond: (((operation_id)::text = 'get-doc'::text) AND (full_path = '/doc/20002463'::text))
Buffers: shared hit=5
Planning:
Buffers: shared hit=180
Planning Time: 1.189 ms
Execution Time: 0.167 ms

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions