Przejdź do głównej zawartości

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;