'How do I optimize hierarchical query
I've been trying to run my query but it always seem to be running like forever. Would like to know how can I Improve on performance for this Had to add in the group by Clause as the left join seem to be creating duplication when viewing the results.
Hierarchical Query Code
with NextValue (equip_id, link_id, node_fr_id, node_to_id ) as
(
SELECT lm.equipment_id, l.link_id, l.node_fr_id_num, l.node_to_id_num
FROM road_details l
LEFT JOIN Equipment_details e ON l.link_id = e.link_id
Where 1=1
START WITH l.link_id = 2
CONNECT By NOCYCLE Prior l.node_to_id_num = l.node_fr_id_num
)
Select * from NextValue
-- Select Particular Equipment starting with 'DET' --
Where 1=1 and equip_id like 'DET%'
-- Remove Duplicate cause by Join clause --
Group by equip_id, link_id, node_fr_id, node_to_id;
Expected output:
| equip_id | link_id | node_fr_id | node_to_id |
|---|---|---|---|
| DET_276627 | 990 | 1138 | 1141 |
| DET_546012 | 1881 | 4856 | 2322 |
| DET_546010 | 1980 | 2397 | 2437 |
| DET_526526 | 13176 | 14259 | 14260 |
| DET_526014 | 13724 | 14762 | 14763 |
| DET_536513 | 114721 | 114445 | 114446 |
| DET_526524 | 106130 | 105001 | 106402 |
| DET_516043 | 18803 | 19196 | 19198 |
| DET_526024 | 106319 | 106753 | 105919 |
| DET_526515 | 105796 | 105762 | 103751 |
| DET_526518 | 1061319 | 106514 | 104601 |
| DET_526037 | 99221 | 96871 | 94382 |
| DET_526510 | 99185 | 97133 | 97881 |
Current output:
| equip_id | link_id | node_fr_id | node_to_id |
|---|---|---|---|
| DET_276627 | 990 | 1138 | 1141 |
| DET_546012 | 1881 | 4856 | 2322 |
| DET_546010 | 1980 | 2397 | 2437 |
| DET_526526 | 13176 | 14259 | 14260 |
| DET_526014 | 13724 | 14762 | 14763 |
| DET_536513 | 114721 | 114445 | 114446 |
| DET_526524 | 106130 | 105001 | 106402 |
| DET_516043 | 18803 | 19196 | 19198 |
| DET_526024 | 106319 | 106753 | 105919 |
| DET_526515 | 105796 | 105762 | 103751 |
| DET_526518 | 1061319 | 106514 | 104601 |
| DET_526518 | 1061319 | 106514 | 104601 |
| DET_526518 | 1061319 | 106514 | 104601 |
| DET_526518 | 1061319 | 106514 | 104601 |
| DET_526518 | 1061319 | 106514 | 104601 |
| DET_526518 | 1061319 | 106514 | 104601 |
| DET_526518 | 1061319 | 106514 | 104601 |
| DET_526037 | 99221 | 96871 | 94382 |
| DET_526510 | 99185 | 97133 | 97881 |
| DET_526510 | 99185 | 97133 | 97881 |
| DET_526510 | 99185 | 97133 | 97881 |
| DET_526510 | 99185 | 97133 | 97881 |
for odd reason it creating duplicates and hence had to add in the Group clause to counter this issue.. but with the group clause added in i've notice the longer querying duration occuring.
Updates for commenters (not enough space to explain): Hi @HimanshuKandpal, let me just explain the query and what it is suppose to do. There are 2 tables, one is road_details and the other is an equipment_details. PK for the road detailsis Link_id, where equipment_id is the PK in the equipment_details and link_id is FK.
Brief explanation road details for context of what this hieratical query is aiming to do. Picture a map of inter connecting roads, e.g. a single road is made up of multiple links (Blocks) each block has a fr_id (head) and to_id (tail) [link_id 1 -> link_id 2 -> link_id 3] where link_id1.to_id = link_id2.fr_id as they are linked that way. The query is supposed to find out what is the next connecting link based on the criteria.
2nd criteria, with each link can have multiple equipment on it or non at all. If a user were to select a certain equipment type, as shown in my where clause are they able to know what is the next or previous link ID of a particular equipment with support from hieratical query.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
