MySQL存储过程对链表进行排序
我有一个表示链接列表的表
id | parent
1 | 1
2 | 1
3 | 2
4 | 3
...
,我知道 mysql 没有内置用于查询的树构建功能,但我想知道是否有人使用存储过程来执行此排序。目前,我正在域代码中执行排序,但表要由外部系统读取,并且性能对外部系统至关重要,因此我想为其提供一个已排序的列表,以免使用其宝贵的 CPU 周期。
欢迎其他建议,因为这仍处于设计阶段。
I have a table with a representation of a linked list
id | parent
1 | 1
2 | 1
3 | 2
4 | 3
...
I know for a fact that mysql does not have the tree building capabilities built in for queries but I was wondering if anyone had used stored procedures to perform this sorting. Currently I am performing the sorting in my domain code but the table as to be read by an external system and performance is paramount on the external system so I would like to provide it with an already sorted list to not use its the precious CPU cycles.
Other advices are welcomed as this is still in the design phase.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
MySQL 没有 Oracle 那样方便的
START WITH / CONNECT BY
语法,但是,您可能需要查看 此链接来自试图模仿 MySQL 中相同功能的人。MySQL doesn't have the handy
START WITH / CONNECT BY
syntax that Oracle does, however, you might want to check out this link from someone who tried to mimic this same functionality in MySQL.我已经于 2011 年 10 月 24 日在 DBA StackExchange 中解决了这个问题:https://dba.stackexchange.com/questions/7147/find-highest-level-of-a-hierarchical-field-with-vs-without-ctes/7161#7161
在我的回答中,我包含了基于该问题的存储过程和示例数据。
尝试一下!
I already solved this in the DBA StackExchange on October 24, 2011 : https://dba.stackexchange.com/questions/7147/find-highest-level-of-a-hierarchical-field-with-vs-without-ctes/7161#7161
In my answer, I included the stored procedures and sample data based on that question.
Give it a Try !!!