Jsonb index optimization #1869
Closed
ndevilleBE
started this conversation in
General
Replies: 1 comment 3 replies
-
Can you try the index:
|
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Dear,
I'm trying to optimize Sensorthings responsiveness by indexing Jsonb records. But I can't figure out how to make it work properly.
The url I'm testing is:
https://sensors.naturalsciences.be/sta/v1.1/Locations?$filter=properties/cruise_identifier%20eq%20'2022/08'%20&$expand=HistoricalLocations($select=time)
As you can see, I have a key 'cruise_identifier' in the JSONB properties column of the LOCATIONS table. The values I'm trying to find is 2022/08.
The response can take sometimes up to 1 minute which is too much for a simple query like that. If I check the logs, the query is:
select "e0"."ID", "e0"."PROPERTIES", "e0"."NAME", "e0"."ENCODING_TYPE", "e0"."DESCRIPTION", "e0"."LOCATION" from "LOCATIONS" as "e0" where (true and "e0"."PROPERTIES"::jsonb#>>'{ cruise_identifier }' = cast('2022/08' as varchar)) order by "e0"."ID" asc offset 0 rows fetch next 101 rows only
In PostgreSQL 15, the execution plan is:
So it uses the index on the Primary Key of the Locations.
I try to create an index on the actual key value but it makes no difference to the query planning.
CREATE INDEX locations_properties_idx ON public."LOCATIONS" using btree ((("PROPERTIES" ->> 'cruise_identifier')::text));
Do you have any suggestion on the best way to improve the performances?!
Many thanks
Beta Was this translation helpful? Give feedback.
All reactions