Query Performance Audit and Safe Indexing Script
Generate a query log parsing and zero-downtime indexing script to remediate sluggish analytical workloads.
Deploy this template when productivity bottlenecks require automated slow-query log analysis and concurrent index generation. It yields clean, idempotent scripts tailored to your database engine.
Role: Staff Performance DBA specializing in low-overhead query optimization and non-blocking database tuning.
Context
- Database system: {{rdbms_type}}
- Query latency cutoff: {{slow_query_threshold_ms}} ms
- Operating environment: {{workload_environment}}
- Source telemetry feed: {{audit_log_format}}
- Maximum lock wait threshold: {{lock_timeout_seconds}} seconds
- Orchestration medium: {{output_script_language}}
Task
Construct an end-to-end audit and safe index generation script that extracts slow query metrics, identifies missing indexes, and builds non-blocking indexing DDL.
Method
- Connect to telemetry outputs formatted as {{audit_log_format}} within {{rdbms_type}}.
- Filter query signatures exceeding the latency cutoff of {{slow_query_threshold_ms}} ms.
- Aggregate queries by query fingerprint and extract sequential scan access paths.
- Map scanned filter predicates and join keys to target table definitions.
- Generate non-blocking index creation commands tailored for {{workload_environment}}.
- Inject session-level lock timeouts using {{lock_timeout_seconds}} before each DDL statement.
- Add pre-execution index existence checks to make index creation strictly idempotent.
- Include health verification queries measuring index usage and storage footprint.
Constraints
- Index creation MUST use online/concurrent building modes to prevent blocking live writes.
- The script MUST NOT create duplicate or redundant composite indexes with identical leading columns.
- All DDL statements must set lock timeout limits matching {{lock_timeout_seconds}}.
- Syntax and command parameters must be fully valid for {{output_script_language}} and {{rdbms_type}}.
Output format
Return the deliverable organized into the following exact sections:
- Audit & Extraction Script: Formatted code block in {{output_script_language}} to parse logs.
- Idempotent Migration DDL: SQL code block containing concurrent index creation statements.
- Operational Rollback Block: SQL statements to drop newly added indexes cleanly if needed.
Self-review
- Confirm that online indexing clauses (e.g., CONCURRENTLY / ONLINE = ON) are present.
- Validate that lock wait limits enforce {{lock_timeout_seconds}} prior to every DDL attempt.
- Verify query filtering matches {{slow_query_threshold_ms}} ms without dropping top offenders.
Explicit role, a named task, and discrete steps the model can follow.
Background, inputs and variables the model needs before it starts.
Hard boundaries — what the model must and must not do.
A named, field-level shape for the response.
Ordered work items that force analysis before an answer.
Length and structure that travel across frontier models.
Signal density — instruction weight without padding.
Documented variables so the scaffold adapts to new inputs.
Quality bar, assumptions and behaviour when inputs are thin.
How much real usage the template has behind it.