Performance of recursive stored procedures in MYSQL to get hierarchical data

前端 未结 3 1521
死守一世寂寞
死守一世寂寞 2021-01-16 06:56

I have table employee like,
employee ( emp_id int primary key, emp_name varchar(50), mngr_id int)

and here mngr_id would either null or contain valid emp_id. Th

3条回答
  •  温柔的废话
    2021-01-16 07:34

    Regarding the last question: There are a few nice options at "What is the most efficient/elegant way to parse a flat table into a tree?"

    You should also consider caching the result of the recursion in an intermediate table. If you change that only on update to your hierarchy table, the recursion performance hit will be negligible.

    EDIT: Personally I would do the recursion in the presentation layer of my app, for example on the web server. This provides greater flexibility compared to what can be achieved in SQL, and you can also use session or application level caching. (Using a pre-constructed DB table that is kept up to date with a trigger never leaves you with an outdated cache, though.)

提交回复
热议问题