githubEdit

Indexing Notes

Recommended database indexes for Elsa Workflows persistence stores to optimize common query patterns.

This document provides indexing recommendations for Elsa Workflows persistence stores. Proper indexing is essential for production performance.

Overview

Elsa's persistence layer executes queries against several core tables/collections. Without proper indexes, these queries may result in full table scans, degrading performance under load.

Key Query Patterns

Query Pattern
Tables Involved
Index Recommendation

Resume by bookmark hash

Bookmarks

(activity_type_name, hash)

List instances by status

WorkflowInstances

(status), (status, definition_id)

Correlate instances

WorkflowInstances

(correlation_id)

Cleanup old instances

WorkflowInstances

(updated_at), (finished_at)

Find bookmarks for instance

Bookmarks

(workflow_instance_id)

List incidents

Incidents

(workflow_instance_id), (timestamp)

PostgreSQL Indexes

Workflow Instances

-- Primary lookup by ID (usually covered by PK)
-- CREATE INDEX idx_workflow_instances_id ON workflow_instances(id);

-- Query by correlation ID (very common for HTTP workflows)
CREATE INDEX idx_workflow_instances_correlation_id 
    ON workflow_instances(correlation_id)
    WHERE correlation_id IS NOT NULL;

-- Query by status (list pending, running, faulted, etc.)
CREATE INDEX idx_workflow_instances_status 
    ON workflow_instances(status);

-- Query by definition (list all instances of a workflow)
CREATE INDEX idx_workflow_instances_definition_id 
    ON workflow_instances(definition_id);

-- Composite for filtered status queries
CREATE INDEX idx_workflow_instances_status_definition 
    ON workflow_instances(status, definition_id);

-- Retention queries (cleanup by age)
CREATE INDEX idx_workflow_instances_updated_at 
    ON workflow_instances(updated_at DESC);

CREATE INDEX idx_workflow_instances_finished_at 
    ON workflow_instances(finished_at)
    WHERE finished_at IS NOT NULL;

-- List by sub-status (more granular than status)
CREATE INDEX idx_workflow_instances_sub_status 
    ON workflow_instances(sub_status);

Bookmarks

Activity Execution Records

Workflow Execution Logs

Incidents

Workflow Inbox Messages

SQL Server Indexes

SQL Server uses similar index patterns with different syntax:

MongoDB Indexes

MongoDB requires explicit index creation. Use the MongoDB shell or driver:

Index Maintenance

PostgreSQL

SQL Server

MongoDB

Performance Monitoring

Identifying Missing Indexes

PostgreSQL:

SQL Server:

MongoDB:

Best Practices

  1. Start with recommended indexes — Apply the indexes above before production deployment.

  2. Monitor query performance — Use database-native tools to identify slow queries.

  3. Don't over-index — Each index adds write overhead and storage. Only add indexes for actual query patterns.

  4. Partial indexes save space — Use WHERE clauses (PostgreSQL) or partialFilterExpression (MongoDB) to index only relevant rows.

  5. Covering indexes reduce I/O — Include frequently accessed columns in the index to avoid table lookups.

  6. Regular maintenance — Schedule index maintenance during low-traffic periods.

  7. Test with production-like data — Index performance varies with data distribution. Test with realistic data volumes.

Vendor Documentation

For detailed index tuning beyond these recommendations:


Last Updated: 2025-11-28

Last updated