Efficient k-NN Trajectory Queries on Mobility Databases
Abstract
1. Introduction
2. Related Work
2.1. Nearest Neighbor Search on PostgreSQL
Existing PostgreSQL k-NN Strategies
2.2. Nearest Neighbor for Trajectories
2.3. Trajectory Joins
3. Preliminaries
3.1. PostgreSQL Storage Model and Type Extensibility
3.2. Problem Statement
4. Methodology
4.1. k-NN Query Design
- Query 1 (Spatial): For every trajectory in Trip, return the five trajectories closest to the point POINT(10 10):
- Query 2 (Geometry Join): For every trajectory in Trip, return the five points in POI that are nearest to that trajectory:
- Query 3 (Trajectory Join): For every trajectory in Trip, return the five other trajectories that are closest to it:
4.2. Naive UDA Implementation
5. Efficient k-NN Algorithms
5.1. Deferred k-NN Approach
| Algorithm 1 Naive k-NN (kPQ, mpid, geo, k) |
|
| Algorithm 2 Deferred k-NN |
|
5.2. Materialized k-NN Approach
| Algorithm 3 Materialized k-NN (spatial) |
|
- Cost Model: The overall cost of each approach can be approximated using the following expressions, where T denotes the number of tuples scanned, N the number of pages accessed during the table scan, and the selectivity factor of the spatial filter (i.e., the fraction of tuples whose MBRs intersect the query window). Each constant represents a specific component of the query execution cost: denotes the I/O cost per sequential page read, the CPU cost of computing one trajectory–geometry distance, the cost of maintaining the k-priority queue, the per-row accumulator overhead in the UDA, and the cost of testing intersection between two MBRs [48,49].
6. Experiments
6.1. Experimental Setup
6.2. k-NN Spatial Query
6.3. k-NN Geometry–Join Query
6.4. k-NN Trajectory Join Query
7. Discussion
Limitations
8. Conclusions
Author Contributions
Funding
Data Availability Statement
Conflicts of Interest
References
- Zheng, Y. Trajectory data mining: An overview. Acm Trans. Intell. Syst. Technol. TIST 2015, 6, 1–41. [Google Scholar] [CrossRef]
- Alsahfi, T.; Almotairi, M.; Elmasri, R. A survey on trajectory data warehouse. Spat. Inf. Res. 2020, 28, 53–66. [Google Scholar] [CrossRef]
- Güting, R.H.; Schneider, M. Moving Objects Databases; Elsevier: Amsterdam, The Netherlands, 2005. [Google Scholar]
- Nam, K.W.; Lee, J.H.; Lee, S.H.; Lee, J.W.; Park, J.H. Developing a main memory moving objects DBMS for high-performance location-based services. In Proceedings of the Asia-Pacific Web Conference, Hangzhou, China, 14–17 April 2004; Springer: Berlin/Heidelberg, Germany, 2004; pp. 864–873. [Google Scholar]
- Nam, K.W.; Yang, K. RealROI: Discovering Real Regions of Interest From Geotagged Photos. IEEE Access 2022, 10, 83489–83497. [Google Scholar] [CrossRef]
- Ghosh, S.; Ghosh, S.K.; Buyya, R. MARIO: A spatio-temporal data mining framework on Google Cloud to explore mobility dynamics from taxi trajectories. J. Netw. Comput. Appl. 2020, 164, 102692. [Google Scholar] [CrossRef]
- Ali Abbaspour, R.; Shaeri, M.; Chehreghan, A. A method for similarity measurement in spatial trajectories. Spat. Inf. Res. 2017, 25, 491–500. [Google Scholar] [CrossRef]
- Jae, L.E.; Ryu, K.H.; Nam, K.W. Indexing for efficient managing current and past trajectory of moving object. In Proceedings of the Asia-Pacific Web Conference, Hangzhou, China, 14–17 April 2004; Springer: Berlin/Heidelberg, Germany, 2004; pp. 782–787. [Google Scholar]
- Tian, J.; Ding, W.; Wu, C.; Nam, K.W. A generalized approach for anomaly detection from the Internet of moving things. IEEE Access 2019, 7, 144972–144982. [Google Scholar] [CrossRef]
- Farahnakiyan, M.H.; Esmaeilyfard, R.; Javidan, R. A proactive privacy-preserving framework for mobile trajectory sharing. J. Netw. Comput. Appl. 2025, 242, 104271. [Google Scholar] [CrossRef]
- Liao, D.; Li, H.; Sun, G.; Zhang, M.; Chang, V. Location and trajectory privacy preservation in 5G-Enabled vehicle social network services. J. Netw. Comput. Appl. 2018, 110, 108–118. [Google Scholar] [CrossRef]
- Hellerstein, J.M.; Naughton, J.F.; Pfeffer, A. Generalized search trees for database systems. In Proceedings of the 21st International Conference on Very Large Data Bases (VLDB’95), Zurich, Switzerland, 11–15 September 1995; pp. 562–573. [Google Scholar]
- Güting, R.H.; Behr, T.; Xu, J. Efficient k-nearest neighbor search on moving object trajectories. VLDB J. 2010, 19, 687–714. [Google Scholar] [CrossRef]
- Cohen, S. User-defined aggregate functions: Bridging theory and practice. In Proceedings of the 2006 ACM SIGMOD International Conference on Management of Data, Chicago, IL, USA, 27–29 June 2006; pp. 49–60. [Google Scholar]
- Zimányi, E.; Sakr, M.; Lesuisse, A. MobilityDB: A mobility database based on PostgreSQL and PostGIS. ACM Trans. Database Syst. TODS 2020, 45, 1–42. [Google Scholar] [CrossRef]
- Yoo, K.; Yang, P.W.; Nam, K.W. Design of Moving Object Query Processing Based on UDF. KIPS Trans. Softw. Data Eng. 2017, 6, 85–90. [Google Scholar] [CrossRef][Green Version]
- Lou, L.; Nam, K.W. GitHub-Awarematics/UrbanSQL—github.com. 2012. Available online: https://github.com/awarematics/UrbanSQL (accessed on 20 November 2025).
- Lew, D.J.; Yoo, K.; Nam, K.W. DeepVQL: Deep Video Queries on PostgreSQL. Proc. VLDB Endow. 2023, 16, 3910–3913. [Google Scholar] [CrossRef]
- Group, P.G.D. PostgreSQL—postgresql.org. 2022. Available online: https://postgresql.org (accessed on 20 November 2025).
- Feng, R. “KNN Ultimate Optimization: From RDS to PostGIS”. 2018. Available online: https://vonng.com/en/pg/knn-optimize/ (accessed on 20 November 2025).
- Korotkov, A. GitHub-postgrespro/imgsmlr: Similar Images Search for PostgreSQL—github.com. 2019. Available online: https://github.com/postgrespro/imgsmlr (accessed on 20 November 2025).
- PostgreSQL. F.9. Cube—Postgresql.org. 2019. Available online: https://www.postgresql.org/docs/current/cube.html (accessed on 20 November 2025).
- Günther, M. Freddy: Fast word embeddings in database systems. In Proceedings of the 2018 International Conference on Management of Data (SIGMOD’18), Houston, TX, USA, 10–15 June 2018; pp. 1817–1819. [Google Scholar]
- Yang, W.; Li, T.; Fang, G.; Wei, H. PASE: PostgreSQL Ultra-High-Dimensional Approximate Nearest Neighbor Search Extension. In Proceedings of the 2020 ACM SIGMOD International Conference on Management of Data, Portland, OR, USA, 14–19 June 2020; pp. 2241–2253. [Google Scholar]
- Eltabakh, M.Y.; Eltarras, R.; Aref, W.G. Space-partitioning trees in PostgreSQL: Realization and performance. In Proceedings of the 22nd International Conference on Data Engineering (ICDE’06), Atlanta, GA, USA, 3–7 April 2006; IEEE: Piscataway, NJ, USA, 2006; p. 100. [Google Scholar]
- Donkó, I.; Szalai-Gindl, J.M.; Gombos, G.; Kiss, A. An implementation of the M-tree index structure for PostgreSQL using GiST. In Proceedings of the 2019 IEEE 15th International Scientific Conference on Informatics, Poprad, Slovakia, 20–22 November 2019; IEEE: Piscataway, NJ, USA, 2019; pp. 000189–000194. [Google Scholar]
- Ciaccia, P.; Patella, M.; Zezula, P. M-tree: An efficient access method for similarity search in metric spaces. In Proceedings of the 23rd International Conference on Very Large Data Bases (VLDB’97), Athens, Greece, 25–29 August 1997; pp. 426–435. [Google Scholar]
- Rslan, E.; Abdelhameed, H.; Ezzat, E. An efficient hybridized index technique for moving object database. Spat. Inf. Res. 2018, 26, 551–561. [Google Scholar] [CrossRef]
- Priya, M.; Kalpana, R. Improving the performance of location based spatial textual query processing using distributed strip index. Spat. Inf. Res. 2019, 27, 565–571. [Google Scholar] [CrossRef]
- Pgvector developers. GitHub-Pgvector/Pgvector—github.com. 2023. Available online: https://github.com/pgvector/pgvector (accessed on 20 November 2025).
- Roussopoulos, N.; Kelley, S.; Vincent, F. Nearest neighbor queries. In Proceedings of the 1995 ACM SIGMOD International Conference on Management of Data, San Jose, CA, USA, 22–25 May 1995; ACM: New York, NY, USA, 1995; pp. 71–79. [Google Scholar]
- Cheung, K.L.; Fu, A.W.C. Enhanced nearest neighbour search on the R-tree. ACM Sigmod Rec. 1998, 27, 16–21. [Google Scholar] [CrossRef]
- Frentzos, E.; Gratsias, K.; Pelekis, N.; Theodoridis, Y. Algorithms for nearest neighbor search on moving object trajectories. Geoinformatica 2007, 11, 159–193. [Google Scholar] [CrossRef]
- Hjaltason, G.R.; Samet, H. Distance browsing in spatial databases. ACM Trans. Database Syst. TODS 1999, 24, 265–318. [Google Scholar] [CrossRef]
- Song, Z.; Roussopoulos, N. K-nearest neighbor search for moving query point. In Proceedings of the International Symposium on Spatial and Temporal Databases, Redondo Beach, CA, USA, 12–15 July 2001; Springer: Berlin/Heidelberg, Germany, 2001; pp. 79–96. [Google Scholar]
- Tao, Y.; Papadias, D.; Shen, Q. Continuous nearest neighbor search. In Proceedings of the VLDB’02: Proceedings of the 28th International Conference on Very Large Databases, Hong Kong, China, 20–23 August 2002; Elsevier: Amsterdam, The Netherlands, 2002; pp. 287–298. [Google Scholar]
- Frentzos, E.; Pelekis, N.; Ntoutsi, I.; Theodoridis, Y. Trajectory database systems. In Mobility, Data Mining and Privacy; Springer: Berlin/Heidelberg, Germany, 2008; pp. 151–187. [Google Scholar]
- Ferhatosmanoglu, H.; Stanoi, I.; Agrawal, D.; El Abbadi, A. Constrained nearest neighbor queries. In Proceedings of the International Symposium on Spatial and Temporal Databases, Redondo Beach, CA, USA, 12–15 July 2001; Springer: Berlin/Heidelberg, Germany, 2001; pp. 257–276. [Google Scholar]
- Papadias, D.; Shen, Q.; Tao, Y.; Mouratidis, K. Group nearest neighbor queries. In Proceedings of the 20th International Conference on Data Engineering, Boston, MA, USA, 2 April 2004; IEEE: Piscataway, NJ, USA, 2004; pp. 301–312. [Google Scholar]
- Deng, K.; Shen, H.T.; Xu, K.; Lin, X. Surface k-NN query processing. In Proceedings of the 22nd International Conference on Data Engineering (ICDE’06), Atlanta, GA, USA, 3–8 April 2006; IEEE: Piscataway, NJ, USA, 2006; p. 78. [Google Scholar]
- Korn, F.; Muthukrishnan, S. Influence sets based on reverse nearest neighbor queries. ACM Sigmod Rec. 2000, 29, 201–212. [Google Scholar] [CrossRef]
- Zhang, J.; Mamoulis, N.; Papadias, D.; Tao, Y. All-nearest-neighbors queries in spatial databases. In Proceedings of the 16th International Conference on Scientific and Statistical Database Management, Santorini Island, Greece, 21–23 June 2004; IEEE: Piscataway, NJ, USA, 2004; pp. 297–306. [Google Scholar]
- Chang, Y.; Tanin, E.; Cong, G.; Jensen, C.S.; Qi, J. Trajectory similarity measurement: An efficiency perspective. arXiv 2023, arXiv:2311.00960. [Google Scholar] [CrossRef]
- Fan, C.; Ye, Y.; Yao, H.; Wu, Y.; Liu, K.; Zhou, S.; Li, S. Activity Semantics Embedding-Based Trajectory Similarity Computation. Trans. GIS 2025, 29, e70085. [Google Scholar] [CrossRef]
- Luo, S.; Zeng, W.; Sun, B. Contrastive learning for graph-based vessel trajectory similarity computation. J. Mar. Sci. Eng. 2023, 11, 1840. [Google Scholar] [CrossRef]
- Bakalov, P.; Hadjieleftheriou, M.; Keogh, E.J.; Tsotras, V.J. Efficient Trajectory Joins Using Symbolic Representations. In Proceedings of the IEEE International Conference on Mobile Data Management (MDM), Ayia Napa, Cyprus, 9–13 May 2005; IEEE: Piscataway, NJ, USA, 2005; pp. 86–93. [Google Scholar]
- Arumugam, S.; Jermaine, C. Closest-Point-of-Approach Join for Moving Object Histories. In Proceedings of the 22nd IEEE International Conference on Data Engineering (ICDE), Atlanta, GA, USA, 3–8 April 2006; IEEE: Piscataway, NJ, USA, 2006; p. 86. [Google Scholar]
- Aboulnaga, A.; Naughton, J.F. Accurate estimation of the cost of spatial selections. In Proceedings of the 16th International Conference on Data Engineering (Cat. No. 00CB37073), San Diego, CA, USA, 28 February–3 March 2000; IEEE: Piscataway, NJ, USA, 2000; pp. 123–134. [Google Scholar]
- Zhang, D.; Tsotras, V.J. Optimizing spatial min/max aggregations. VLDB J. 2005, 14, 170–181. [Google Scholar] [CrossRef]
- Düntgen, C.; Behr, T.; Güting, R.H. BerlinMOD: A benchmark for moving object databases. VLDB J. 2009, 18, 1335–1368. [Google Scholar] [CrossRef]










