You are on page 1of 22

Name: Hrishikesh Bade

Reg ID: 201080012


Sem: 5
Sub: Spatial Databases and Graphs Lab

Experiment 05: Spatial Joins and Spatial Indexes

Aim: Spatial Joins and Spatial Indexes

Spatial Joins: Read the following content and execute queries given inthem ...
https://postgis.net/workshops/postgis-intro/joins.html
https://postgis.net/workshops/postgis-intro/joins_exercises.html
https://postgis.net/workshops/postgis-intro/joins_advanced.html // section
22.1.2
onward
Execute queries:
To check indexes available in PostgreSQL
To check indexes created/present on a table
Create a new table as follows and check for the available index (if any)
Test-Query: you will check the execution-time of following query with and without indexes

Re-run the Test-Query multiple times to get the average execution-time.

1. Without Index:
Run the Test-Query and note the execution-time.

2. With GIST Index:


Note the index creation time.
Execute a query to check the created index.
Run the Test-Query and note the execution time.

3. With BRIN Index:


DROP INDEX nyc_cb_gist_geom_idx;
Execute a query to check the available index (if
any). Note the index creation time.
Execute a query to check the created index.
Run the Test-Query and note the execution time.
Theory:

● Spatial indexes make using a spatial database for large data sets possible.
Without indexing, a search for features requires a sequential scan of every record
in the database. Indexing speeds up searching by organizing the data into a
structure which can be quickly traversed to find matching records. Spatial joins
are the bread-and-butter of spatial databases. They allow you to combine
information from different tables by using spatial relationships as the join key.
Much of what we think of as “standard GIS analysis” can be expressed as spatial
joins.
● GiST (Generalized Search Tree) indexes break up data into "things to one
side", "things which overlap", "things which are inside" and can be used on a
wide range of data-types, including GIS data.
● BRIN (Block Range Index) indexes operate by summarizing the spatial extent
of ranges of table records. Search is done via a scan of the ranges. BRIN is only
appropriate for use for some kinds of data .
● SP-GiST (Space-Partitioned Generalized Search Tree) is a generic index
method that supports partitioned search trees such as quad-trees, k-d trees, and
radix trees (tries).
● Some of the functions used:
○ sum(expression): aggregate to return a sum for a set of records
○ count(expression): aggregate to return the size of a set of records
○ ST_Area(geometry) returns the area of the polygons
○ ST_AsText(geometry) returns WKT text
○ ST_Contains(geometry A, geometry B) returns the true if
geometry A contains geometry B
○ ST_Distance(geometry A, geometry B) returns the minimum
distance between geometry A and geometry B
○ ST_DWithin(geometry A, geometry B, radius) returns the true if
geometry A is radius distance or less from geometry B
○ ST_GeomFromText(text) returns geometry
○ ST_Intersects(geometry A, geometry B) returns the true if
geometry A intersects geometry B
○ ST_Length(linestring) returns the length of the linestring
○ ST_Touches(geometry A, geometry B) returns the true if the
boundary of geometry A touches geometry B
○ ST_Within(geometry A, geometry B) returns the true if geometry A
is within geometry B

Output:
● https://postgis.net/workshops/postgis-intro/joins_exercises.html
https://postgis.net/workshops/postgis-intro/joins_advanced.html
To check indexes available in PostgreSQL :
To check indexes created/present on a table :

Create a new table as follows and check for the available index (if
any) :
Test-Query : you will check the execution-time of the following query with and without
indexes ...
SELECT b.blkid
FROM nyc_cb_YourRegistrationNumber b
JOIN nyc_subway_stations s
ON ST_Contains(b.geom, s.geom)
WHERE s.name LIKE 'B%';
Note : Re-run the Test-Query multiple times to get the average execution-time.
1. Without Index :
Run the Test-Query and note the execution-time.
Average Time: 0.371s

2. With GIST Index :


CREATE INDEX nyc_cb_gist_geom_idx ON nyc_cb_YourRegistrationNumber USING
GIST (geom);
Note the index creation-time.
2. With GIST Index :
CREATE INDEX nyc_cb_gist_geom_idx ON nyc_cb_YourRegistrationNumber USING GIST (geom);
Note the index creation-time.

Execute a query to check the created index.


Run the Test-Query and note the execution-time.
Average Time: .300s
3. With BRIN Index :
DROP INDEX nyc_cb_gist_geom_idx;

Execute a query to check the available index (if any).

CREATE INDEX nyc_cb_brin_geom_idx ON nyc_cb_YourRegistrationNumber


USING BRIN (geom);
Note the index creation-time.

Execute a query to check the created index.


Run the Test-Query and note the executiAverage Time: 0.370son-time.
Average Time: 0.370s

Try any other query to check its execution-time with and without indexes.

Without Index
With Gist Index With BRIN Index

Conclusion:
Thus we have successfully studied indexing in psql using pgadmin4.

You might also like