A点出发到各地的路径及其距离
来源:互联网 发布:阴氏埙授权淘宝店 编辑:程序博客网 时间:2024/05/16 14:50
求所有的可能路径及距离,样例数据如下:
- with bus as
- (
- select 1 id, 'A' dstart, 'B' dend, '120' distance from dual union all
- select 2 id, 'B' dstart, 'C' dend, '200' distance from dual union all
- select 3 id, 'C' dstart, 'E' dend, '180' distance from dual union all
- select 4 id, 'A' dstart, 'D' dend, '150' distance from dual union all
- select 5 id, 'D' dstart, 'M' dend, '300' distance from dual union all
- select 6 id, 'F' dstart, 'M' dend, '260' distance from dual
- )
- select * From bus;
实现效果:
- SUM_DISTANCE PATH
- 120 A->B
- 320 A->B->C
- 500 A->B->C->E
- 150 A->D
- 450 A->D->M
- 200 B->C
- 380 B->C->E
- 180 C->E
- 300 D->M
- 260 F->M
原始SQL代码:
- with bus as
- (
- select 1 id, 'A' dstart, 'B' dend, '120' distance from dual union all
- select 2 id, 'B' dstart, 'C' dend, '200' distance from dual union all
- select 3 id, 'C' dstart, 'E' dend, '180' distance from dual union all
- select 4 id, 'A' dstart, 'D' dend, '150' distance from dual union all
- select 5 id, 'D' dstart, 'M' dend, '300' distance from dual union all
- select 6 id, 'F' dstart, 'M' dend, '260' distance from dual
- )
- select level, a.dstart, a.dend,
- sys_connect_by_path(distance,'+') sum_distance,
- (select sum(b.distance) from bus b start with b.id=a.id connect by prior dstart = dend
- ),
- CONNECT_BY_root(a.dstart)|| sys_connect_by_path(a.dend,'->') path,
- CONNECT_BY_ISLEAF leaf
- from bus a
- connect by prior dend = dstart;
一看这个SQL语句里有一个标量子查询,数据量少的时候看不出来问题,多的时候就会出现性能问题,因此利用dbms_aw.eval_number函数处理了树形函数的返回值,优化后的SQL代码 :
- with bus as
- (
- select 1 id, 'A' dstart, 'B' dend, '120' distance from dual union all
- select 2 id, 'B' dstart, 'C' dend, '200' distance from dual union all
- select 3 id, 'C' dstart, 'E' dend, '180' distance from dual union all
- select 4 id, 'A' dstart, 'D' dend, '150' distance from dual union all
- select 5 id, 'D' dstart, 'M' dend, '300' distance from dual union all
- select 6 id, 'F' dstart, 'M' dend, '260' distance from dual
- )
- select level, a.dstart, a.dend, ltrim(sys_connect_by_path(distance,'+'),'+') distance_expression,
- dbms_aw.eval_number(ltrim(sys_connect_by_path(distance,'+'),'+')) sum_distance,
- CONNECT_BY_root(a.dstart)|| sys_connect_by_path(a.dend,'->') path,
- CONNECT_BY_ISLEAF leaf
- from bus a
- connect by prior dend = dstart;
0 0
- oracle A点出发到各地的路径及其距离
- A点出发到各地的路径及其距离
- 图论04—任意指定点到所有其它点的最短路径及距离
- 点到平面的距离
- 点到平面的距离
- 点到直线的距离
- 点到线段的距离
- 点到线段的距离
- 点到直线的距离
- 点到线段的距离
- 点到线段的距离
- 点到直线的距离
- 点到直线的距离
- 点到直线的距离
- 点到圆弧的距离
- 点到曲线的距离
- 点到平面的距离
- 点到平面的距离
- 表单重复提交
- 现代人是聪明了还是懒惰了
- 博客搬家
- Android调用支付宝快捷支付时提示"系统繁忙,请稍后再试(ALI64)"错误
- thinkphp基本操作
- A点出发到各地的路径及其距离
- Merge Intervals 这个应该叫合并间隙?
- 把数组排成最小的数
- 使用phpmailer发送邮件,且需要激活完整代码
- iOS开发:获取矩形渐变色的UIImage
- 《APP开发》APP规范实例-详细的UI设计方法
- 每天学习一算法【3】单链表翻转
- TCP与UDP的区别,以及它们各自的定义
- java中“集合”与“数组”转换