keycloak: Slow user query by attribute
Describe the bug
If querying users by attribute values the response is really slow.
On further investigation the query is missing the database index because attribute name and value is queried by lower value, eg. lower(name) and lower(value).
Either remove lower and match the actual value (which seems to be the more reasonable approach) or add a matching new index to the attribute table:
CREATE INDEX index ON user_attribute(lower(name), lower(value));
Version
18.0.1
Expected behavior
Query by attribute returns in a reasonable time.
Actual behavior
Query takes really long and produces a lot of load on the database.
How to Reproduce?
Just query by attribute.
Anything else?
Adding a max value to the query totally breaks the request. (Timeouts)
About this issue
- Original URL
- State: open
- Created 2 years ago
- Comments: 17 (13 by maintainers)
Commits related to this issue
- Remove lowercasing upon search by attribute search Fixes: #12671 — committed to hmlnarik/keycloak by hmlnarik 2 years ago
Actually, the bug @hmlnarik mentioned is still there. But I can provide a PR to fix that.
I just checked how we used to do it on the image and yeah, you would have to alter some jars - so it’s a bit more involved. We had a script to insert migrations:
You would have to call this script from while building the image like that:
Maybe it would be in fact easier to just run a custom script against the database.