With 400 million records, there are several strategies to improve query performance on your nem_dispatch table:
Indexing Strategies
Current composite primary key is good, but consider additional indexes based on your query patterns:
- If you frequently query by
duid alone: CREATE INDEX idx_nem_dispatch_duid ON nem_dispatch (duid);
- If you query by date ranges:
CREATE INDEX idx_nem_dispatch_settlement_date ON nem_dispatch (settlement_date);
- For queries filtering on
lastchanged: CREATE INDEX idx_nem_dispatch_lastchanged ON nem_dispatch (lastchanged);
Column Optimization
Reducing duid from 12 to 8 characters would help slightly by:
- Reducing row size (saves 4 bytes per row = ~1.6GB total)
- Making indexes smaller and faster
- Improving cache efficiency
- The impact is modest but worthwhile for 400M records
Major Performance Improvements
Partitioning (most impactful for this size):
-- Partition by date range (monthly/yearly)
CREATE TABLE nem_dispatch_partitioned (
settlement_date timestamp,
duid character varying(8),
scada_value double precision,
lastchanged timestamp
) PARTITION BY RANGE (settlement_date);
Other optimizations:
- VACUUM and ANALYZE regularly to maintain statistics
- Increase work_mem for complex queries
- Consider columnar storage (like Citus) for analytical workloads
- Archive old data if not frequently accessed
- Use connection pooling (pgBouncer)
Quick Wins
- Reduce
duid to 8 characters
- Add targeted indexes for your common query patterns
- Implement date-based partitioning
- Regular maintenance (VACUUM ANALYZE)
What types of queries are you running most frequently? This would help prioritize which indexes to create first.