Building Graph-Based Video Relationship Queries With Apache AGE
Our most expensive query was a simple "show related videos" panel.
At ViralVidVault, we track video trends. We found that finding related videos through shared channels or co-viewed sessions killed our database performance. We tried using SQLite with recursive joins. It worked for one hop. At two hops, the data exploded. One query created hundreds of thousands of rows. Our workers started timing out.
The data is a graph. We were trying to force it into tables and paying the price.
We moved the relationship layer to Apache AGE. This is an openCypher extension for PostgreSQL. We kept our PHP 8.4 app and our SQLite store.
The results: • Related panel latency dropped from 900ms to under 40ms. • Complex two-hop traversals now take single-digit milliseconds. • Our operational workload stayed the same because AGE runs inside Postgres.
Why use Apache AGE instead of a standalone graph database?
Operational Simplicity You do not need a new database to back up or secure. AGE uses your existing Postgres setup, connection pools, and security rules.
Native Graph Queries In SQL, variable-length paths require complex recursion. In Cypher, you write them as simple patterns. A 40-line recursive SQL block became a 6-line Cypher query.
Better Performance A graph engine indexes adjacency. It stops expanding paths that do not match. This prevents the data fan-out that crashed our previous system.
A key lesson from our migration: Always index your entrypoint properties. If you do not index the ID you use to start a traversal, AGE will perform a full scan. This makes even the best graph query slow.
We use the graph as a read model. Our raw data stays in SQLite. We use a Python script to sync the two. This keeps our graph fast, lightweight, and easy to rebuild.
If your recursive SQL queries are getting too complex, do not fight the relational model. Build a small graph projection alongside your current store.
