Back to Blog
Software

Business Systems That Slow Down as Data Grows: Diagnosing Database Query Performance and Fixing It with Indexes and Partitioning

Why business systems that were fast at launch slow down as data accumulates, and how to fix them in practice, from slow query diagnosis to composite indexes, keyset pagination, and date-based partitioning.

POLYGLOTSOFT Tech Team2026-11-257 min read18
Database PerformanceQuery TuningIndexingPartitioningEnterprise System

Why Screens Slow Down in Year Three

An order lookup screen that loaded in under a second at launch often takes more than ten seconds three years later. The code hasn't changed; the data volume has. Test data during development usually runs from a few thousand to a few tens of thousands of rows. An order table that grows by 10,000 rows a day, on the other hand, holds roughly 11 million rows after three years. A query that could scan everything unnoticed at 10,000 rows becomes a bottleneck at 1,000 times that size.

"Just add more servers" rarely fixes this. A query that can't use an index and reads the whole table still has to read the same amount of data on twice the CPU. If the real cause is lock contention or disk I/O, scaling up helps even less. Costs rise every month, and because the data keeps growing, the same problem comes back soon.

Find the Slow Queries First

Measure before you fix. In MySQL, enable `slow_query_log` and lower `long_query_time` (10 seconds by default) to around one second. In PostgreSQL, set a threshold with `log_min_duration_statement` and use the `pg_stat_statements` extension to see cumulative execution time and call counts per query.

Then check the execution plan with `EXPLAIN ANALYZE`. Look at three things:

  • Full scans: `Seq Scan` (PostgreSQL) or `type: ALL` (MySQL) on large tables
  • Estimated vs. actual rows: a large gap often means stale statistics
  • Sorts and temp tables: recurring `Using filesort` or `Using temporary`
  • Prioritize by total time spent. A query that takes 0.05 seconds but runs 200,000 times a day consumes about 2.8 hours daily; one that takes 8 seconds but runs 10 times a day consumes 80 seconds. Users may complain about the second, but fixing the first does far more to reduce server load. Track the two lists separately.

    Index Design Principles

    Build composite indexes around the actual filter conditions and sort order. For "recent orders by customer," `(customer_id, order_date)` is the right order. Equality columns go first; range and sort columns go last. Reverse the order and the index loses most of its value, even though it exists.

    Indexes aren't free, though. Every INSERT and UPDATE must maintain every index, so a table with more than ten indexes writes noticeably slower. For write-heavy tables such as IoT sensor ingestion or bulk imports, keep indexes to a minimum and use usage statistics to drop the ones nobody reads.

    Problems Built into Screens and Queries

    Many problems can't be solved with indexes alone.

  • N+1 queries: fetching a 50-row list and then querying details row by row sends 51 queries. This is common with ORM lazy loading; switch to a JOIN or a batched lookup
  • Total row counts: running `COUNT(*)` on a 10-million-row table to show "N results" can take seconds on its own. Consider estimates, or simply show whether a next page exists
  • Deep pagination: `OFFSET 500000` reads and discards 500,000 rows. Keyset pagination, which queries rows after the last key seen, is the alternative
  • Surprisingly, the single most effective change is often limiting the default date range on list screens. Default to "last three months" and let users pick earlier ranges explicitly, and most queries will touch only a fraction of the data.

    Managing Large Tables

    Once tables reach tens of millions of rows, consider date-based partitioning. With monthly partitions, queries that filter by date read only the relevant partitions, and data past its retention period can be detached or dropped as a whole partition instead of through a massive DELETE. History data you're legally required to keep can move to a separate archive table or store, keeping operational tables lean.

    Don't run heavy aggregations like month-end reports directly against the production database. Prepare daily summary tables in advance, or run reports against a read-only replica so they don't compete with day-to-day screens.

    Make Performance Part of Ongoing Maintenance

    Performance work is never one-and-done, because data keeps accumulating. Regular reviews should track:

  • Monthly row counts and storage growth for key tables
  • Slow query counts and the change in total time for the top 10 queries
  • Response times for key screens, measured at p95 rather than the average
  • With growth trends in hand, you can predict how large a table will be in six months and which screen will slow down first, and act before users notice.

    POLYGLOTSOFT's Pro SM plan (about $300/month) covers incident response plus performance monitoring and optimization, with a dedicated PM handling database tuning. From slow query analysis to index redesign and partitioning, we improve your live system step by step without taking it offline. If you run a business system that gets slower as data grows, get in touch with us today.