Query optimization when using a JSON field Query optimization when using a JSON field sql sql

Query optimization when using a JSON field


Change Planner Method Configuration and force planer not to do seqscan

eg

      SET enable_seqscan = OFF;       SELECT   n.*        FROM     node n               WHERE    node_type_id = '2'               AND      properties @> '{"slug":"wild-castles"}'::JSONB             ORDER BY n.id ASC OFFSET 0 LIMIT 10;


In my experience, you sometimes have to trick the query planner to get it to perform well, and it takes some tweaking and fiddling with...

I would try running this to see how it performs:

SELECT nn.* FROM (    SELECT n.*    FROM   node n    WHERE  node_type_id = '2'    AND    properties @> '{"slug":"wild-castles"}'::JSONB) nnORDER BY nn.id ASC OFFSET 0 LIMIT 10;