Skip to content

Missing composite indexes for efficient queries for multi-tenant setup #2370

@zhurs

Description

@zhurs

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.

  1. 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);
  1. 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions