Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Gbroccolo itpug p_gday2014_geodbindex
1. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Uso degli indici nei database
geospaziali con PostgreSQL
Giuseppe Broccolo
www.2ndquadrant.it
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
2. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Chi sono
• Consulente PostgreSQL e PostGIS
– Sviluppo, Replica, Disaster Recovery, Benchmark pre-produzione, Remote
DBA, Supporto 24/7, Formazione
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
3. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Sommario
• Gli indici nei DB geospaziali
• Cosa offre PostgreSQL
• Un esempio d'uso: lavorare con un “insieme di punti”
– Uso di tipi nativi in PostgreSQL
– Cosa introduce PostGIS
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
4. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Indici
• Struttura binaria su disco che velocizza l'accesso ai dati:
– Non solo letture veloci: INSERT, UPDATE, DELETE
– Struttura ad albero: bilanciamento (o meno) della struttura dei nodi
– Performance teoriche:
• R/W: O(log N)
• Size: O(N)
– L'indice più usato: B-tree
• Bilanciato
• Applicabile su dati che supportino operatori di ordinamento/confronto
– <, >, =, BETWEEN, IN, IS (NOT) NULL, LIKE, …
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
5. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Indici per dati geospaziali
• La struttura dei dati geospaziali
– Contempla oggetti generalmente n-dimensionali
– Non prevede operatori di ordinamento, ma di posizionamento
• Nodi definiti a partire da un container che includa l'intero
data set detto Bounding Box
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
6. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Bounding Boxes
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
7. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Bounding Boxes
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
8. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Indici per dati geospaziali
• Algoritmi:
– Bilanciati: R-tree, ...
– Non bilanciati: kd-tree, Quad-tree, ...
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
9. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Indici per dati geospaziali
• Algoritmi:
– Bilanciati: R-tree, ...
– Non bilanciati: kd-tree, Quad-tree, ...
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
10. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Indici per dati geospaziali
• Algoritmi:
– Bilanciati: R-tree, ...
– Non bilanciati: kd-tree, Quad-tree, ...
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
11. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
PostgreSQL
• DBMS noto a tutti i presenti
• Prevede “in core” tipi di dato geometrico (non geografico)
– point, circle, box
– @-@, @@, <->, &&, <<, >>, <<|, |>>, ...
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
12. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
PostgreSQL + PostGIS
• DBMS noto a tutti i presenti
• Prevede “in core” tipi di dato geometrico (non geografico)
– point, circle, box
– @-@, @@, <->, &&, <<, >>, <<|, |>>, ...
• Estendibile per uso geospaziale grazie a PostGIS
– geometry (geometrie/geografie)
– <@, @>, &&, <<, >>, <<|, |>>, ...
– ST_Lenght(), ST_Distance(), ...
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
13. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
PostgreSQL + PostGIS
• Indici bilanciati
– B-Tree
– GIN (Generalized Inverted Index) – adatto in lettura/update
– GiST (Generalized Search Tree) - adatto in inserimento, “lossy”
• Supporto per ricerche k-NN
• Indice non bilanciato
– SP-GiST (Space Partitioned GiST) – basso I/O, transazioni “in-memory”
• Introdotto in PostgreSQL 9.2
• Supportato da PostGIS >2.1
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
14. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
PostgreSQL + PostGIS
• Indici bilanciati
– B-Tree
– GIN (Generalized Inverted Index) – adatto in lettura/update
– GiST (Generalized Search Tree) - adatto in inserimento, “lossy”
• Supporto per ricerche k-NN
• Indice non bilanciato
– SP-GiST (Space Partitioned GiST) – basso I/O, transazioni “in-memory”
• Introdotto in PostgreSQL 9.2
• Supportato per PostGIS >2.1
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
15. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Esempi & Performance
• L'ambiente di test: Vagrant VM (CentOS 6)
– Single virtual core 2.26GHz, RAM 512MB, Disco 7.2k
• PostgreSQL 9.3 + PostGIS 2.1
– postgresql.conf: default
• Insieme di punti 2D (~10.000.000)
– Ricerca di Nearest Neighbours
– Ricerca all'interno di Bounding Boxes
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
16. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Esempi & Performance
• L'ambiente di test: Vagrant VM (CentOS 6)
– Single virtual core 2.26GHz, RAM 512MB, Disco 7.2k
• PostgreSQL 9.3 + PostGIS 2.1
– postgresql.conf: default
• Insieme di punti 2D (~10.000.000)
– Ricerca di Nearest Neighbours
– Ricerca all'interno di Bounding Boxes
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
17. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Esempi & Performance
• L'ambiente di test: Vagrant VM (CentOS 6)
– Single virtual core 2.26GHz, RAM 512MB, Disco 7.2k
• PostgreSQL 9.3 + PostGIS 2.1
– postgresql.conf: default
• Insieme di punti 2D (~10.000.000)
– Ricerca di Nearest Neighbours
– Ricerca all'interno di Bounding Boxes
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
18. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Esempi & Performance
• DDL
=# dt+
– many_point: id (serial Primary Key), point (point)
– many_geom: id (serial Primary Key), point (geometry(point,0))
List of relations
Schema | Name | Type | Owner | Size | Description
----------+-----------------+-------+----------+------------+-------------
public | many_geom | table | postgres | 653 MB |
public | many_point | table | postgres | 501 MB |
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
19. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Un po' di indici...
– Il tipo point supporta sia l'indicizzazione GiST che SPGiST
=# CREATE INDEX idx_gist_point ON many_point USING gist(point);
=# CREATE INDEX idx_spgist_point ON many_point USING spgist(point);
– Il tipo geom supporta solo l'indicizzazione GiST
=# CREATE INDEX idx_gist_geom ON many_geom USING gist(point);
=# CREATE INDEX idx_spgist ON many_geom USING spgist(point);
ERROR: data type geometry has no default operator class for access
method "spgist"
HINT: You must specify an operator class for the index or define a
default operator class for the data type.
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
20. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Un po' di indici...
index size time
idx_gist_point 715MB 214s
idx_spgist_point 437MB 137s
idx_gist_geom 523MB 290s
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
21. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Ricerca di Nearest Neighbours
– Utilizzando il tipo point
=# SELECT * FROM many_point ORDER BY point(0.5, 0.5) <-> point LIMIT 10;
– Utilizzando il tipo geom
=# SELECT * FROM many_geom ORDER BY ST_MakePoint(0.5, 0.5) <-> geom LIMIT 10;
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
22. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
BTW...
– Utilizzando il tipo geom in PostGIS <9.2 + PostGIS <2.0
SELECT *, ST_Distance(t.geom, o.origin) AS dist
FROM many_point AS t INNER JOIN
(SELECT ST_MakePoint(0.5, 0.5) AS origin) AS o
ON ST_DWithin(t.geom, o.origin, 100)
ORDER BY dist
LIMIT 10;
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
23. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Ricerca di Nearest Neighbours
• Risultati ottenuti (assenza di indici):
– Dato di tipo point
planner strategy time execution
Seq. Scan + Sort 732ms
7.3s
– Dato di tipo geom
planner strategy time execution
Seq. Scan + Sort 17.2s
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
24. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Ricerca di Nearest Neighbours
• Risultati ottenuti:
– Dato di tipo point
planner strategy time execution
Seq. Scan + Sort 732ms
Index Scan
(idx_gist_point)
– Dato di tipo geom
52ms
planner strategy time execution
Index Scan
(idx_gist_geom)
18ms
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
25. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Ricerca all'interno di Bounding Boxes
– Utilizzando il tipo point
=# SELECT * FROM many_point WHERE point <@ box(point(0.4, 0.4), point(0.6,
0.6));
– Utilizzando il tipo geom
=# SELECT * FROM many_geom WHERE point && ST_GeomFromEWKT('SRID=0;
LINESTRING(0.4 0.4, 0.4 0.6, 0.6 0.6, 0.6 0.4, 0.4 0.4)');
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
26. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Ricerca all'interno di Bounding Boxes
• Risultati ottenuti (assenza di indici):
– Dato di tipo point
planner strategy time execution
Seq. Scan + Sort 732ms
Seq. Scan + Filter “&&” 5.7s
– Dato di tipo geom
planner strategy time execution
Seq. Scan + Filter “<@” 2.0s
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
27. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Ricerca all'interno di Bounding Boxes
• Risultati ottenuti:
– Dato di tipo point
planner strategy time execution
Seq. Scan + Sort 732ms
Bitmap Index Scan
(idx_spgist_point)
– Dato di tipo geom
0.4s
planner strategy time execution
Bitmap Index Scan
0.7s
(idx_gist_geom)
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
28. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Conclusioni
• Sono state mostrate le potenzialità di PostgreSQL per lavorare con
dati geometrici
– Già include alcuni tipi semplici di geometrie con cui lavorare
• PostGIS introduce nuove funzionalità che potenziano l'uso degli
indici
– Aumento delle performance
• In attesa dell'introduzione degli indici SP-GiST anche per i punti di
tipo geom(point) di PostGIS...
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it
29. PGDay.IT 2014
Prato
Venerdì 7 Novembre 2014
Domande?
• giuseppe.broccolo@2ndquadrant.it
• @giubro
• gemini__81
• gbroccolo7
2ndQuadrant Italia Giuseppe Broccolo – giuseppe.broccolo@2ndquadrant.it