Maxim Mironjuk
-
February 28, 2026
Since MySQL 8, tree structures and other hierarchical data can be resolved directly in SQL through WITH RECURSIVE, without needing recursive application logic or a fixed number of self joins. The basic syntax follows the SQL standard, but MySQL brings a few implementation details of its own, from the internal handling as a UNION loop to the configurable infinite loop protection through cte_max_recursion_depth. This article covers how recursive CTEs work internally in MySQL, where the typical pitfalls sit, and how a Magento category tree can be resolved with one in practice, performantly.