
Database Indexing Strategy
- 437 installs
- 305 repo stars
- Updated March 4, 2026
- aj-geddes/useful-ai-prompts
database-indexing-strategy is an agent skill that helps developers design PostgreSQL and MySQL indexing strategies by analyzing access patterns and balancing read speed with write overhead.
About
database-indexing-strategy is an agent skill from aj-geddes/useful-ai-prompts for planning indexes on PostgreSQL and MySQL workloads. It covers B-tree, Hash, GiST, and BRIN index types, plus composite, partial, filtered, and expression indexes with concrete CREATE INDEX examples. Three reference guides ship in the skill's references/ directory for PostgreSQL types, MySQL types, and single- versus multi-column patterns. Use it when EXPLAIN shows sequential scans, lock contention rises on hot tables, or new queries need index coverage without bloating storage. The skill emphasizes maintenance and monitoring alongside creation so indexes stay aligned with real access patterns.
- Maps queries to composite and covering indexes
- Weighs read gains against write amplification
- Covers partial, functional, and unique indexes
- Plans migration and backfill sequencing
- Ties indexes to EXPLAIN plans and SLOs
Database Indexing Strategy by the numbers
- 437 all-time installs (skills.sh)
- Ranked #134 of 911 Databases skills by installs in the Skillselion catalog
- Data as of Aug 5, 2026 (Skillselion catalog sync)
npx skills add https://github.com/aj-geddes/useful-ai-prompts --skill database-indexing-strategyAdd your badge
Show developers this skill is listed on Skillselion. Paste this into your README.
| Installs | 437 |
|---|---|
| repo stars | ★ 305 |
| Last updated | March 4, 2026 |
| Repository | aj-geddes/useful-ai-prompts ↗ |
How do you design database indexes for slow queries?
Design indexing strategies for slow queries by analyzing access patterns, choosing composite and partial indexes, and balancing read speed with write overhead and storage cost.
Who is it for?
Backend developers tuning PostgreSQL or MySQL schemas where EXPLAIN plans show sequential scans or write-heavy tables need selective indexes.
Skip if: NoSQL-only stores or teams that only need connection pooling without query-level index design.
When should I use this skill?
A developer reports slow SQL, asks about composite or partial indexes, or needs GiST, BRIN, or B-tree selection guidance.
What you get
CREATE INDEX statements, composite and partial index plans, and index-type selection notes for PostgreSQL or MySQL
- CREATE INDEX statements
- Index-type recommendation
- Maintenance checklist
By the numbers
- Includes 3 reference guides in the references/ directory for PostgreSQL types, MySQL types, and column-index patterns
- Documents 4 index families: B-tree, Hash, GiST, and BRIN
Files
Database Indexing Strategy
Table of Contents
Overview
Design comprehensive indexing strategies to improve query performance, reduce lock contention, and maintain data integrity. Covers index types, design patterns, and maintenance procedures.
When to Use
- Index creation and planning
- Query performance optimization through indexing
- Index type selection (B-tree, Hash, GiST, BRIN)
- Composite and partial index design
- Index maintenance and monitoring
- Storage optimization with indexes
- Full-text search index design
Quick Start
B-tree Indexes (Default):
-- Standard equality and range queries
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_orders_created_at ON orders(created_at DESC);
-- Composite indexes for multi-column queries
CREATE INDEX idx_orders_user_status
ON orders(user_id, status)
WHERE cancelled_at IS NULL;Reference Guides
Detailed implementations in the references/ directory:
| Guide | Contents |
|---|---|
| PostgreSQL Index Types | PostgreSQL Index Types |
| MySQL Index Types | MySQL Index Types |
| Single Column Indexes | Single Column Indexes, Composite Indexes, Partial/Filtered Indexes, Expression Indexes |
Best Practices
✅ DO
- Follow established patterns and conventions
- Write clean, maintainable code
- Add appropriate documentation
- Test thoroughly before deploying
❌ DON'T
- Skip testing or validation
- Ignore error handling
- Hard-code configuration values
MySQL Index Types
MySQL Index Types
B-tree Indexes:
-- Standard index for most queries
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_orders_user_created
ON orders(user_id, created_at);
-- Prefix indexes for large columns
CREATE INDEX idx_description_prefix
ON products(description(100));FULLTEXT Indexes:
-- Full-text search on text columns
CREATE FULLTEXT INDEX idx_products_search
ON products(name, description);
-- Query using MATCH...AGAINST
SELECT * FROM products
WHERE MATCH(name, description) AGAINST('laptop' IN BOOLEAN MODE);Spatial Indexes:
-- For geographic data
CREATE SPATIAL INDEX idx_locations
ON locations(geom);PostgreSQL Index Types
PostgreSQL Index Types
B-tree Indexes (Default):
-- Standard equality and range queries
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_orders_created_at ON orders(created_at DESC);
-- Composite indexes for multi-column queries
CREATE INDEX idx_orders_user_status
ON orders(user_id, status)
WHERE cancelled_at IS NULL;Hash Indexes:
-- Exact match queries only
CREATE INDEX idx_product_sku USING hash ON products(sku);
-- Good for equality lookups on large text fields
CREATE INDEX idx_uuid_hash USING hash ON sessions(session_id);BRIN Indexes (Block Range):
-- For large tables with monotonically increasing columns
CREATE INDEX idx_events_timestamp USING brin ON events(created_at)
WITH (pages_per_range = 128);
-- Excellent for time-series data
CREATE INDEX idx_logs_timestamp USING brin
ON application_logs(log_timestamp);GiST & GIN Indexes:
-- GiST for spatial data and complex types
CREATE INDEX idx_locations_geom USING gist ON locations(geom);
-- GIN for JSONB and array columns
CREATE INDEX idx_products_metadata USING gin ON products(metadata);
CREATE INDEX idx_user_tags USING gin ON users(tags);Single Column Indexes
Single Column Indexes
PostgreSQL:
-- Filtered index for active records only
CREATE INDEX idx_users_active
ON users(created_at)
WHERE deleted_at IS NULL;
-- Descending order for LIMIT queries
CREATE INDEX idx_posts_published DESC
ON posts(published_at DESC)
WHERE status = 'published';MySQL:
-- Simple equality lookup
CREATE INDEX idx_users_verified ON users(email_verified);
-- Range queries on numeric columns
CREATE INDEX idx_products_price ON products(price);Composite Indexes
PostgreSQL - Optimal Ordering:
-- Order: equality columns, then range, then sort
-- Query: WHERE user_id = X AND created_at > Y ORDER BY id
CREATE INDEX idx_optimal_composite
ON orders(user_id, created_at, id);
-- Covering index to eliminate table access
CREATE INDEX idx_covering_orders
ON orders(user_id, status, created_at)
INCLUDE (total, currency);MySQL - Leftmost Prefix:
-- MySQL uses leftmost prefix matching
-- Can be used by: (user_id), (user_id, status), (user_id, status, created_at)
CREATE INDEX idx_users_complex
ON users(user_id, status, created_at);
-- For queries: user_id + status + created_at
SELECT * FROM orders
WHERE user_id = 1 AND status = 'completed' AND created_at > '2024-01-01';Partial/Filtered Indexes
PostgreSQL:
-- Only index active products
CREATE INDEX idx_active_products
ON products(category_id)
WHERE active = true;
-- Reduce index size and improve performance
CREATE INDEX idx_not_cancelled_orders
ON orders(user_id, created_at)
WHERE status != 'cancelled';
-- Complex filter conditions
CREATE INDEX idx_vip_orders
ON orders(total DESC)
WHERE total > 10000 AND customer_type = 'vip';Expression Indexes
PostgreSQL:
-- Index on computed values
CREATE INDEX idx_users_email_lower
ON users(LOWER(email));
-- Enable case-insensitive searches
SELECT * FROM users WHERE LOWER(email) = 'john@example.com';
-- Date extraction indexes
CREATE INDEX idx_orders_year
ON orders(EXTRACT(YEAR FROM created_at));#!/bin/bash
# validate-schema.sh - Validate database schema
# Usage: ./validate-schema.sh <schema_file>
set -euo pipefail
SCHEMA_FILE="${{1:?Usage: $0 <schema_file>}}"
echo "Validating schema: $SCHEMA_FILE"
# TODO: Add schema validation
# - Check SQL syntax
# - Verify foreign key references
# - Check index definitions
# - Validate naming conventions
# - Check for missing constraints
echo "Schema validation complete."
-- Migration: [description]
-- Created: [date]
-- TODO: Customize for your migration framework
BEGIN;
-- Up migration
-- TODO: Add schema changes
-- CREATE TABLE IF NOT EXISTS ...
-- ALTER TABLE ...
-- Down migration (rollback)
-- TODO: Add rollback statements
-- DROP TABLE IF EXISTS ...
COMMIT;
Related skills
How it compares
Pick database-indexing-strategy over generic SQL skills when the task is index-type selection and CREATE INDEX design rather than general query rewriting.
FAQ
Which databases does database-indexing-strategy support?
database-indexing-strategy targets PostgreSQL and MySQL. It documents B-tree, Hash, GiST, and BRIN types plus composite, partial, filtered, and expression indexes with SQL examples and three reference guides.
When should you add a partial index?
database-indexing-strategy recommends partial indexes when queries consistently filter on a subset of rows—e.g., active orders with WHERE cancelled_at IS NULL—so the index stays smaller and cheaper to maintain than a full-table index.