Building Video Recommendations With SurrealDB Graph Traversal Queries
TrendVidStream, a video streaming platform, faced performance issues with its "related videos" recommendation system. The original implementation used a SQLite query with four self-joins over a views table, filtered by region and ordered by a co-occurrence score. This worked for hundreds of thousands of view rows but degraded when the platform expanded to eight regions and began ingesting trending videos per region. Query latency jumped from 30ms to 900ms, and the query planner began picking the wrong index depending on which region's data was hot. Attempts to make the query region-aware added more JOINs and WHERE clauses, further complicating optimization. The root cause was that recommendation queries are inherently graph problems, not relational ones. The query "viewers who watched A also watched B" is a two-hop traversal: video → viewers → videos. In SQL, this requires self-joins on a bridge table, and each additional hop adds another join. In a graph database like SurrealDB, it becomes a single path expression. The team migrated the recommendation logic to SurrealDB's graph traversal queries, which simplified the code and improved performance. They retained SQLite FTS5 for full-text search, as it remains excellent for that purpose and integrates well with their PHP 8.4 stack.
Graph databases can simplify complex recommendation queries that become slow with relational joins.