ื“ืœื’ ืœืชื•ื›ืŸ ื”ืจืืฉื™

Database Indexing Strategy

Optimize query performance with proper indexing.

Default Indexesโ€‹

All entities include indexes on:

  • id (Primary Key)
  • tenantId (Multi-tenancy filter)
  • organizationId (Organization filter)

Common Index Patternsโ€‹

Single Column Indexโ€‹

@MultiORMEntity("time_log")
export class TimeLog extends TenantOrganizationBaseEntity {
@MultiORMColumn()
@Index()
employeeId: string;

@MultiORMColumn()
@Index()
startedAt: Date;
}

Composite Indexโ€‹

@Index(["tenantId", "organizationId", "employeeId"])
@MultiORMEntity("time_log")
export class TimeLog extends TenantOrganizationBaseEntity {}

Unique Indexโ€‹

@Index(["email", "tenantId"], { unique: true })
@MultiORMEntity("user")
export class User extends TenantBaseEntity {}
TableColumnsTypePurpose
time_log(employeeId, startedAt)CompositeTime reports
time_log(projectId, startedAt)CompositeProject reports
task(projectId, status)CompositeTask lists
task(title)GIN/FTSSearch
employee(organizationId, isActive)CompositeEmployee lists
invoice(organizationId, status)CompositeInvoice filters
contact(organizationId, contactType)CompositeCRM queries

Query Analysisโ€‹

-- PostgreSQL
EXPLAIN ANALYZE SELECT * FROM time_log
WHERE employee_id = 'uuid'
AND started_at BETWEEN '2025-01-01' AND '2025-01-31';

Index Monitoringโ€‹

-- Unused indexes
SELECT indexrelname, idx_scan
FROM pg_stat_user_indexes
WHERE idx_scan = 0
ORDER BY pg_relation_size(indexrelid) DESC;