R
R
ruslanbat2014-01-16 11:22:22
PostgreSQL
ruslanbat, 2014-01-16 11:22:22

How to optimize a PostgreSQL query?

Request

INSERT INTO client_4_level_group (client_id, level1, level2, level3, level4)
    SELECT max(client_id), level1, level2, level3, 388 FROM client_4_level_group   group by client_id, level1, level2, level3 
    HAVING COUNT(client_id) = (select count(*) from nomenclature_brand where nomenclature_brand_id != 388)

+---------------------------------------------------------------------------------------------------------------------------------------------+
| QUERY PLAN                                                                                                                                  |
+---------------------------------------------------------------------------------------------------------------------------------------------+
| Subquery Scan "*SELECT*"  (cost=61429.69..64038.44 rows=274606 width=20) (actual time=1314.545..1331.383 rows=20797 loops=1)                |
|   ->  HashAggregate  (cost=61429.69..63763.84 rows=274606 width=16) (actual time=1314.544..1327.734 rows=20797 loops=1)                     |
|         Filter: (count(public.client_4_level_group.client_id) = $0)                                                                         |
|         InitPlan 1 (returns $0)                                                                                                             |
|           ->  Aggregate  (cost=1.77..1.77 rows=1 width=0) (actual time=0.040..0.040 rows=1 loops=1)                                         |
|                 ->  Seq Scan on nomenclature_brand  (cost=0.00..1.45 rows=128 width=0) (actual time=0.009..0.025 rows=128 loops=1)          |
|                       Filter: (nomenclature_brand_id <> 388)                                                                                |
|         ->  Seq Scan on client_4_level_group  (cost=0.00..20237.06 rows=2746057 width=16) (actual time=0.012..253.424 rows=2662848 loops=1) |
| Total runtime: 1577.023 ms                                                                                                                  |
+---------------------------------------------------------------------------------------------------------------------------------------------+
9 rows in set

in the table client_4_level_group 2978140 rows
need to speed up the execution of the request for 2 days I've been suffering I can't figure out where to dig

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
SiDChik, 2014-01-16
@SiDChik

store max(client_id) as a separate field so you don't have to aggregate each group?

W
whats, 2014-05-08
@whats

SELECT max(client_id), level1, level2, level3, 388 FROM client_4_level_group   group by client_id, level1, level2, level3

By this request you form the list on unique fields. You will have in this list all lines in which all these fields will differ. What's the point in aggregating something in unique rows when the values ​​in the group will always be the same? The result will be the same
SELECT client_id, level1, level2, level3, 388 FROM client_4_level_group   group by client_id, level1, level2, level3

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question