89 | | |
| 89 | - Complex invoice analytics: (/agencyos/sql/04_complex_invoice_analytics_index.sql |
| 90 | - `idx_invoice_complex_analytics`: Multi-column index for tenant-specific analytics with status, currency, monthly grouping, and amount filtering |
| 91 | - `idx_invoice_overdue_analysis`: Optimized for overdue invoice queries with tenant, status, due date, and amount sorting |
| 92 | - `idx_invoice_financial_reporting`: Quarterly financial reporting with currency, status, and amount categorization |
| 93 | - `idx_invoice_number_search`: Case-insensitive invoice number search with tenant isolation |
| 94 | - `idx_client_invoice_performance`: Client performance metrics with status-based priority ordering |
| 95 | - `idx_invoice_temporal_analytics`: Time-series analysis with year, month, and day-of-week grouping |
| 115 | Using `EXPLAIN` on complex invoice analytics |
| 116 | |
| 117 | - Multi-tenant analytics query: |
| 118 | {{{ |
| 119 | EXPLAIN ANALYZE |
| 120 | SELECT |
| 121 | invoice_status, |
| 122 | invoice_currency, |
| 123 | DATE_TRUNC('month', invoice_issue_date) as month, |
| 124 | SUM(invoice_total_amount) as total_amount, |
| 125 | COUNT(*) as invoice_count |
| 126 | FROM "Invoice" |
| 127 | WHERE tenant_id = 'tenant_123' |
| 128 | AND invoice_status IN ('PENDING', 'PROCESSING', 'OVERDUE', 'PAID') |
| 129 | AND invoice_issue_date >= '2024-01-01' |
| 130 | AND invoice_currency = 'EUR' |
| 131 | GROUP BY invoice_status, invoice_currency, DATE_TRUNC('month', invoice_issue_date) |
| 132 | ORDER BY month DESC, total_amount DESC; |
| 133 | }}} |
| 134 | |
| 135 | - Result with complex index: |
| 136 | {{{ |
| 137 | Index Only Scan using idx_invoice_complex_analytics on "Invoice" (cost=0.14..12.49 rows=11 width=24) |
| 138 | Index Cond: ((tenant_id = 'tenant_123') AND (invoice_currency = 'EUR'::"Currency") AND (invoice_issue_date >= '2024-01-01 00:00:00'::timestamp without time zone)) |
| 139 | Heap Fetches: 11 |
| 140 | }}} |
| 141 | |