p2-inc / keycloak-orgs

Single realm, multi-tenancy for SaaS apps
https://phasetwo.io
Other
418 stars 72 forks source link

Add index to ORGANIZATION_ATTRIBUTE on ORGANIZATION_ID column #138

Closed phamann closed 12 months ago

phamann commented 1 year ago

What?

Adds a database index to the ORGANIZATION_ATTRIBUTE table on the ORGANIZATION_ID column, to improve query performance when searching orgs by attribute such as /realms/:realm/orgs?q=foo=bar.

Why?

We've noticed slow queries to the search endpoint when querying a database with both a lot of orgs and with each org having a lot of attributes.

Example slow queries:

select a1_0.ORGANIZATION_ID,a1_0.ID,a1_0.NAME,a1_0.VALUE from ORGANIZATION_ATTRIBUTE a1_0 where a1_0.ORGANIZATION_ID=?
SELECT `o1_0` . `ID` , `o1_0` . `CREATED_BY_USER_ID` , `o1_0` . `DISPLAY_NAME` , `o1_0` . `NAME` , `o1_0` . `REALM_ID` , `o1_0` . `URL` FROM ORGANIZATION `o1_0` LEFT JOIN `ORGANIZATION_ATTRIBUTE` `a1_0` ON `o1_0` . `ID` = `a1_0` . `ORGANIZATION_ID` WHERE `lower` ( `a1_0` . `NAME` ) = ? AND `lower` ( `a1_0` . `VALUE` ) = ? AND `o1_0` . `REALM_ID` = ? ORDER BY `o1_0` . `NAME` LIMIT ?
xgp commented 1 year ago

@phamann this passes the tests on other DB types. Let me know if you want to add the precondition discussed before merge.

<changeSet>
    <preConditions onFail="MARK_RAN">
        <indexExists indexName="IDX_ORGANIZATION_ATTRIBUTE" />
    </preConditions>
    ...
</changeSet>
xgp commented 12 months ago

Completed in #145 145