Database optimisation - indexes, query planner and cache

As an application grows, the database often becomes its weakest link. Queries that were initially fast begin to take seconds, and the server’s CPU reaches 100% utilisation with every report. Understanding this, how to optimise a MySQL or PostgreSQL database, it’s not just a matter of adding a few indexes – it’s a process of in-depth analysis of how the database engine interprets your SQL code and how it manages system resources.

Step 1: Understanding the Query Planner

Before you start optimising, you need to know what’s going on „under the bonnet”. Every database has a component called the Query Planner (or Optimizer), which decides the quickest way to retrieve data.

A key tool in your arsenal is the command EXPLAIN ANALYZE (in PostgreSQL) or EXPLAIN (in MySQL). This allows you to view the query execution plan. Your main aim is to eliminate operations such as Sequential Scan (searching the entire table row by row) in favour of Index Scan (using the index).

When analysing the plan, pay attention to the „cost” and duration of individual nodes. If you see that the database is performing a costly sort on disk rather than in memory, this is a sign that the appropriate index is missing or that the working memory configuration (work_mem) is too low. You can find out more about the basics of writing queries here: SQL – queries from the basics to advanced.

Indexes – the surgeon’s precision tools

Indexes are the most powerful optimisation method, but using them incorrectly can slow down write operations (INSERT/UPDATE). Knowing this, how to optimise a MySQL or PostgreSQL database, requires an understanding of various types of structures:

  • B-tree: The standard index for most queries (equality, ranges, sorting).
  • Hash: Very fast, but only for simple comparisons using operators =.
  • GIN and GiST: Essential in PostgreSQL for full-text search or for JSONB data and arrays.

Advanced indexing strategies:

  1. Composite Indexes: If you often filter data by two columns at the same time (e.g. status i created_at), a composite index will be significantly more efficient than two separate ones.
  2. Partial Indexes: They only index those records that meet the condition (e.g. only active users). This makes the index smaller and faster.
  3. Indexes covered: An index that contains all the columns required by the query, meaning the database does not need to look in the actual table (known as an ‘Index-Only Scan’).

You will find detailed technical guidelines in the document PostgreSQL – index documentation.

Cache and connection pool – reducing the load on the engine

Even the best-optimised database has its limits. That is why optimisation must go beyond the SQL engine itself.

Connection pooling: Opening a new connection to the database with every HTTP request is extremely costly. Tools such as PgBouncer For PostgreSQL, they act as a proxy, maintaining a fixed pool of open connections, which drastically reduces latency. This is crucial when building high-performance Building an API with a database – Node.js and PostgreSQL.

Cache layer (Redis): The fastest query is the one you don’t have to run. Storing the results of frequent, resource-intensive queries in the database Redis (RAM) allows response times to be reduced from hundreds of milliseconds to microseconds. Remember, however, the guidelines cache invalidation – you need to know when the data in the cache becomes out of date and needs refreshing.

Database maintenance: VACUUM and ANALYZE

In PostgreSQL, writing and deleting data do not free up disk space immediately (the MVCC mechanism). Over time, the table becomes „bloated”, which slows down queries. Regularly performing operations VACUUM and updating the planner’s statistics using ANALYSIS It is essential for maintaining high performance. It’s worth taking a look at the website Use The Index, Luke, which is the bible of SQL optimisation for professionals.



Productivity is a process, not a one-off change

Knowing this, how to optimise a MySQL or PostgreSQL database, it is a process of continuous monitoring and adaptation. Changing user behaviour patterns require periodic audits of indexes and revisions to query plans. Remember: every optimisation should be preceded by measurements and followed by a review of the results.

At 4ADStudio, we design databases with scalability in mind. We help our clients identify bottlenecks and implement solutions that ensure their applications run at lightning speed, regardless of the volume of data.

Is your application struggling under heavy load? Are SQL queries taking ages, and are your server costs rising? Get in touch with us – we’ll carry out a performance audit of your database and implement optimisations that will breathe new life into your business!

Leave a Comment

Your email address will not be published. Required fields are marked *

Write to us

You want to improve
your business?

Bartłomiej Biedrończyk


    CALL ME
    +
    Call me!
    4AD
    Privacy Overview

    This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.