Skip to content

Instantly share code, notes, and snippets.

@jameslaneconkling
Created February 10, 2025 01:49
Show Gist options
  • Save jameslaneconkling/ced326a09d73813c5ffa4c941509b59e to your computer and use it in GitHub Desktop.
Save jameslaneconkling/ced326a09d73813c5ffa4c941509b59e to your computer and use it in GitHub Desktop.
Postgres JSONB and Composite Type property access performance
-------------------------------------------------------
-- create small object and measure time to access a key
-------------------------------------------------------
CREATE TABLE small_object AS (
SELECT jsonb_object_agg(n, 'abc') AS obj FROM generate_series(1,10) n
);
SELECT small_object.obj->'10' FROM small_object;
-- Time: 0.996 ms
-------------------------------------------------------
-- create large object and measure time to access a key
-------------------------------------------------------
CREATE TABLE large_object AS (
SELECT jsonb_object_agg(n, 'abc') AS obj FROM generate_series(1,1000000) n
);
SELECT large_object.obj->'1000000' FROM large_object;
-- Time: 20.994 ms
-----------------------------------------------------------------
-- create a large composite type and measure time to access a key
-----------------------------------------------------------------
CREATE TYPE small_composite_type AS (
a integer,
b integer,
c integer,
d integer,
e integer,
f integer,
g integer,
h integer,
i integer,
j integer
);
CREATE TABLE composite_type AS (
SELECT (1, 1, 1, 1, 1, 1, 1, 1, 1, 1)::small_composite_type AS composite_type
);
SELECT (composite_type.composite_type).j FROM composite_type;
-- Time: 0.418 ms
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment