Skip to content
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

COALESCE based null handling results in poor query planning #1939

Closed
magJ opened this issue Feb 28, 2023 · 2 comments
Closed

COALESCE based null handling results in poor query planning #1939

magJ opened this issue Feb 28, 2023 · 2 comments

Comments

@magJ
Copy link

magJ commented Feb 28, 2023

I have a collection/table submissionEvents, it has a column submisssionId which I have manually added an index for.

When I query my collection via the records list API, the query is very very slow, and continues to get slower as the table grows in size.

When I query the table directly using the sqlite cli, the query is very fast.
I ran pocketbase in debug mode to see what query it was producing, and saw that it is adding COALESCE in the where conditions, it seems like this confuses the sqlite query planner, and results in poor query performance.

Query without COALESCE, runs very fast, and we can see that it's using my index.

explain query plan SELECT `submissionEvents`.* FROM `submissionEvents` WHERE submissionEvents.submissionId = 47581 ORDER BY submissionEvents.created DESC LIMIT 100;
QUERY PLAN
|--SEARCH submissionEvents USING INDEX ix_submissionId (submissionId=?)
`--USE TEMP B-TREE FOR ORDER BY

Query with COALESCE, runs very very slow, does not use my index.

explain query plan SELECT `submissionEvents`.* FROM `submissionEvents` WHERE COALESCE(submissionEvents.submissionId, '') = COALESCE(47581, '') ORDER BY submissionEvents.created DESC LIMIT 100;
QUERY PLAN
`--SCAN submissionEvents USING INDEX _lyyo499fdv233kr_created_idx

It seems like COALESCE was added a while back to avoid needing to filter nulls in the application code.

If the null filtering could be done in the application, I think it would produce simpler queries and allow sqlite to do a better job of planning.

@ganigeorgiev
Copy link
Member

ganigeorgiev commented Feb 28, 2023

This will be optimized in the next release together with the other index related tasks.

@ganigeorgiev
Copy link
Member

The redundant COALESCE statements were removed in the develop branch. The changes will be available with v0.14.0 release (most likely next week or the week following it).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

No branches or pull requests

2 participants