概述
数据库索引优化工作流是一个中级难度的工作流。分析慢查询,识别缺失或冗余索引,以对写性能最小影响应用变更。该工作流的目标是:Improve database query performance through strategic index optimization.
环境准备
执行本工作流需要以下环境:A database with production-like data volume;Slow query log or query analyzer access;Database migration tool for index changes。请确保在开始前准备好所有必需的工具和资源,以避免中途因环境问题中断。
执行步骤
本工作流包含5个步骤。步骤1:Identify slow queries——Analyze the slow query log to find queries taking more than 100ms. 步骤2:Analyze existing indexes——Review current indexes and identify redundant or unused indexes. 步骤3:Create missing indexes——Add indexes for slow queries that lack appropriate index support. 步骤4:Drop unused indexes——Remove indexes that are never used to reduce write overhead. 步骤5:Verify improvement——Re-run slow queries to confirm improved execution times.
验证与清理
预期结果:Database query performance is improved, and the index set is optimized for the current workload.。验证方法:Compare query execution times before and after; confirm no query regressed.。清理操作:Roll back index migrations if write performance degrades significantly.。estimatedMinutes 为 90 分钟。
代码示例
EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 123;
CREATE INDEX idx_orders_customer ON orders(customer_id, created_at);