[Solved] Which INDEX type should I use?
Looking to automatically optimize YOUR SQL query? Start for free.

EverSQL Database Performance Knowledge Base

Which INDEX type should I use?

I have a huge dataset (customer,timestamp,consumption). I need to use an index to improve the performance of various queries, but I cannot seem to create any index that will boost up the performance of this specific query:

SELECT customer, AVG(consumption) 
FROM alicante_1y 
GROUP BY customer;

From what I have been reading, there is no easy/direct way to improve the performance of AVG function...

Any help appreciated. Thank you in advance.

explain (analyze, verbose) output:

HashAggregate  (cost=194302.67..194315.09 rows=993 width=16) (actual time=6847.581..6848.630 rows=994 loops=1)  
Output: customer, avg(consumption)
Group Key: alicante_1y.customer
->  Seq Scan on public.alicante_1y  (cost=0.00..150840.45 rows=8692445 width=16) (actual time=0.175..1829.867 rows=8692445 loops=1)
Output: customer, t, consumption
Planning time: 0.633 ms
Execution time: 6849.036 ms

How to optimize this SQL query?

The following recommendations will help you in your SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  2. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
Optimal indexes for this query:
ALTER TABLE `alicante_1y` ADD INDEX `alicante_1y_idx_customer` (`customer`);
The optimized query:

Related Articles

* original question posted on StackOverflow here.