-
-
Notifications
You must be signed in to change notification settings - Fork 278
Closed
Description
Hello Permify team,
While working with multiple tenants, I noticed that some queries in the codebase could be significantly more efficient if composite indexes were added.
- Function
DataReader.HeadSnapshot
The following query is used:
builder := r.database.Builder.Select("MAX(id)").From(TransactionsTable).Where(squirrel.Eq{"tenant_id": tenantID})
For optimal performance with large datasets and multiple tenants, a composite index on the transactions table is recommended:
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_transactions_tenant_id ON transactions (tenant_id, id);
- Function
SchemaReader.HeadVersion
The following query is used:
query, args, err = r.database.Builder.
Select("version").From(SchemaDefinitionTable).Where(squirrel.Eq{"tenant_id": tenantID}).OrderBy("version DESC").Limit(1).
ToSql()
This would benefit from a composite index on the schema_definitions table:
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_schema_tenant_version ON schema_definitions (tenant_id, version);
Thank you for considering this improvement!
If needed, I can prepare a PR for this.
Metadata
Metadata
Assignees
Labels
No labels