select version();
version
----------------------------------------------------------------------------------------------------
PostgreSQL 10devel on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-8) 6.3.0 20170221, 64-bit
(1 ligne)
create table ventes_agg(dtv timestamp, mtv smallint);
CREATE TABLE
Temps : 43,663 ms
WITH serie(i) AS (SELECT generate_series(1,5256000 ,1))
INSERT INTO ventes_agg (SELECT current_timestamp - i * '1 minute'::interval, trunc(random()*100) from serie);
INSERT 0 5256000
Time: 11945,540 ms (00:11,946)
select extract(year from dtv), count(*) from ventes_agg group by extract(year from dtv) order by 1;
date_part | count
-----------+--------
2007 | 429559
2008 | 527040
2009 | 525600
2010 | 525600
2011 | 525600
2012 | 527040
2013 | 525600
2014 | 525600
2015 | 525600
2016 | 527040
2017 | 91721
(11 lignes)
Time: 2833,996 ms (00:02,834)
analyze ventes_agg;
ANALYZE
Temps : 411,778 ms
postgres=# \dt+ ventes_agg
Liste des relations
Schéma | Nom | Type | Propriétaire | Taille | Description
--------+------------+-------+--------------+--------+-------------
public | ventes_agg | table | postgres | 222 MB |
(1 ligne)
set max_parallel_workers_per_gather=0;
SET
explain analyze select avg(mtv) from ventes_agg where dtv between to_date('01/01/2015', 'DD/MM/YYYY') and to_date('01/01/2016','DD/MM/YYYY');
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=134830.64..134830.65 rows=1 width=32) (actual time=2276.085..2276.086 rows=1 loops=1)
-> Seq Scan on ventes_agg (cost=0.00..133531.00 rows=519855 width=2) (actual time=391.432..2219.805 rows=525600 loops=1)
Filter: ((dtv >= to_date('01/01/2015'::text, 'DD/MM/YYYY'::text)) AND (dtv <= to_date('01/01/2016'::text, 'DD/MM/YYYY'::text)))
Rows Removed by Filter: 4730400
Planning time: 0.056 ms
Execution time: 2276.109 ms
...
Planning time: 0.064 ms
Execution time: 2285.435 ms
...
Planning time: 0.067 ms
Execution time: 2303.971 ms
set max_parallel_workers_per_gather=3;
SET
explain analyze select avg(mtv) from ventes_agg where dtv between to_date('01/01/2015', 'DD/MM/YYYY') and to_date('01/01/2016','DD/MM/YYYY');
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------------------------------------
Finalize Aggregate (cost=63740.23..63740.24 rows=1 width=32) (actual time=624.474..624.474 rows=1 loops=1)
-> Gather (cost=63739.91..63740.22 rows=3 width=32) (actual time=624.426..624.466 rows=4 loops=1)
Workers Planned: 3
Workers Launched: 3
-> Partial Aggregate (cost=62739.91..62739.92 rows=1 width=32) (actual time=619.384..619.385 rows=1 loops=4)
-> Parallel Seq Scan on ventes_agg (cost=0.00..62320.68 rows=167695 width=2) (actual time=114.817..605.783 rows=131400 loops=4)
Filter: ((dtv >= to_date('01/01/2015'::text, 'DD/MM/YYYY'::text)) AND (dtv <= to_date('01/01/2016'::text, 'DD/MM/YYYY'::text)))
Rows Removed by Filter: 1182600
Planning time: 0.073 ms
Execution time: 626.995 ms
...
Planning time: 0.180 ms
Execution time: 640.018 ms
...
Planning time: 0.074 ms
Execution time: 634.993 ms
create index ventes_agg_i1 on ventes_agg(dtv);
CREATE INDEX
Time: 6612,634 ms (00:06,613)
\di+ ventes_agg_i1
Liste des relations
Schéma | Nom | Type | Propriétaire | Table | Taille | Description
--------+---------------+-------+--------------+------------+--------+-------------
public | ventes_agg_i1 | index | postgres | ventes_agg | 113 MB |
(1 ligne)
select correlation from pg_stats where tablename='ventes_agg' and attname = 'dtv';
correlation
-------------
-1
(1 ligne)
explain analyze select avg(mtv) from ventes_agg where dtv between to_date('01/01/2015', 'DD/MM/YYYY') and to_date('01/01/2016','DD/MM/YYYY');
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=20656.61..20656.62 rows=1 width=32) (actual time=163.512..163.512 rows=1 loops=1)
-> Index Scan using ventes_agg_i1 on ventes_agg (cost=0.44..19328.48 rows=531252 width=2) (actual time=0.030..102.133 rows=525600 loops=1)
Index Cond: ((dtv >= to_date('01/01/2015'::text, 'DD/MM/YYYY'::text)) AND (dtv <= to_date('01/01/2016'::text, 'DD/MM/YYYY'::text)))
Planning time: 0.156 ms
Execution time: 163.544 ms
...
Planning time: 0.084 ms
Execution time: 167.483 ms
...
Planning time: 0.087 ms
Execution time: 164.751 ms
drop index ventes_agg_i1;
DROP INDEX
Temps : 289,288 ms
create index ventes_agg_br1 on ventes_agg using brin(dtv);
CREATE INDEX
Temps : 783,252 ms
\di+ ventes_agg_br1
Liste des relations
Schéma | Nom | Type | Propriétaire | Table | Taille | Description
--------+----------------+-------+--------------+------------+--------+-------------
public | ventes_agg_br1 | index | postgres | ventes_agg | 48 kB |
(1 ligne)
explain analyze select avg(mtv) from ventes_agg where dtv between to_date('01/01/2015', 'DD/MM/YYYY') and to_date('01/01/2016','DD/MM/YYYY');
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------------------------
Aggregate (cost=45821.51..45821.52 rows=1 width=32) (actual time=441.963..441.964 rows=1 loops=1)
-> Bitmap Heap Scan on ventes_agg (cost=5457.34..44493.38 rows=531252 width=2) (actual time=2.918..383.086 rows=525600 loops=1)
Recheck Cond: ((dtv >= to_date('01/01/2015'::text, 'DD/MM/YYYY'::text)) AND (dtv <= to_date('01/01/2016'::text, 'DD/MM/YYYY'::text)))
Rows Removed by Index Recheck: 19040
Heap Blocks: lossy=2944
-> Bitmap Index Scan on ventes_agg_br1 (cost=0.00..5324.52 rows=531252 width=0) (actual time=0.154..0.154 rows=29440 loops=1)
Index Cond: ((dtv >= to_date('01/01/2015'::text, 'DD/MM/YYYY'::text)) AND (dtv <= to_date('01/01/2016'::text, 'DD/MM/YYYY'::text)))
Planning time: 0.139 ms
Execution time: 442.002 ms
...
Planning time: 0.084 ms
Execution time: 463.037 ms
...
Planning time: 0.080 ms
Execution time: 462.794 ms