Module J2
Dalibo SCOP
24.12
18 décembre 2024
Cette formation est sous licence CC-BY-NC-SA. Vous êtes libre de la redistribuer et/ou modifier aux conditions suivantes :
PostgreSQL® Postgres® et le logo Slonik sont des marques déposées par PostgreSQL Community Association of Canada.
Ce document ne couvre que les versions supportées de PostgreSQL au moment de sa rédaction, soit les versions 13 à 17.
EXPLAIN
CALL
TRUNCATE
COPY
WHERE
services
services_big
employes
employes_big
service*
num_service
employes*
matricule
date_embauche
SELECT matricule, nom, prenom, nom_service, fonction, localisation FROM employes emp JOIN services ser ON (emp.num_service = ser.num_service) WHERE ser.localisation = 'Nantes';
L’objet de ce module est de comprendre son plan d’exécution :
Hash Join (cost=1.06..2.28 rows=4 width=48) Hash Cond: (emp.num_service = ser.num_service) -> Seq Scan on employes emp (cost=0.00..1.14 rows=14 width=35) -> Hash (cost=1.05..1.05 rows=1 width=21) -> Seq Scan on services ser (cost=0.00..1.05 rows=1 width=21) Filter: ((localisation)::text = 'Nantes'::text)
Rappels :
Règle 1 : récupérer le bon résultat
Règle 2 : le plus rapidement possible
critere IN (SELECT ...)
L’optimiseur doit choisir :
DISTINCT
SET
ANALYZE
pg_class
relpages
reltuples
NULL
pg_statistic
pg_stats
SELECT * FROM pg_stats WHERE schemaname = 'public' AND tablename = 'employes' AND attname = 'date_embauche' \gx
-[ RECORD 1 ]----------+--------------------------------------------------------- schemaname | public tablename | employes attname | date_embauche inherited | f null_frac | 0 avg_width | 4 n_distinct | -0.5 most_common_vals | {2006-03-01,2006-09-01,2000-06-01,2005-03-06,2006-01-01} most_common_freqs | {0.214286,0.214286,0.142857,0.142857,0.142857} histogram_bounds | {2003-01-01,2006-06-01} correlation | 1 most_common_elems | ¤ most_common_elem_freqs | ¤ elem_count_histogram | ¤
CREATE STATISTICS
CREATE STATISTICS employe_big_extract ON extract('year' FROM date_embauche) FROM employes_big;
Vues disponibles :
pg_stats_ext
pg_stats_ext_exprs
ANALYZE [ VERBOSE ] [ table [ ( colonne [, ...] ) ] [, ...] ]
psql
vacuumdb --analyze-only
default_statistics_target
ALTER TABLE matable ALTER COLUMN nomchamp SET STATISTICS 300 ;
ALTER STATISTICS nom SET STATISTICS valeur ;
BUFFERS
WAL
SERIALIZE
SETTINGS
VERBOSE
MEMORY
GENERIC_PLAN
OFF
COSTS
TIMING
SUMMARY
FORMAT
seq_page_cost
cpu_tuple_cost
cpu_operator_cost
enable_seqscan
parallel_tuple_cost
min_parallel_table_scan_size
VACUUM
random_page_cost
cpu_index_tuple_cost
effective_cache_size
effective_io_concurrency
maintenance_io_concurrency
min_parallel_index_scan_size
enable_indexscan
enable_indexonlyscan
enable_bitmapscan
EXISTS
IN
work_mem
hash_mem_multiplier
enable_nestloop
enable_hashjoin
enable_mergejoin
enable_hashagg
UNION ALL
Les statistiques sont-elles à jour ?
SELECT * FROM corr1 WHERE c1=1 AND c2=1
c1 = 1
c2 = 1
CREATE STATISTICS corr1_c1_c2 ON c1,c2 FROM corr1 ;
join_collapse_limit
from_collapse_limit
SELECT * FROM employes_big WHERE extract('year' from date_embauche) = 2006 ;
extract
CREATE STATISTIC
SELECT * FROM t1 WHERE c2 LIKE 'x%';
varchar_pattern_ops
text_pattern_ops
CREATE INDEX ON matable (champ_texte varchar_pattern_ops);
LIKE '%mot%'
pg_trgm
DELETE
Delete (actual time=111.251..111.251 rows=0 loops=1) -> Hash Join (actual time=1.094..21.402 rows=9347 loops=1) -> Seq Scan on lot_a30_descr_lot (actual time=0.007..11.248 rows=34934 loops=1) -> Hash (actual time=0.501..0.501 rows=561 loops=1) -> Bitmap Heap Scan on lot_a10_pdl (actual time=0.121..0.326 rows=561 loops=1) Recheck Cond: (id_fantoir_commune = 320013) -> Bitmap Index Scan on... (actual time=0.101..0.101 rows=561 loops=1) Index Cond: (id_fantoir_commune = 320013) Trigger for constraint fk_lotlocal_lota30descrlot: time=1010.358 calls=9347 Trigger for constraint fk_nonbatia21descrsuf_lota30descrlot: time=2311695.025 calls=9347 Total runtime: 2312835.032 ms
SELECT DISTINCT t1.* FROM t1 JOIN t2 ON (t1.id=t2.t1_id);
DISTINCT ON
GROUP BY
WHERE col1 + 2 > 5 → WHERE col1 > 5 - 2
WHERE col1 <> 'valeur';
NOT IN
NOT EXISTS
UNION
SELECT
LATERAL
shared_preload_libraries='auto_explain' -- redémarrage !
ALTER DATABASE erp SET auto_explain.log_min_duration = '3s' ;
LOAD 'auto_explain' ; SET auto_explain.log_analyze TO true;
N’hésitez pas, c’est le moment !
https://dali.bo/j2_quiz
La version en ligne des solutions de ces TP est disponible sur https://dali.bo/j2_solutions.