How We Cut PostgreSQL Query Time by 65% on a 10M+ Record Insurance Database

Short answer: We made a European insurer's PostgreSQL workload 65% faster by measuring first, then rebuilding indexes around the queries the business actually ran, rewriting the heaviest joins, caching repeated reporting work, and moving department-specific queries into a modular layer that could change without touching production tables. Four Shrijan Tech engineers did this while embedded in the client's own data team, at roughly 60% of the projected cost of hiring in-house.
This article walks through that playbook step by step. The client is anonymised under NDA, but the numbers are real and come from our insurance database case study.
What was the problem?
The insurer held more than 10 million records across several PostgreSQL schemas: policyholders, claims and medical history. Four very different groups depended on that data: underwriting, claims, HR and finance. Each wanted its own views, its own reports and its own changes, usually urgently.
Three things had gone wrong at once:
- Slow reporting. Large queries with unoptimised joins took minutes, so reports ran late and teams stopped trusting the numbers on screen.
- Constant change requests. Every new department request meant editing core SQL, which was risky and slow.
- A staffing model that didn't flex. A full in-house data engineering team was expensive and hard to scale up or down as demand changed.
Step 1: Measure before you touch anything
Performance work fails when teams tune the queries they think are slow. The first job is to find the queries that consume the most total time, which is frequency multiplied by duration. On PostgreSQL, the pg_stat_statements extension gives you exactly that list:
SELECT query, calls, total_exec_time, mean_exec_time, rows
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 20;
In almost every database we have worked on, a small group of queries accounts for most of the load. Then run each of those through EXPLAIN (ANALYZE, BUFFERS) to see where time actually goes: sequential scans on big tables, nested loops over millions of rows, sorts that spill to disk.
Step 2: Index for the questions the business asks
Indexes are the single biggest lever, but only when they match real access patterns. On a claims-heavy workload that usually means:
- Composite indexes in the same column order as the filter, for example
(policy_id, claim_date)for "claims for this policy in this period". - Partial indexes for hot subsets, such as open claims only, which keeps the index small and fast.
- Covering indexes (
INCLUDEcolumns) so frequent report queries can be answered from the index without reading the table. - Removing unused indexes, which slow down every insert and update for no benefit.
-- Illustrative: a partial, covering index for an "open claims by policy" report
CREATE INDEX CONCURRENTLY idx_claims_open_policy
ON claims (policy_id, claim_date)
INCLUDE (amount, status)
WHERE status = 'open';
Use CREATE INDEX CONCURRENTLY on production systems so the table stays writable while the index builds.
Step 3: Rewrite the joins that do the most damage
Most slow reports were not slow because of one bad table. They were slow because of how tables were combined. Typical fixes:
- Filter early: push
WHEREconditions into subqueries or CTEs so joins operate on thousands of rows, not millions. - Replace correlated subqueries that run once per row with a single join or a window function.
- Make join keys the same data type on both sides, so PostgreSQL can use the index instead of casting every row.
- Keep table statistics fresh (
ANALYZE, autovacuum tuning) so the planner picks the right join strategy.
Our engineers also tuned the stored procedures behind recurring jobs, because a procedure called thousands of times a day magnifies every inefficiency inside it.
Step 4: Stop recomputing the same answers
Dashboards ask the same questions over and over: claims this month, premiums by region, open cases by department. Computing those from raw rows on every refresh is wasteful. Caching and pre-aggregating those results, for example with summary tables or materialized views refreshed on a schedule, turns minute-long reports into second-long ones.
That is what made the next step possible: live Power BI dashboards for claims, policies and medical data. Leadership could watch KPIs in near real time instead of waiting for overnight reports.
Step 5: Make change requests cheap and safe
The constant stream of departmental requests was as big a problem as raw speed. Our team built a modular query layer: department-specific views and parameterised queries sitting on top of the core schema. A new report for finance became a new module, not an edit to SQL that claims also depended on. Changes shipped faster and without disrupting live operations.
Step 6: Automate data integrity
When several departments read and write the same records, inconsistencies creep in. We added automated validation scripts and triggers that check consistency and keep data synchronised between departments. Fast reports are worthless if the numbers are wrong.
Why did staff augmentation work for this project?
Database tuning is not a one-off project. Data grows, new reports arrive, and query patterns shift. The insurer needed specialists who would stay and own the system, without the cost of a large in-house hire.
So Shrijan Tech provided four full-time database and BI engineers who worked directly inside the client's IT and analytics teams: the same tools, the same meetings, the same priorities. That cut the cost of the function by roughly 60% compared with the client's projected in-house cost, and the team could be resized as needs changed. We cover the economics in detail in Staff Augmentation vs. In-House Hiring.
What were the results?
- 65% faster query execution. Heavy queries went from minutes to seconds.
- 10M+ records handled smoothly across all departments.
- Real-time reporting through Power BI dashboards on live data.
- About 60% lower cost than the projected in-house team.
- Better collaboration between data and business teams, with fewer bottlenecks.
"Shrijan Tech became an integral part of our data team. Their expertise in database optimization and BI reporting has been invaluable. Our systems run faster, our reports are real-time, and their engineers feel like part of our own organization." (Data Operations Manager, Insurance Enterprise)
A PostgreSQL performance checklist you can use today
- Enable
pg_stat_statementsand rank queries by total execution time. - Run
EXPLAIN (ANALYZE, BUFFERS)on the top 10 and note every sequential scan on a large table. - Add composite, partial or covering indexes that match real filters. Drop indexes nobody uses.
- Filter before joining, and align join key data types.
- Check autovacuum and statistics on your largest tables.
- Pre-aggregate dashboard queries instead of recomputing from raw rows.
- Separate department-specific logic into views or modules, away from core tables.
- Automate data-consistency checks.
- Re-measure after every change, and keep the before and after numbers.
Frequently asked questions
How much faster can PostgreSQL get with tuning alone?
It depends on how far the current setup is from its access patterns. In this insurance project, indexing, join rewrites and caching cut query execution time by 65% without new hardware. Databases that have never been tuned often see larger gains on their worst queries.
Do I need to migrate off PostgreSQL to handle 10 million+ rows?
No. Ten million rows is well within what PostgreSQL handles comfortably when indexes, queries and maintenance are set up properly. Most "PostgreSQL is too slow" problems are design and tuning problems, not database limits.
Can Power BI report on PostgreSQL in real time?
Yes. Power BI connects to PostgreSQL directly. The key is to point dashboards at pre-aggregated or well-indexed queries rather than raw tables, so refreshes take seconds instead of minutes.
Who at Shrijan Tech does this kind of work?
Database and BI engineers from our AI & Data Solutions practice. They can be engaged on a fixed-scope project or embedded in your team through staff augmentation, as the insurer did.
How do I start a database performance engagement?
Share your slowest reports or a pg_stat_statements export through our contact form. An engineer will review it and reply within 24 hours with where the biggest wins are likely to be.



