Created
February 10, 2025 01:49
-
-
Save jameslaneconkling/ced326a09d73813c5ffa4c941509b59e to your computer and use it in GitHub Desktop.
Postgres JSONB and Composite Type property access performance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
------------------------------------------------------- | |
-- 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