SlideShare a Scribd company logo
1 of 42
Download to read offline
(Ab)using 4D indexing in
PostGIS 2.2 with
PostgreSQL 9.5 to give you
the perfect match
Victor Blomqvist
vb@viblo.se
blomqvist@tantanapp.com
Tantan (探探)
March 19, pgDay Asia 2016 in Singapore
1tantanapp.com
At Tantan we use PostgreSQL &
PostGIS for everything!
2tantanapp.com
Suggesting users is difficult
• How should we rank them?
• How can it execute quickly?
(At Tantan we need to do 1000 ranking queries per
second at peak!)
tantanapp.com 3
The exciting new feature in
PostGIS is this:
<<->>
tantanapp.com 4
Today I will show how to take
advantage of the 4th dimension!
1. Use double the amount of dimensions from 2
2. …
3. Profit!
tantanapp.com 5
We will look at 3 different properties
to help our suggestion SELECT
1. Popularity
2. Age
3. Activity
tantanapp.com 6
Let’s begin with 2 dimensions
tantanapp.com 7
Y
(Latitude)
X
(Longitude)
Table and index
CREATE TABLE users (
id serial PRIMARY KEY,
birthdate date,
location geometry,
active_time timestamp,
popularity double precision
);
CREATE INDEX users_location_gix
ON users USING GIST (location);
tantanapp.com 8
Selecting
SELECT * FROM users
ORDER BY location <-> ST_MakePoint(103.8, 1.3)
/* Singapore */
LIMIT 10;
tantanapp.com 9
Lets look at our first case
1. Popularity <--
2. Age
3. Activity
tantanapp.com 10
The popularity formula
Popularity = likes / (likes + dislikes)
tantanapp.com 11
Order by location and popularity
WITH x AS (SELECT * FROM users
ORDER BY location <-> ST_MakePoint(103.8, 1.3)
LIMIT 100)
SELECT * FROM x
ORDER BY
ST_Distance(location::geography, ST_MakePoint(103.8, 1.3))
* 1 / (popularity+1)
LIMIT 10;
tantanapp.com 12
Picture of x-y-z axis
tantanapp.com 13
X
(Longitude)
Y
(Latitude)
Z
(Popularity)
Adding a 3rd dimension!
ALTER TABLE users ADD COLUMN loc_pop geometry;
UPDATE users
SET loc_pop = ST_makepoint(ST_X(location),
ST_Y(location), 0.01 * 1 / (popularity+1));
CREATE INDEX users_loc_pop_gix
ON users USING GIST (loc_pop gist_geometry_ops_nd);
tantanapp.com 14
To use our new 3D index we need
to use the new <<->> operator
<<->> — Returns the n-D distance between the centroids
of A and B bounding boxes.
This operand will make use of n-D GiST indexes that may
be available on the geometries. It is different from other
operators that use spatial indexes in that the spatial
index is only used when the operator is in the ORDER BY
clause.
* http://postgis.net/docs/manual-2.2/geometry_distance_centroid_nd.html
tantanapp.com 15
With <<->> our query becomes
SELECT * FROM users
ORDER BY loc_pop <<->> ST_MakePoint(103.8, 1.3, 0)
LIMIT 10;
tantanapp.com 16
Second case
1. Popularity
2. Age <--
3. Activity
tantanapp.com 17
A quick review of our table:
CREATE TABLE users (
id serial PRIMARY KEY,
birthdate date,
location geometry,
active_time timestamp,
popularity double precision
);
tantanapp.com 18
Selecting with age filter
SELECT * FROM users
WHERE age(birthdate) between '20 years' AND '30 years'
ORDER BY location <-> ST_MakePoint(103.8, 1.3)
LIMIT 10;
tantanapp.com 19
A very selective user might only
want to look at 74 year olds
SELECT * FROM users
WHERE age(birthdate) between '74 years' AND '75 years'
ORDER BY location <-> ST_MakePoint(103.8, 1.3)
LIMIT 10;
tantanapp.com 20
Explain analyze of the query
Limit (cost=0.41..62011.93 rows=10 width=96) (actual
time=49.456..551.955 rows=10 loops=1)
-> Index Scan using users_location_gix on users
(cost=0.41..1103805.51 rows=178 width=96) (actual time=49.398..546.631
rows=10 loops=1)
Order By: (location <-> 'XXX'::geometry)
Filter: ((birthdate >= (now() - '75 years'::interval)) AND (birthdate <=
(now() - '74 years'::interval)))
Rows Removed by Filter: 192609
Planning time: 0.157 ms
Execution time: 553.151 ms
tantanapp.com 21
This is a real
problem
tantanapp.com 22
Possible solutions?
• Prevent searches of
restricted ages
• Add a distance
restriction
• Add age to the geo index
tantanapp.com 23
Adding age to the geo index
ALTER TABLE users ADD COLUMN loc_age geometry;
UPDATE users
SET loc_age = ST_makepoint(ST_X(location), ST_Y(location),
Extract('year' FROM birthdate)/100000);
CREATE INDEX users_loc_age_gix ON users USING GIST (loc_age
gist_geometry_ops_nd);
tantanapp.com 24
Updated query
SELECT * FROM users
WHERE loc_age &&&
ST_MakeLine(
ST_MakePoint(180, 90, Extract('year' FROM Now() - interval '74
years')/100000),
ST_MakePoint(-180, -90, Extract('year' FROM Now() - interval '75
years')/100000))
ORDER BY loc_age <<->> ST_MakePoint(103.8, 1.3, 0)
LIMIT 10;
tantanapp.com 25
Looking at the execution plan we
can see that all is good
Limit (cost=0.41..8.43 rows=1 width=136) (actual time=15.294..16.082
rows=10 loops=1)
-> Index Scan using users_loc_age_gix on users (cost=0.41..8.43 rows=1
width=136) (actual time=14.685..14.948 rows=10 loops=1)
Index Cond: (loc_age &&& 'XXX'::geometry)
Order By: (loc_age <<->> 'XXX'::geometry)
Planning time: 0.332 ms
Execution time: 19.053 ms
tantanapp.com 26
Looking at the result we see that
something is wrong
UserID Age
6827677 74 years 11 mons 7 days
1281456 75 years 15 days
1269119 73 years 7 mons 27 days
5791734 73 years 7 mons 8 days
3875002 74 years 7 mons 14 days
6373179 73 years 5 mons 8 days
3727434 74 years 7 mons 21 days
5214330 74 years 10 days
3127049 74 years 8 mons 22 days
6390900 74 years 21 days
tantanapp.com 27
Solution: Keep the non-geometry
where statement
SELECT * FROM users
WHERE age(birthdate) BETWEEN '74 years' AND '75 years'
AND loc_age &&&
ST_MakeLine(
ST_MakePoint(180, 90, Extract('year' FROM Now() - interval '74
years')/100000),
ST_MakePoint(-180, -90, Extract('year' FROM Now() - interval '75
years')/100000))
ORDER BY loc_age <<->> ST_MakePoint(103.8, 1.3, 0)
LIMIT 10;
tantanapp.com 28
Finally we look at Activity
1. Popularity
2. Age
3. Activity <--
tantanapp.com 29
Order by last active time
WITH x AS (SELECT * FROM users
ORDER BY location <-> ST_MakePoint(103.8, 1.3)
LIMIT 100)
SELECT * FROM x
ORDER BY
ST_Distance(location::geography, ST_MakePoint(103.8, 1.3))
* Extract(Now() - active_time)
LIMIT 10;
tantanapp.com 30
What is the difference between
time, popularity and age?
• Popularity is bounded, a users popularity can range
between 0 and 1.
• Age is also bounded, and static. All our users are
between 16 and 100 years old and their birthdate
never change.
• Time increase infinitely in one direction.
tantanapp.com 31
Adding time to the geo column
ALTER TABLE users ADD COLUMN loc_active geometry;
UPDATE users
SET loc_active = ST_MakePoint(ST_X(location), ST_Y(location),
Extract('epoch' FROM active_time) / 60 / 10000);
CREATE INDEX users_loc_active_gix
ON users USING GIST (loc_active gist_geometry_ops_nd);
tantanapp.com 32
Select with time becomes
SELECT * FROM users
ORDER BY loc_active <<->> ST_MakePoint(103.8, 1.3,
Extract('epoch' FROM Now()) / 60 / 10000)
LIMIT 10;
tantanapp.com 33
What we talked about so far
1. Location X and Y
2. Popularity
3. Age
4. Activity
tantanapp.com 34
Adding a 4th dimension
ALTER TABLE users ADD COLUMN loc_pop_age geometry;
UPDATE users
SET loc_pop_age =
ST_MakePoint(ST_X(location), ST_Y(location),
0.01 * 1 / (popularity+1),
Extract('year' FROM birthdate)/100000);
CREATE INDEX users_loc_pop_age_gix
ON users USING GIST (loc_pop_age gist_geometry_ops_nd);
tantanapp.com 35
Adding a 4th dimension - Query
SELECT * FROM users
WHERE loc_pop_age &&&
ST_MakeLine(
ST_MakePoint(180, 90, -100, Extract('year' FROM Now() - interval
'20 years')/100000),
ST_MakePoint(-180, -90, 100, Extract('year' FROM Now() - interval
'30 years')/100000))
ORDER BY loc_pop_age <<->> ST_MakePoint(103.8, 1.3, 0, 0)
LIMIT 10;
tantanapp.com 36
Query runtimes 2D vs 3D vs 4D
tantanapp.com 37
0 10 20 30 40 50
Location
Location + popularity
Location + age (20-30)
Location + age (74)
Location + activity
Location + pop + age (4D)
3D/4D index (ms) 2D index (ms)
Query times did not impress.
What about index sizes?
tantanapp.com 38
0 50 100 150 200
users_pkey
users_location_gix
users_loc_pop_gix
users_loc_pop_age_gix
users_loc_age_gix
users_loc_active_gix
Size bloated (MB) Size fresh(MB)
Conclusion
1. Popularity
• Pro: Improves ranking?
• Con: Makes it more difficult to reason about the ranking formula
• Con: Makes the query slower
2. Age
• Pro: Fixes query time of outlier queries
• Con: Make the average query time longer
• Con: Have to take special care to not disturb the normal ranking
3. Time
• Pro: Improves ranking?
• Con: Much more difficult to reason about the ranking formula
• Con: Makes the query slower
tantanapp.com 39
Conclusion
Its more difficult to use the 3rd and 4th dimension
than what a quick glance reveals. Test extensively!
tantanapp.com 40
tantanapp.com 41
Questions?
tantanapp.com 42
Thank You!
blomqvist@tantanapp.com
vb@viblo.se

More Related Content

Viewers also liked

Lightening Talk - PostgreSQL Worst Practices
Lightening Talk - PostgreSQL Worst PracticesLightening Talk - PostgreSQL Worst Practices
Lightening Talk - PostgreSQL Worst PracticesPGConf APAC
 
Query Parallelism in PostgreSQL: What's coming next?
Query Parallelism in PostgreSQL: What's coming next?Query Parallelism in PostgreSQL: What's coming next?
Query Parallelism in PostgreSQL: What's coming next?PGConf APAC
 
Lessons PostgreSQL learned from commercial databases, and didn’t
Lessons PostgreSQL learned from commercial databases, and didn’tLessons PostgreSQL learned from commercial databases, and didn’t
Lessons PostgreSQL learned from commercial databases, and didn’tPGConf APAC
 
Use Case: PostGIS and Agribotics
Use Case: PostGIS and AgriboticsUse Case: PostGIS and Agribotics
Use Case: PostGIS and AgriboticsPGConf APAC
 
How to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'rollHow to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'rollPGConf APAC
 
PostgreSQL Rocks Indonesia
PostgreSQL Rocks IndonesiaPostgreSQL Rocks Indonesia
PostgreSQL Rocks IndonesiaPGConf APAC
 
There is Javascript in my SQL
There is Javascript in my SQLThere is Javascript in my SQL
There is Javascript in my SQLPGConf APAC
 
Why we love pgpool-II and why we hate it!
Why we love pgpool-II and why we hate it!Why we love pgpool-II and why we hate it!
Why we love pgpool-II and why we hate it!PGConf APAC
 
Introduction to Vacuum Freezing and XID
Introduction to Vacuum Freezing and XIDIntroduction to Vacuum Freezing and XID
Introduction to Vacuum Freezing and XIDPGConf APAC
 
PostgreSQL Enterprise Class Features and Capabilities
PostgreSQL Enterprise Class Features and CapabilitiesPostgreSQL Enterprise Class Features and Capabilities
PostgreSQL Enterprise Class Features and CapabilitiesPGConf APAC
 
Security Best Practices for your Postgres Deployment
Security Best Practices for your Postgres DeploymentSecurity Best Practices for your Postgres Deployment
Security Best Practices for your Postgres DeploymentPGConf APAC
 
What's New in PostgreSQL 9.6
What's New in PostgreSQL 9.6What's New in PostgreSQL 9.6
What's New in PostgreSQL 9.6EDB
 
Useful PostgreSQL Extensions
Useful PostgreSQL ExtensionsUseful PostgreSQL Extensions
Useful PostgreSQL ExtensionsEDB
 
Best Practices for Becoming an Exceptional Postgres DBA
Best Practices for Becoming an Exceptional Postgres DBA Best Practices for Becoming an Exceptional Postgres DBA
Best Practices for Becoming an Exceptional Postgres DBA EDB
 
Postgresql database administration volume 1
Postgresql database administration volume 1Postgresql database administration volume 1
Postgresql database administration volume 1Federico Campoli
 
What's New in PostgreSQL 9.3
What's New in PostgreSQL 9.3What's New in PostgreSQL 9.3
What's New in PostgreSQL 9.3EDB
 
EnterpriseDB BackUp and Recovery Tool
EnterpriseDB BackUp and Recovery ToolEnterpriseDB BackUp and Recovery Tool
EnterpriseDB BackUp and Recovery ToolEDB
 

Viewers also liked (20)

Lightening Talk - PostgreSQL Worst Practices
Lightening Talk - PostgreSQL Worst PracticesLightening Talk - PostgreSQL Worst Practices
Lightening Talk - PostgreSQL Worst Practices
 
Query Parallelism in PostgreSQL: What's coming next?
Query Parallelism in PostgreSQL: What's coming next?Query Parallelism in PostgreSQL: What's coming next?
Query Parallelism in PostgreSQL: What's coming next?
 
Lessons PostgreSQL learned from commercial databases, and didn’t
Lessons PostgreSQL learned from commercial databases, and didn’tLessons PostgreSQL learned from commercial databases, and didn’t
Lessons PostgreSQL learned from commercial databases, and didn’t
 
Use Case: PostGIS and Agribotics
Use Case: PostGIS and AgriboticsUse Case: PostGIS and Agribotics
Use Case: PostGIS and Agribotics
 
How to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'rollHow to teach an elephant to rock'n'roll
How to teach an elephant to rock'n'roll
 
PostgreSQL Rocks Indonesia
PostgreSQL Rocks IndonesiaPostgreSQL Rocks Indonesia
PostgreSQL Rocks Indonesia
 
There is Javascript in my SQL
There is Javascript in my SQLThere is Javascript in my SQL
There is Javascript in my SQL
 
Why we love pgpool-II and why we hate it!
Why we love pgpool-II and why we hate it!Why we love pgpool-II and why we hate it!
Why we love pgpool-II and why we hate it!
 
Introduction to Vacuum Freezing and XID
Introduction to Vacuum Freezing and XIDIntroduction to Vacuum Freezing and XID
Introduction to Vacuum Freezing and XID
 
PostgreSQL Enterprise Class Features and Capabilities
PostgreSQL Enterprise Class Features and CapabilitiesPostgreSQL Enterprise Class Features and Capabilities
PostgreSQL Enterprise Class Features and Capabilities
 
Security Best Practices for your Postgres Deployment
Security Best Practices for your Postgres DeploymentSecurity Best Practices for your Postgres Deployment
Security Best Practices for your Postgres Deployment
 
What's New in PostgreSQL 9.6
What's New in PostgreSQL 9.6What's New in PostgreSQL 9.6
What's New in PostgreSQL 9.6
 
Useful PostgreSQL Extensions
Useful PostgreSQL ExtensionsUseful PostgreSQL Extensions
Useful PostgreSQL Extensions
 
Best Practices for Becoming an Exceptional Postgres DBA
Best Practices for Becoming an Exceptional Postgres DBA Best Practices for Becoming an Exceptional Postgres DBA
Best Practices for Becoming an Exceptional Postgres DBA
 
Postgresql database administration volume 1
Postgresql database administration volume 1Postgresql database administration volume 1
Postgresql database administration volume 1
 
What's New in PostgreSQL 9.3
What's New in PostgreSQL 9.3What's New in PostgreSQL 9.3
What's New in PostgreSQL 9.3
 
EnterpriseDB BackUp and Recovery Tool
EnterpriseDB BackUp and Recovery ToolEnterpriseDB BackUp and Recovery Tool
EnterpriseDB BackUp and Recovery Tool
 
PostgreSQL DBA Neler Yapar?
PostgreSQL DBA Neler Yapar?PostgreSQL DBA Neler Yapar?
PostgreSQL DBA Neler Yapar?
 
TTÜ Geeky Weekly
TTÜ Geeky WeeklyTTÜ Geeky Weekly
TTÜ Geeky Weekly
 
Founding a LLC in Turkey
Founding a LLC in TurkeyFounding a LLC in Turkey
Founding a LLC in Turkey
 

Similar to (Ab)using 4d Indexing

Geospatial Data Analysis and Visualization in Python
Geospatial Data Analysis and Visualization in PythonGeospatial Data Analysis and Visualization in Python
Geospatial Data Analysis and Visualization in PythonHalfdan Rump
 
From the proposal to ECMAScript – Step by Step
From the proposal to ECMAScript – Step by StepFrom the proposal to ECMAScript – Step by Step
From the proposal to ECMAScript – Step by StepIgalia
 
12 things Oracle DBAs must know about SQL
12 things Oracle DBAs must know about SQL12 things Oracle DBAs must know about SQL
12 things Oracle DBAs must know about SQLSolarWinds
 
Geopy module in python
Geopy module in pythonGeopy module in python
Geopy module in pythonAshmita Dhakal
 
Opensource gis development - part 2
Opensource gis development - part 2Opensource gis development - part 2
Opensource gis development - part 2Andrea Antonello
 
DeepScan: Exploiting Deep Learning for Malicious Account Detection in Locatio...
DeepScan: Exploiting Deep Learning for Malicious Account Detection in Locatio...DeepScan: Exploiting Deep Learning for Malicious Account Detection in Locatio...
DeepScan: Exploiting Deep Learning for Malicious Account Detection in Locatio...yeung2000
 
Around the world with extensions | PostgreSQL Conference Europe 2018 | Craig ...
Around the world with extensions | PostgreSQL Conference Europe 2018 | Craig ...Around the world with extensions | PostgreSQL Conference Europe 2018 | Craig ...
Around the world with extensions | PostgreSQL Conference Europe 2018 | Craig ...Citus Data
 
10. Getting Spatial
10. Getting Spatial10. Getting Spatial
10. Getting SpatialFAO
 
R getting spatial
R getting spatialR getting spatial
R getting spatialFAO
 
The Future of Sharding
The Future of ShardingThe Future of Sharding
The Future of ShardingEDB
 
Nyc open data project ii -- predict where to get and return my citibike
Nyc open data project ii -- predict where to get and return my citibikeNyc open data project ii -- predict where to get and return my citibike
Nyc open data project ii -- predict where to get and return my citibikeVivian S. Zhang
 
From the proposal to ECMAScript, step by step
From the proposal to ECMAScript, step by stepFrom the proposal to ECMAScript, step by step
From the proposal to ECMAScript, step by stepIgalia
 
Just in time (series) - KairosDB
Just in time (series) - KairosDBJust in time (series) - KairosDB
Just in time (series) - KairosDBVictor Anjos
 
Where the %$#^ Is Everybody? Geospatial Solutions For Oracle APEX
Where the %$#^ Is Everybody? Geospatial Solutions For Oracle APEXWhere the %$#^ Is Everybody? Geospatial Solutions For Oracle APEX
Where the %$#^ Is Everybody? Geospatial Solutions For Oracle APEXJim Czuprynski
 
UKOUG 2019 - SQL features
UKOUG 2019 - SQL featuresUKOUG 2019 - SQL features
UKOUG 2019 - SQL featuresConnor McDonald
 
GeoMesa on Apache Spark SQL with Anthony Fox
GeoMesa on Apache Spark SQL with Anthony FoxGeoMesa on Apache Spark SQL with Anthony Fox
GeoMesa on Apache Spark SQL with Anthony FoxDatabricks
 

Similar to (Ab)using 4d Indexing (20)

Geospatial Data Analysis and Visualization in Python
Geospatial Data Analysis and Visualization in PythonGeospatial Data Analysis and Visualization in Python
Geospatial Data Analysis and Visualization in Python
 
From the proposal to ECMAScript – Step by Step
From the proposal to ECMAScript – Step by StepFrom the proposal to ECMAScript – Step by Step
From the proposal to ECMAScript – Step by Step
 
12 things Oracle DBAs must know about SQL
12 things Oracle DBAs must know about SQL12 things Oracle DBAs must know about SQL
12 things Oracle DBAs must know about SQL
 
Geopy module in python
Geopy module in pythonGeopy module in python
Geopy module in python
 
Tictac
TictacTictac
Tictac
 
Opensource gis development - part 2
Opensource gis development - part 2Opensource gis development - part 2
Opensource gis development - part 2
 
DeepScan: Exploiting Deep Learning for Malicious Account Detection in Locatio...
DeepScan: Exploiting Deep Learning for Malicious Account Detection in Locatio...DeepScan: Exploiting Deep Learning for Malicious Account Detection in Locatio...
DeepScan: Exploiting Deep Learning for Malicious Account Detection in Locatio...
 
Around the world with extensions | PostgreSQL Conference Europe 2018 | Craig ...
Around the world with extensions | PostgreSQL Conference Europe 2018 | Craig ...Around the world with extensions | PostgreSQL Conference Europe 2018 | Craig ...
Around the world with extensions | PostgreSQL Conference Europe 2018 | Craig ...
 
10. Getting Spatial
10. Getting Spatial10. Getting Spatial
10. Getting Spatial
 
R getting spatial
R getting spatialR getting spatial
R getting spatial
 
The Future of Sharding
The Future of ShardingThe Future of Sharding
The Future of Sharding
 
Tic tac toe game code
Tic tac toe game codeTic tac toe game code
Tic tac toe game code
 
Tic tac toe
Tic tac toeTic tac toe
Tic tac toe
 
10. R getting spatial
10.  R getting spatial10.  R getting spatial
10. R getting spatial
 
Nyc open data project ii -- predict where to get and return my citibike
Nyc open data project ii -- predict where to get and return my citibikeNyc open data project ii -- predict where to get and return my citibike
Nyc open data project ii -- predict where to get and return my citibike
 
From the proposal to ECMAScript, step by step
From the proposal to ECMAScript, step by stepFrom the proposal to ECMAScript, step by step
From the proposal to ECMAScript, step by step
 
Just in time (series) - KairosDB
Just in time (series) - KairosDBJust in time (series) - KairosDB
Just in time (series) - KairosDB
 
Where the %$#^ Is Everybody? Geospatial Solutions For Oracle APEX
Where the %$#^ Is Everybody? Geospatial Solutions For Oracle APEXWhere the %$#^ Is Everybody? Geospatial Solutions For Oracle APEX
Where the %$#^ Is Everybody? Geospatial Solutions For Oracle APEX
 
UKOUG 2019 - SQL features
UKOUG 2019 - SQL featuresUKOUG 2019 - SQL features
UKOUG 2019 - SQL features
 
GeoMesa on Apache Spark SQL with Anthony Fox
GeoMesa on Apache Spark SQL with Anthony FoxGeoMesa on Apache Spark SQL with Anthony Fox
GeoMesa on Apache Spark SQL with Anthony Fox
 

More from PGConf APAC

PGConf APAC 2018: Sponsored Talk by Fujitsu - The growing mandatory requireme...
PGConf APAC 2018: Sponsored Talk by Fujitsu - The growing mandatory requireme...PGConf APAC 2018: Sponsored Talk by Fujitsu - The growing mandatory requireme...
PGConf APAC 2018: Sponsored Talk by Fujitsu - The growing mandatory requireme...PGConf APAC
 
PGConf APAC 2018: PostgreSQL 10 - Replication goes Logical
PGConf APAC 2018: PostgreSQL 10 - Replication goes LogicalPGConf APAC 2018: PostgreSQL 10 - Replication goes Logical
PGConf APAC 2018: PostgreSQL 10 - Replication goes LogicalPGConf APAC
 
PGConf APAC 2018 - Lightening Talk #3: How To Contribute to PostgreSQL
PGConf APAC 2018 - Lightening Talk #3: How To Contribute to PostgreSQLPGConf APAC 2018 - Lightening Talk #3: How To Contribute to PostgreSQL
PGConf APAC 2018 - Lightening Talk #3: How To Contribute to PostgreSQLPGConf APAC
 
PGConf APAC 2018 - Lightening Talk #2 - Centralizing Authorization in PostgreSQL
PGConf APAC 2018 - Lightening Talk #2 - Centralizing Authorization in PostgreSQLPGConf APAC 2018 - Lightening Talk #2 - Centralizing Authorization in PostgreSQL
PGConf APAC 2018 - Lightening Talk #2 - Centralizing Authorization in PostgreSQLPGConf APAC
 
Sponsored Talk @ PGConf APAC 2018 - Choosing the right partner in your Postgr...
Sponsored Talk @ PGConf APAC 2018 - Choosing the right partner in your Postgr...Sponsored Talk @ PGConf APAC 2018 - Choosing the right partner in your Postgr...
Sponsored Talk @ PGConf APAC 2018 - Choosing the right partner in your Postgr...PGConf APAC
 
PGConf APAC 2018 - A PostgreSQL DBAs Toolbelt for 2018
PGConf APAC 2018 - A PostgreSQL DBAs Toolbelt for 2018PGConf APAC 2018 - A PostgreSQL DBAs Toolbelt for 2018
PGConf APAC 2018 - A PostgreSQL DBAs Toolbelt for 2018PGConf APAC
 
PGConf APAC 2018 - Patroni: Kubernetes-native PostgreSQL companion
PGConf APAC 2018 - Patroni: Kubernetes-native PostgreSQL companionPGConf APAC 2018 - Patroni: Kubernetes-native PostgreSQL companion
PGConf APAC 2018 - Patroni: Kubernetes-native PostgreSQL companionPGConf APAC
 
PGConf APAC 2018 - High performance json postgre-sql vs. mongodb
PGConf APAC 2018 - High performance json  postgre-sql vs. mongodbPGConf APAC 2018 - High performance json  postgre-sql vs. mongodb
PGConf APAC 2018 - High performance json postgre-sql vs. mongodbPGConf APAC
 
PGConf APAC 2018 - Monitoring PostgreSQL at Scale
PGConf APAC 2018 - Monitoring PostgreSQL at ScalePGConf APAC 2018 - Monitoring PostgreSQL at Scale
PGConf APAC 2018 - Monitoring PostgreSQL at ScalePGConf APAC
 
PGConf APAC 2018 - Where's Waldo - Text Search and Pattern in PostgreSQL
PGConf APAC 2018 - Where's Waldo - Text Search and Pattern in PostgreSQLPGConf APAC 2018 - Where's Waldo - Text Search and Pattern in PostgreSQL
PGConf APAC 2018 - Where's Waldo - Text Search and Pattern in PostgreSQLPGConf APAC
 
PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...
PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...
PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...PGConf APAC
 
PGConf APAC 2018 - PostgreSQL HA with Pgpool-II and whats been happening in P...
PGConf APAC 2018 - PostgreSQL HA with Pgpool-II and whats been happening in P...PGConf APAC 2018 - PostgreSQL HA with Pgpool-II and whats been happening in P...
PGConf APAC 2018 - PostgreSQL HA with Pgpool-II and whats been happening in P...PGConf APAC
 
PGConf APAC 2018 - PostgreSQL performance comparison in various clouds
PGConf APAC 2018 - PostgreSQL performance comparison in various cloudsPGConf APAC 2018 - PostgreSQL performance comparison in various clouds
PGConf APAC 2018 - PostgreSQL performance comparison in various cloudsPGConf APAC
 
Sponsored Talk @ PGConf APAC 2018 - Migrating Oracle to EDB Postgres Approach...
Sponsored Talk @ PGConf APAC 2018 - Migrating Oracle to EDB Postgres Approach...Sponsored Talk @ PGConf APAC 2018 - Migrating Oracle to EDB Postgres Approach...
Sponsored Talk @ PGConf APAC 2018 - Migrating Oracle to EDB Postgres Approach...PGConf APAC
 
PGConf APAC 2018 - Tale from Trenches
PGConf APAC 2018 - Tale from TrenchesPGConf APAC 2018 - Tale from Trenches
PGConf APAC 2018 - Tale from TrenchesPGConf APAC
 
PGConf APAC 2018 Keynote: PostgreSQL goes eleven
PGConf APAC 2018 Keynote: PostgreSQL goes elevenPGConf APAC 2018 Keynote: PostgreSQL goes eleven
PGConf APAC 2018 Keynote: PostgreSQL goes elevenPGConf APAC
 
Amazon (AWS) Aurora
Amazon (AWS) AuroraAmazon (AWS) Aurora
Amazon (AWS) AuroraPGConf APAC
 

More from PGConf APAC (17)

PGConf APAC 2018: Sponsored Talk by Fujitsu - The growing mandatory requireme...
PGConf APAC 2018: Sponsored Talk by Fujitsu - The growing mandatory requireme...PGConf APAC 2018: Sponsored Talk by Fujitsu - The growing mandatory requireme...
PGConf APAC 2018: Sponsored Talk by Fujitsu - The growing mandatory requireme...
 
PGConf APAC 2018: PostgreSQL 10 - Replication goes Logical
PGConf APAC 2018: PostgreSQL 10 - Replication goes LogicalPGConf APAC 2018: PostgreSQL 10 - Replication goes Logical
PGConf APAC 2018: PostgreSQL 10 - Replication goes Logical
 
PGConf APAC 2018 - Lightening Talk #3: How To Contribute to PostgreSQL
PGConf APAC 2018 - Lightening Talk #3: How To Contribute to PostgreSQLPGConf APAC 2018 - Lightening Talk #3: How To Contribute to PostgreSQL
PGConf APAC 2018 - Lightening Talk #3: How To Contribute to PostgreSQL
 
PGConf APAC 2018 - Lightening Talk #2 - Centralizing Authorization in PostgreSQL
PGConf APAC 2018 - Lightening Talk #2 - Centralizing Authorization in PostgreSQLPGConf APAC 2018 - Lightening Talk #2 - Centralizing Authorization in PostgreSQL
PGConf APAC 2018 - Lightening Talk #2 - Centralizing Authorization in PostgreSQL
 
Sponsored Talk @ PGConf APAC 2018 - Choosing the right partner in your Postgr...
Sponsored Talk @ PGConf APAC 2018 - Choosing the right partner in your Postgr...Sponsored Talk @ PGConf APAC 2018 - Choosing the right partner in your Postgr...
Sponsored Talk @ PGConf APAC 2018 - Choosing the right partner in your Postgr...
 
PGConf APAC 2018 - A PostgreSQL DBAs Toolbelt for 2018
PGConf APAC 2018 - A PostgreSQL DBAs Toolbelt for 2018PGConf APAC 2018 - A PostgreSQL DBAs Toolbelt for 2018
PGConf APAC 2018 - A PostgreSQL DBAs Toolbelt for 2018
 
PGConf APAC 2018 - Patroni: Kubernetes-native PostgreSQL companion
PGConf APAC 2018 - Patroni: Kubernetes-native PostgreSQL companionPGConf APAC 2018 - Patroni: Kubernetes-native PostgreSQL companion
PGConf APAC 2018 - Patroni: Kubernetes-native PostgreSQL companion
 
PGConf APAC 2018 - High performance json postgre-sql vs. mongodb
PGConf APAC 2018 - High performance json  postgre-sql vs. mongodbPGConf APAC 2018 - High performance json  postgre-sql vs. mongodb
PGConf APAC 2018 - High performance json postgre-sql vs. mongodb
 
PGConf APAC 2018 - Monitoring PostgreSQL at Scale
PGConf APAC 2018 - Monitoring PostgreSQL at ScalePGConf APAC 2018 - Monitoring PostgreSQL at Scale
PGConf APAC 2018 - Monitoring PostgreSQL at Scale
 
PGConf APAC 2018 - Where's Waldo - Text Search and Pattern in PostgreSQL
PGConf APAC 2018 - Where's Waldo - Text Search and Pattern in PostgreSQLPGConf APAC 2018 - Where's Waldo - Text Search and Pattern in PostgreSQL
PGConf APAC 2018 - Where's Waldo - Text Search and Pattern in PostgreSQL
 
PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...
PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...
PGConf APAC 2018 - Managing replication clusters with repmgr, Barman and PgBo...
 
PGConf APAC 2018 - PostgreSQL HA with Pgpool-II and whats been happening in P...
PGConf APAC 2018 - PostgreSQL HA with Pgpool-II and whats been happening in P...PGConf APAC 2018 - PostgreSQL HA with Pgpool-II and whats been happening in P...
PGConf APAC 2018 - PostgreSQL HA with Pgpool-II and whats been happening in P...
 
PGConf APAC 2018 - PostgreSQL performance comparison in various clouds
PGConf APAC 2018 - PostgreSQL performance comparison in various cloudsPGConf APAC 2018 - PostgreSQL performance comparison in various clouds
PGConf APAC 2018 - PostgreSQL performance comparison in various clouds
 
Sponsored Talk @ PGConf APAC 2018 - Migrating Oracle to EDB Postgres Approach...
Sponsored Talk @ PGConf APAC 2018 - Migrating Oracle to EDB Postgres Approach...Sponsored Talk @ PGConf APAC 2018 - Migrating Oracle to EDB Postgres Approach...
Sponsored Talk @ PGConf APAC 2018 - Migrating Oracle to EDB Postgres Approach...
 
PGConf APAC 2018 - Tale from Trenches
PGConf APAC 2018 - Tale from TrenchesPGConf APAC 2018 - Tale from Trenches
PGConf APAC 2018 - Tale from Trenches
 
PGConf APAC 2018 Keynote: PostgreSQL goes eleven
PGConf APAC 2018 Keynote: PostgreSQL goes elevenPGConf APAC 2018 Keynote: PostgreSQL goes eleven
PGConf APAC 2018 Keynote: PostgreSQL goes eleven
 
Amazon (AWS) Aurora
Amazon (AWS) AuroraAmazon (AWS) Aurora
Amazon (AWS) Aurora
 

Recently uploaded

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdfChristopherTHyatt
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 

Recently uploaded (20)

Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 

(Ab)using 4d Indexing

  • 1. (Ab)using 4D indexing in PostGIS 2.2 with PostgreSQL 9.5 to give you the perfect match Victor Blomqvist vb@viblo.se blomqvist@tantanapp.com Tantan (探探) March 19, pgDay Asia 2016 in Singapore 1tantanapp.com
  • 2. At Tantan we use PostgreSQL & PostGIS for everything! 2tantanapp.com
  • 3. Suggesting users is difficult • How should we rank them? • How can it execute quickly? (At Tantan we need to do 1000 ranking queries per second at peak!) tantanapp.com 3
  • 4. The exciting new feature in PostGIS is this: <<->> tantanapp.com 4
  • 5. Today I will show how to take advantage of the 4th dimension! 1. Use double the amount of dimensions from 2 2. … 3. Profit! tantanapp.com 5
  • 6. We will look at 3 different properties to help our suggestion SELECT 1. Popularity 2. Age 3. Activity tantanapp.com 6
  • 7. Let’s begin with 2 dimensions tantanapp.com 7 Y (Latitude) X (Longitude)
  • 8. Table and index CREATE TABLE users ( id serial PRIMARY KEY, birthdate date, location geometry, active_time timestamp, popularity double precision ); CREATE INDEX users_location_gix ON users USING GIST (location); tantanapp.com 8
  • 9. Selecting SELECT * FROM users ORDER BY location <-> ST_MakePoint(103.8, 1.3) /* Singapore */ LIMIT 10; tantanapp.com 9
  • 10. Lets look at our first case 1. Popularity <-- 2. Age 3. Activity tantanapp.com 10
  • 11. The popularity formula Popularity = likes / (likes + dislikes) tantanapp.com 11
  • 12. Order by location and popularity WITH x AS (SELECT * FROM users ORDER BY location <-> ST_MakePoint(103.8, 1.3) LIMIT 100) SELECT * FROM x ORDER BY ST_Distance(location::geography, ST_MakePoint(103.8, 1.3)) * 1 / (popularity+1) LIMIT 10; tantanapp.com 12
  • 13. Picture of x-y-z axis tantanapp.com 13 X (Longitude) Y (Latitude) Z (Popularity)
  • 14. Adding a 3rd dimension! ALTER TABLE users ADD COLUMN loc_pop geometry; UPDATE users SET loc_pop = ST_makepoint(ST_X(location), ST_Y(location), 0.01 * 1 / (popularity+1)); CREATE INDEX users_loc_pop_gix ON users USING GIST (loc_pop gist_geometry_ops_nd); tantanapp.com 14
  • 15. To use our new 3D index we need to use the new <<->> operator <<->> — Returns the n-D distance between the centroids of A and B bounding boxes. This operand will make use of n-D GiST indexes that may be available on the geometries. It is different from other operators that use spatial indexes in that the spatial index is only used when the operator is in the ORDER BY clause. * http://postgis.net/docs/manual-2.2/geometry_distance_centroid_nd.html tantanapp.com 15
  • 16. With <<->> our query becomes SELECT * FROM users ORDER BY loc_pop <<->> ST_MakePoint(103.8, 1.3, 0) LIMIT 10; tantanapp.com 16
  • 17. Second case 1. Popularity 2. Age <-- 3. Activity tantanapp.com 17
  • 18. A quick review of our table: CREATE TABLE users ( id serial PRIMARY KEY, birthdate date, location geometry, active_time timestamp, popularity double precision ); tantanapp.com 18
  • 19. Selecting with age filter SELECT * FROM users WHERE age(birthdate) between '20 years' AND '30 years' ORDER BY location <-> ST_MakePoint(103.8, 1.3) LIMIT 10; tantanapp.com 19
  • 20. A very selective user might only want to look at 74 year olds SELECT * FROM users WHERE age(birthdate) between '74 years' AND '75 years' ORDER BY location <-> ST_MakePoint(103.8, 1.3) LIMIT 10; tantanapp.com 20
  • 21. Explain analyze of the query Limit (cost=0.41..62011.93 rows=10 width=96) (actual time=49.456..551.955 rows=10 loops=1) -> Index Scan using users_location_gix on users (cost=0.41..1103805.51 rows=178 width=96) (actual time=49.398..546.631 rows=10 loops=1) Order By: (location <-> 'XXX'::geometry) Filter: ((birthdate >= (now() - '75 years'::interval)) AND (birthdate <= (now() - '74 years'::interval))) Rows Removed by Filter: 192609 Planning time: 0.157 ms Execution time: 553.151 ms tantanapp.com 21
  • 22. This is a real problem tantanapp.com 22
  • 23. Possible solutions? • Prevent searches of restricted ages • Add a distance restriction • Add age to the geo index tantanapp.com 23
  • 24. Adding age to the geo index ALTER TABLE users ADD COLUMN loc_age geometry; UPDATE users SET loc_age = ST_makepoint(ST_X(location), ST_Y(location), Extract('year' FROM birthdate)/100000); CREATE INDEX users_loc_age_gix ON users USING GIST (loc_age gist_geometry_ops_nd); tantanapp.com 24
  • 25. Updated query SELECT * FROM users WHERE loc_age &&& ST_MakeLine( ST_MakePoint(180, 90, Extract('year' FROM Now() - interval '74 years')/100000), ST_MakePoint(-180, -90, Extract('year' FROM Now() - interval '75 years')/100000)) ORDER BY loc_age <<->> ST_MakePoint(103.8, 1.3, 0) LIMIT 10; tantanapp.com 25
  • 26. Looking at the execution plan we can see that all is good Limit (cost=0.41..8.43 rows=1 width=136) (actual time=15.294..16.082 rows=10 loops=1) -> Index Scan using users_loc_age_gix on users (cost=0.41..8.43 rows=1 width=136) (actual time=14.685..14.948 rows=10 loops=1) Index Cond: (loc_age &&& 'XXX'::geometry) Order By: (loc_age <<->> 'XXX'::geometry) Planning time: 0.332 ms Execution time: 19.053 ms tantanapp.com 26
  • 27. Looking at the result we see that something is wrong UserID Age 6827677 74 years 11 mons 7 days 1281456 75 years 15 days 1269119 73 years 7 mons 27 days 5791734 73 years 7 mons 8 days 3875002 74 years 7 mons 14 days 6373179 73 years 5 mons 8 days 3727434 74 years 7 mons 21 days 5214330 74 years 10 days 3127049 74 years 8 mons 22 days 6390900 74 years 21 days tantanapp.com 27
  • 28. Solution: Keep the non-geometry where statement SELECT * FROM users WHERE age(birthdate) BETWEEN '74 years' AND '75 years' AND loc_age &&& ST_MakeLine( ST_MakePoint(180, 90, Extract('year' FROM Now() - interval '74 years')/100000), ST_MakePoint(-180, -90, Extract('year' FROM Now() - interval '75 years')/100000)) ORDER BY loc_age <<->> ST_MakePoint(103.8, 1.3, 0) LIMIT 10; tantanapp.com 28
  • 29. Finally we look at Activity 1. Popularity 2. Age 3. Activity <-- tantanapp.com 29
  • 30. Order by last active time WITH x AS (SELECT * FROM users ORDER BY location <-> ST_MakePoint(103.8, 1.3) LIMIT 100) SELECT * FROM x ORDER BY ST_Distance(location::geography, ST_MakePoint(103.8, 1.3)) * Extract(Now() - active_time) LIMIT 10; tantanapp.com 30
  • 31. What is the difference between time, popularity and age? • Popularity is bounded, a users popularity can range between 0 and 1. • Age is also bounded, and static. All our users are between 16 and 100 years old and their birthdate never change. • Time increase infinitely in one direction. tantanapp.com 31
  • 32. Adding time to the geo column ALTER TABLE users ADD COLUMN loc_active geometry; UPDATE users SET loc_active = ST_MakePoint(ST_X(location), ST_Y(location), Extract('epoch' FROM active_time) / 60 / 10000); CREATE INDEX users_loc_active_gix ON users USING GIST (loc_active gist_geometry_ops_nd); tantanapp.com 32
  • 33. Select with time becomes SELECT * FROM users ORDER BY loc_active <<->> ST_MakePoint(103.8, 1.3, Extract('epoch' FROM Now()) / 60 / 10000) LIMIT 10; tantanapp.com 33
  • 34. What we talked about so far 1. Location X and Y 2. Popularity 3. Age 4. Activity tantanapp.com 34
  • 35. Adding a 4th dimension ALTER TABLE users ADD COLUMN loc_pop_age geometry; UPDATE users SET loc_pop_age = ST_MakePoint(ST_X(location), ST_Y(location), 0.01 * 1 / (popularity+1), Extract('year' FROM birthdate)/100000); CREATE INDEX users_loc_pop_age_gix ON users USING GIST (loc_pop_age gist_geometry_ops_nd); tantanapp.com 35
  • 36. Adding a 4th dimension - Query SELECT * FROM users WHERE loc_pop_age &&& ST_MakeLine( ST_MakePoint(180, 90, -100, Extract('year' FROM Now() - interval '20 years')/100000), ST_MakePoint(-180, -90, 100, Extract('year' FROM Now() - interval '30 years')/100000)) ORDER BY loc_pop_age <<->> ST_MakePoint(103.8, 1.3, 0, 0) LIMIT 10; tantanapp.com 36
  • 37. Query runtimes 2D vs 3D vs 4D tantanapp.com 37 0 10 20 30 40 50 Location Location + popularity Location + age (20-30) Location + age (74) Location + activity Location + pop + age (4D) 3D/4D index (ms) 2D index (ms)
  • 38. Query times did not impress. What about index sizes? tantanapp.com 38 0 50 100 150 200 users_pkey users_location_gix users_loc_pop_gix users_loc_pop_age_gix users_loc_age_gix users_loc_active_gix Size bloated (MB) Size fresh(MB)
  • 39. Conclusion 1. Popularity • Pro: Improves ranking? • Con: Makes it more difficult to reason about the ranking formula • Con: Makes the query slower 2. Age • Pro: Fixes query time of outlier queries • Con: Make the average query time longer • Con: Have to take special care to not disturb the normal ranking 3. Time • Pro: Improves ranking? • Con: Much more difficult to reason about the ranking formula • Con: Makes the query slower tantanapp.com 39
  • 40. Conclusion Its more difficult to use the 3rd and 4th dimension than what a quick glance reveals. Test extensively! tantanapp.com 40