Skip to content

Fix: faster wildcard queries for JSONB fields #943

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 8, 2024

Conversation

psrok1
Copy link
Member

@psrok1 psrok1 commented Jun 8, 2024

Your checklist for this pull request

  • I've read the contributing guideline.
  • I've tested my changes by building and running the project, and testing changed functionality (if applicable)
  • I've added automated tests for my change (if applicable, optional)
  • I've updated documentation to reflect my change (if applicable)

What is the current behaviour?

JSONB wildcard queries are very slow because there is no supported JSONB "LIKE" operator that works with current implementation of GIN index. In the same time, LIKE on jsonb_path_query function result is very expensive.

So queries like:

cfg:"*/content/*"

may take minutes, especially when matching rows are really far in config table, which is far more than usual request timeout setting.

What is the new behaviour?

Configs are too long to be indexed by simple btree, but we found that simple object.cfg::text LIKE "%...%" works much faster even if it performs a full table scan. It's not exactly what we want if we use key predicate (cfg.urls:*/content/*) so it's still combined with the original jsonb_path_query-based solution with hope that PostgreSQL will use simple LIKE to pre-filter the rows before using function-based query. On our database data it usually does.

In future PRs I plan to optimize it further to avoid full scan at all.

Test plan

Closing issues

@psrok1 psrok1 added the type:bug Something isn't working label Jun 8, 2024
@psrok1 psrok1 merged commit b28b828 into master Jun 8, 2024
12 checks passed
@psrok1 psrok1 deleted the fix/jsonb-wildcard-queries branch June 8, 2024 16:28
@psrok1 psrok1 mentioned this pull request Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant