All Posts

How to Optimize SQL Queries With the Help of ChatGPT

Learn how to use ChatGPT to find possible performance optimizations for your SQL queries.

July 31, 2023

TLDR: To optimize a SQL query with ChatGPT, give ChatGPT your problematic query, your database schema, information about existing indexes, and optionally a query plan. Then ask it to optimize the query for you.

In the world of databases, efficiency and performance are critical to ensure a seamless user experience and maintain overall system health. One of the most essential ways to ensure efficiency is optimizing your SQL queries. In this blog post, we’ll explore how ChatGPT can assist with that.

Common SQL Performance Bottlenecks

Before we get into how ChatGPT can help, let’s take a moment to understand some common performance bottlenecks in SQL.


One of the most common issues is missing indexes. Indexes are critical for fast data retrieval. Without the proper indexes, the database must perform a full table scan to find the requested data, significantly slowing down query execution.

What is an Index?

Database indexes work similarly to an index in a book. Without an index, finding a particular word or topic in a book would require you to go through each page, one by one. But with an index, you can quickly locate the pages where the word or topic appears.

In SQL databases, an index allows the database engine to find a row or set of rows much faster without scanning the entire table. It's especially beneficial when dealing with large tables and complex queries.

On the other end of the spectrum, unnecessary indexes can also hinder performance. Every additional index adds overhead to data modification operations (INSERT, UPDATE, DELETE), as the database needs to update the indexes in addition to the table data.

Correlated Subqueries

Another common bottleneck involves correlated subqueries. These are queries where the inner query depends on the outer query’s result, forcing the database to execute the subquery once for each row processed by the outer query. This can dramatically increase the execution time, particularly for large data sets.

Suppose we have two tables: employees and salaries. We want to find all employees whose salary is higher than the average salary in their respective departments.

A correlated subquery could look something like this:

FROM employees e
WHERE e.salary > (
  SELECT AVG(s.salary)
  FROM salaries s
  WHERE s.department_id = e.department_id

Here, the database needs to compute the average salary for each department for each row in the employees table, which could be computationally expensive. (Note: This is expensive in most database systems but there are exceptions. For example, DuckDB automatically decorrelates subqueries to improve performance.)

An improved counterpart using a JOIN operation could look like this:

FROM employees e
  SELECT department, AVG(salary) AS avg_salary
  FROM salaries
  GROUP BY department
) s ON e.department_id = s.department_id
WHERE e.salary > s.avg_salary;

In this query, we first calculate the average salary for each department and then join this with the employees table. This approach will typically be faster because the average salary for each department is calculated just once, not for every row in the employees table.

How ChatGPT Can Help

Now we understand the common bottlenecks. But how can we identify which of those causes a given query to be slow?

In certain instances, identifying the root of the issue might be straightforward and observable at first glance. However, as queries get more complex, it becomes much harder to spot where the problem lies. That’s where ChatGPT can help!

For instance, it could point out missing in your queries, or suggest ways to rewrite correlated subqueries in a non-correlated way, or propose a more optimal join strategy. It can even go beyond just the SQL queries and offer suggestions on database schema design and index creation. It’s like having a SQL expert by your side, accessible 24/7.

Give ChatGPT Details About Your Query and DB

Sometimes it might be enough to provide just your problematic query, and ChatGPT will already suggest useful improvements.

But you’ll get better feedback by providing additional information besides your query - the schema of the relevant tables, details about existing indexes, and optionally a query plan.

For example with PostgreSQL, use the \d table_name command in psql to view the schema and indexes of a given table.

The query plan can be obtained using the EXPLAIN or EXPLAIN ANALYZE command before your SQL query. This will give you details on how the database engine plans to execute the query, providing insights on the chosen indexes, join strategies, and more.

Example Prompt

Once you’ve gathered all this information, you can input it to ChatGPT. The prompt might look like this:

The following PostgreSQL query is slow. How can I optimize it? SELECT FROM employees e WHERE e.salary > (   SELECT AVG(s.salary)   FROM salaries s   WHERE s.department_id = e.department_id );

These are the relevant tables and indexes: Table “public.employees” Column | Type | Collation | Nullable | Default ---------------+----------+-----------+----------+---------------------------------- id | integer | | not null | nextval(‘employes_id_seq’::regclass) name | text | | | department_id | smallint | | | Indexes: “employes_pkey” PRIMARY KEY, btree (id)

Table “public.salaries” Column | Type | Collation | Nullable | Default ---------------+----------+-----------+----------+---------------------------------- id | integer | | not null | nextval(‘salaries_id_seq’::regclass) salary | bigint | | | department_id | smallint | | | Indexes: “salaries_pkey” PRIMARY KEY, btree (id)


Optimizing SQL queries is a critical aspect of database management, but it can often be complex and time-consuming. With tools like ChatGPT, this process can be made significantly easier. By providing ChatGPT with your problematic query, database schema, and index information, it can suggest potential performance improvements that might have been missed otherwise.

Get started with DB Pilot

DB Pilot is a Database GUI client and SQL editor for PostgreSQL, MySQL, SQLite, DuckDB & more.