Skip to main content

Optimize Aiven for PostgreSQL® slow queries

Optimize slow queries to ensure maximum service performance. Use Aiven's AI capabilities or pg_stat_statements to identify optimization opportunities.

Artificial intelligence recommendations Early availabilty

Use Aiven's artificial intelligence (AI) capabilities to suggest optimizations to your databases and queries.

Aiven considers various aspects to suggest optimization, for example query structure, table size, existing indexes and their cardinality, column types and sizes, the connections between the tables and columns in the query.

Display optimization recommendations

To optimize a query:

  1. In the Aiven Console, open your Aiven for PostgreSQL service.

  2. Click AI insights.

  3. For the query of your choice, click Optimize.

  4. In the Query optimization report window, see the optimization suggestion and apply the suggestion by running the provided SQL queries.

Manual optimization

Aiven for PostgreSQL allows you to identify slow queries using the pg_stat_statements view. To improve slow running queries, use one of the manual optimization techniques.

It is worth knowing that many database indexes on a table can also cause problems for write performance due to the overhead of maintaining them.

Handle an increase in database connections

When your application code scales horizontally to accommodate high loads, you might find that you inadvertently reach the connection limits for your plan. Each connection in PostgreSQL runs in a separate process, and this makes them more expensive (compared to threads, for example) in terms of inter-process communication and memory usage, since each connection consumes a certain amount of RAM.

In such cases, you can use the connection pooling, based on PgBouncer, to handle an increase in database connections. You can add and configure the connection pooling for your service in the Connection pools view in Aiven Console.

Move read-only queries to standby nodes

If your Aiven for PostgreSQL® service is running a Business or Premium plan, you have one or more standby nodes available in a high availability setup in the same cloud region.

To reduce the effect of slow queries on the primary node, you can redirect read-only queries to the additional read-only nodes by directly connecting via the read-only replica URL.

Move read-only queries to a remote read-only replica

You can also create a remote read-only replica service in the same or a different cloud or region that you can use to reduce the query load on the primary service for read-only queries.