wasAsked.

How would you model and query hierarchical product categories efficiently in SQL and Entity Framework Core?

Zomato · Full Stack Developer · 3-4-years · technical

Published

Year not specified · .NET, Entity Framework Core, SQL

Community answers

Posted by wasAsked editorial collection

For shallow or frequently changing trees, an adjacency list (Id, ParentId) is simple. Query descendants with a recursive CTE and prevent cycles. Other models such as materialized paths or closure tables trade more complex writes for faster subtree reads. Example: WITH tree AS ( SELECT Id, ParentId, Name FROM Category WHERE RestaurantId=@RestaurantId AND OutletId=@OutletId AND Id=@RootId UNION ALL SELECT c.Id,c.ParentId,c.Name FROM Category c JOIN tree p ON c.ParentId=p.Id WHERE c.RestaurantId=@RestaurantId AND c.OutletId=@OutletId ) SELECT * FROM tree; Add tenant-qualified indexes, a maximum depth, cycle validation, and deterministic sibling ordering. EF Core may execute the CTE via a mapped SQL query.

Backbencher Developer · · 0 likes

There are four common ways to store a tree of categories, with different trade-offs. 1. Adjacency list (ParentId column) — simplest public class Category { public int Id; public int? ParentId; public Category? Parent; public List<Category> Children = []; public string Name = ""; } - Easy inserts and moves (change ParentId). - Loading a subtree needs a recursive query. In SQL use a recursive CTE: WITH Tree AS ( SELECT Id, ParentId, Name, 0 AS Depth FROM Categories WHERE Id = @rootId UNION ALL SELECT c.Id, c.ParentId, c.Name, t.Depth + 1 FROM Categories c JOIN Tree t ON c.ParentId = t.Id ) SELECT * FROM Tree; In EF Core, run it with FromSql, or — for small trees such as product categories — load all categories in one query and build the tree in memory; EF fixes up Parent and Children navigations automatically. 2. Materialized path — fast reads - Store a path like /1/4/17/. Subtree query: WHERE Path LIKE '/1/4/%', which can use an index. - Easy breadcrumbs; moving a subtree means updating the paths of all descendants. 3. SQL Server hierarchyid - Built-in type with methods such as GetAncestor and IsDescendantOf, plus depth-first indexes. Supported by EF Core 8+ for SQL Server. 4. Closure table — flexible - A separate table stores every ancestor and descendant pair with depth. Very fast ancestor and descendant queries at the cost of more storage and more complex writes. Choosing for product categories - Categories are small (hundreds to a few thousand rows), read-heavy and rarely change. Adjacency list plus caching the whole tree in memory or Redis is usually the best balance; invalidate the cache when an admin edits categories. - If you must filter products by "category including all subcategories" in SQL, store a materialized path, or pre-compute descendant IDs, so you can use WHERE CategoryId IN (...). Also enforce integrity: prevent cycles when moving categories, limit depth if the UI requires it, and index ParentId.

Ravindra Wadile · · 0 likes

Sign in to add your answer

Related interview topics