| Name | Year | Description |
|---|---|---|
| ImgSmlr | 2017 | image-scene retrieval |
| Cube | 2006 | high-dimensional vector data retrieval |
| Freddy | 2017 | approximate-NN retrieval |
| Pase | 2020 | high-dimensional and approximate-NN search |
| MobilityDB | 2020 | supports spatio-temporal queries |
| Our work | 2025 | supports spatio-temporal queries and UDAs |
| Description | Our Query | MobilityDB |
|---|---|---|
| k-NN spatial query | SELECT m_knn(t.traj,’POINT (10 10)’,5) FROM Trip t | SELECT T.CarId, trajectory(T.Trip <—>’POINT (10 10)’) AS MinDistance FROM Trip T ORDER BY MinDistance LIMIT 5 |
| k-NN geometry join query | SELECT m_knn(t.traj,p.geo,5) FROM Trip t, POI p | WITH TripsTraj AS ( SELECT *, Trajectory(Trip) AS Trajectory FROM Trip ) SELECT T.CarId, P1.PointId, P1.Distance FROM TripsTraj T CROSS JOIN LATERAL( SELECT P.PointId, T.Trajectory <—>P.Geom AS Distance FROM Points P ORDER BY Distance LIMIT 5 ) AS P1 ORDER BY T.TripId, T.CarId, P1.Distance; |
| k-NN trajectory join query | SELECT m_knn(t1.traj,t2.traj,5) FROM Trip t1, Trip t2 | SELECT T1.CarId AS CarId1, C2.CarId AS CarId2, C2.Distance FROM Trip T1 CROSS JOIN LATERAL( SELECT T2.CarId, minValue(T1.Trip <—>T2.Trip) AS Distance FROM Trip T2 WHERE T1.CarId < T2.CarId ORDER BY Distance LIMIT 5 ) AS C2 ORDER BY T1.CarId, C2.CarId; |
| Scale Factor | Days | Vehicles | Trajectories | Unit (GB) |
|---|---|---|---|---|
| 0.005 | 2 | 141 | 1.8 K | 0.026 GB |
| 0.05 | 6 | 447 | 15 K | 0.26 GB |
| 0.2 | 13 | 894 | 62.5 K | 0.99 GB |
| 1.0 | 28 | 2000 | 292.9 K | 4.9 GB |
Disclaimer/Publisher’s Note: The statements, opinions and data contained in all publications are solely those of the individual author(s) and contributor(s) and not of MDPI and/or the editor(s). MDPI and/or the editor(s) disclaim responsibility for any injury to people or property resulting from any ideas, methods, instructions or products referred to in the content. |
© 2025 by the authors. Published by MDPI on behalf of the International Society for Photogrammetry and Remote Sensing. Licensee MDPI, Basel, Switzerland. This article is an open access article distributed under the terms and conditions of the Creative Commons Attribution (CC BY) license (https://creativecommons.org/licenses/by/4.0/).
Share and Cite
Lou, L.; Lew, D.J.; Nam, K.W. Efficient k-NN Trajectory Queries on Mobility Databases. ISPRS Int. J. Geo-Inf. 2025, 14, 458. https://doi.org/10.3390/ijgi14120458
Lou L, Lew DJ, Nam KW. Efficient k-NN Trajectory Queries on Mobility Databases. ISPRS International Journal of Geo-Information. 2025; 14(12):458. https://doi.org/10.3390/ijgi14120458
Chicago/Turabian StyleLou, Linghui, Dong June Lew, and Kwang Woo Nam. 2025. "Efficient k-NN Trajectory Queries on Mobility Databases" ISPRS International Journal of Geo-Information 14, no. 12: 458. https://doi.org/10.3390/ijgi14120458
APA StyleLou, L., Lew, D. J., & Nam, K. W. (2025). Efficient k-NN Trajectory Queries on Mobility Databases. ISPRS International Journal of Geo-Information, 14(12), 458. https://doi.org/10.3390/ijgi14120458

