Content Deep Dive
Performant database tree traversal with Rails
Blog post from PlanetScale
Post Details
Company
Date Published
Author
Mike Coutermarsh
Word Count
826
Language
English
Hacker News Points
53
Summary
We solved a performance problem in our Rails API when performing a tree traversal by creating an in-memory cache to store the snapshots and preloading it with recent snapshots from each branch. This reduced the number of database queries from thousands to just a few, solving the N+1 query problem. We also tested and refined the solution using feature flags and data on hits and misses, and considered alternative solutions such as using a recursive common table expression or materialized path in the database.