SlideShare a Scribd company logo
(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

PDF
Big Data and PostgreSQL
PDF
PostgreSQL 9.6 Performance-Scalability Improvements
PDF
PostgreSQL on Amazon RDS
PDF
PostgreSQL WAL for DBAs
PDF
Go Faster With Native Compilation
PDF
Migration From Oracle to PostgreSQL
PDF
PostgreSQL: Past present Future
PDF
Swapping Pacemaker Corosync with repmgr
Big Data and PostgreSQL
PostgreSQL 9.6 Performance-Scalability Improvements
PostgreSQL on Amazon RDS
PostgreSQL WAL for DBAs
Go Faster With Native Compilation
Migration From Oracle to PostgreSQL
PostgreSQL: Past present Future
Swapping Pacemaker Corosync with repmgr

Viewers also liked (20)

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

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

PDF
Non-Relational Postgres / Bruce Momjian (EnterpriseDB)
PDF
Non-Relational Postgres
 
ODP
MySQL and GIS Programming
PDF
Spatial query on vanilla databases
PDF
Accelerating Local Search with PostgreSQL (KNN-Search)
PDF
Comparing Geospatial Implementation in MongoDB, Postgres, and Elastic
PDF
On Beyond (PostgreSQL) Data Types
DOCX
JAVA 2013 IEEE DATAMINING PROJECT Fast nearest neighbor search with keywords
DOCX
Fast nearest neighbor search with keywords
PDF
Advanced query optimization
PPTX
Indexes: The Second Pillar of Database Wisdom
PDF
Indexing Complex PostgreSQL Data Types
PDF
Efficient spatial queries on vanilla databases
PDF
Cassandra Community Webinar | Become a Super Modeler
PDF
Become a super modeler
PDF
Database Design most common pitfalls
PDF
Real-Time Analytics at Uber Scale
DOC
IEEE 2014 JAVA DATA MINING PROJECTS Fast nearest neighbor search with keywords
PPT
Sample document
PPT
Geo distance search with my sql presentation
Non-Relational Postgres / Bruce Momjian (EnterpriseDB)
Non-Relational Postgres
 
MySQL and GIS Programming
Spatial query on vanilla databases
Accelerating Local Search with PostgreSQL (KNN-Search)
Comparing Geospatial Implementation in MongoDB, Postgres, and Elastic
On Beyond (PostgreSQL) Data Types
JAVA 2013 IEEE DATAMINING PROJECT Fast nearest neighbor search with keywords
Fast nearest neighbor search with keywords
Advanced query optimization
Indexes: The Second Pillar of Database Wisdom
Indexing Complex PostgreSQL Data Types
Efficient spatial queries on vanilla databases
Cassandra Community Webinar | Become a Super Modeler
Become a super modeler
Database Design most common pitfalls
Real-Time Analytics at Uber Scale
IEEE 2014 JAVA DATA MINING PROJECTS Fast nearest neighbor search with keywords
Sample document
Geo distance search with my sql presentation
Ad

More from PGConf APAC (17)

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

Recently uploaded (20)

PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Spectral efficient network and resource selection model in 5G networks
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPT
Teaching material agriculture food technology
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PPTX
Programs and apps: productivity, graphics, security and other tools
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Network Security Unit 5.pdf for BCA BBA.
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PPTX
Spectroscopy.pptx food analysis technology
PDF
cuic standard and advanced reporting.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Big Data Technologies - Introduction.pptx
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
Diabetes mellitus diagnosis method based random forest with bat algorithm
Spectral efficient network and resource selection model in 5G networks
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Per capita expenditure prediction using model stacking based on satellite ima...
Teaching material agriculture food technology
Advanced methodologies resolving dimensionality complications for autism neur...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Programs and apps: productivity, graphics, security and other tools
Digital-Transformation-Roadmap-for-Companies.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Network Security Unit 5.pdf for BCA BBA.
“AI and Expert System Decision Support & Business Intelligence Systems”
Spectroscopy.pptx food analysis technology
cuic standard and advanced reporting.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Big Data Technologies - Introduction.pptx
Dropbox Q2 2025 Financial Results & Investor Presentation
Agricultural_Statistics_at_a_Glance_2022_0.pdf
ACSFv1EN-58255 AWS Academy Cloud Security Foundations.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy

(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