Skip to content

Commit 2c51d91

Browse files
fix shortest queries
1 parent b07fd09 commit 2c51d91

File tree

1 file changed

+34
-35
lines changed

1 file changed

+34
-35
lines changed

modules/ROOT/pages/patterns/shortest-paths.adoc

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ Without `CALL`, the planner does not know how many targets there are per source
572572
----
573573
PROFILE
574574
MATCH p = ANY SHORTEST
575-
(source:N {trail: ["C", "C", "A", "C", "A", "B", "B", "B", "A"]})--+
575+
(source:N {trail: ["C", "C", "A", "C", "A", "B", "B", "B", "A"]})--{2,}
576576
(target:N {trail: ["A", "B", "C", "A", "B", "C", "A", "B", "C"]})
577577
RETURN length(p) AS pathLength
578578
----
@@ -596,24 +596,24 @@ It, therefore, must exhaust all possible target nodes before determining the sho
596596
+-----------------------------------+----+----------------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
597597
| Operator | Id | Details | Estimated Rows | Rows | DB Hits | Memory (Bytes) | Page Cache Hits/Misses | Time (ms) | Pipeline |
598598
+-----------------------------------+----+----------------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
599-
| +ProduceResults | 0 | `length(p)` | 19612941 | 1 | 0 | 0 | 0/0 | 0.027 | |
599+
| +ProduceResults | 0 | pathLength | 19612941 | 1 | 0 | 0 | 0/0 | 0.228 | |
600600
| | +----+----------------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+ |
601-
| +Projection | 1 | length((source)-[anon_7*]-(target)) AS `length(p)` | 19612941 | 1 | 17 | | 9/0 | 0.036 | |
601+
| +Projection | 1 | length((source)-[anon_7*]-(target)) AS pathLength | 19612941 | 1 | 17 | | 9/0 | 1.405 | |
602602
| | +----+----------------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+ |
603-
| +StatefulShortestPath(All, Trail) | 2 | SHORTEST 1 (source) ((`anon_3`)-[`anon_4`]-(`anon_5`)){1, } (target) | 19612941 | 1 | 354292 | 64720328 | 85358/0 | 139.138 | In Pipeline 1 |
603+
| +StatefulShortestPath(All, Trail) | 2 | SHORTEST 1 (source) ((`anon_3`)-[`anon_4`]-(`anon_5`)){2, } (target) | 19612941 | 1 | 354300 | 65608340 | 85662/0 | 290.001 | In Pipeline 1 |
604604
| | | | expanding from: source | | | | | | | |
605605
| | | | inlined predicates: target.trail = $autolist_1 | | | | | | | |
606606
| | | | target:N | | | | | | | |
607607
| | +----+----------------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
608608
| +Filter | 3 | source.trail = $autolist_0 | 4429 | 1 | 177146 | | | | |
609609
| | +----+----------------------------------------------------------------------+----------------+-------+---------+----------------+ | | |
610-
| +NodeByLabelScan | 4 | source:N | 88573 | 88573 | 88574 | 376 | 2128/0 | 42.628 | Fused in Pipeline 0 |
610+
| +NodeByLabelScan | 4 | source:N | 88573 | 88573 | 88574 | 376 | 1853/0 | 56.193 | Fused in Pipeline 0 |
611611
+-----------------------------------+----+----------------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
612612
613-
Total database accesses: 620029, total allocated memory: 64720664
613+
Total database accesses: 620037, total allocated memory: 65608676
614614
615615
1 row
616-
ready to start consuming query after 59 ms, results consumed after another 183 ms
616+
ready to start consuming query after 83 ms, results consumed after another 351 ms
617617
----
618618
619619
However, since each `trail` property is unique, rewriting the query to use a `CALL` subquery yields a more efficient plan.
@@ -626,43 +626,43 @@ PROFILE
626626
MATCH (start:N {trail: ["C", "C", "A", "C", "A", "B", "B", "B", "A"]}),
627627
(end:N {trail: ["A", "B", "C", "A", "B", "C", "A", "B", "C"]})
628628
CALL (start, end) {
629-
MATCH p = ANY SHORTEST (start)--+(end)
629+
MATCH p = ANY SHORTEST (start)--{2,}(end)
630630
RETURN p
631631
}
632632
RETURN length(p) AS pathLength
633633
----
634634
635-
The result is a significantly faster query (down from 59 to 9 milliseconds):
635+
The result is a less resource intensive query:
636636
637637
.Query Plan
638638
[source, role="queryplan"]
639639
----
640-
+------------------------------------+----+----------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
641-
| Operator | Id | Details | Estimated Rows | Rows | DB Hits | Memory (Bytes) | Page Cache Hits/Misses | Time (ms) | Pipeline |
642-
+------------------------------------+----+----------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
643-
| +ProduceResults | 0 | `length(p)` | 19612941 | 1 | 0 | 0 | 0/0 | 0.019 | |
644-
| | +----+----------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+ |
645-
| +Projection | 1 | length(p) AS `length(p)` | 19612941 | 1 | 0 | | 0/0 | 0.007 | |
646-
| | +----+----------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+ |
647-
| +Projection | 2 | (start)-[anon_14*]-(end) AS p | 19612941 | 1 | 17 | | 9/0 | 0.073 | |
648-
| | +----+----------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+ |
649-
| +StatefulShortestPath(Into, Trail) | 3 | ANY 1 (start) ((`anon_10`)-[`anon_11`]-(`anon_12`)){1, } (end) | 19612941 | 1 | 1936 | 990280 | 205/0 | 1.135 | In Pipeline 3 |
650-
| | +----+----------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
651-
| +CartesianProduct | 4 | | 19612941 | 1 | 0 | 9040 | | 0.131 | In Pipeline 2 |
652-
| |\ +----+----------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
653-
| | +Filter | 5 | end.trail = $autolist_1 | 22143 | 1 | 177146 | | | | |
654-
| | | +----+----------------------------------------------------------------+----------------+-------+---------+----------------+ | | |
655-
| | +NodeByLabelScan | 6 | end:N | 442865 | 88573 | 88574 | 392 | 2128/0 | 29.822 | Fused in Pipeline 1 |
656-
| | +----+----------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
657-
| +Filter | 7 | start.trail = $autolist_0 | 4429 | 1 | 177146 | | | | |
658-
| | +----+----------------------------------------------------------------+----------------+-------+---------+----------------+ | | |
659-
| +NodeByLabelScan | 8 | start:N | 88573 | 88573 | 88574 | 376 | 2128/0 | 40.743 | Fused in Pipeline 0 |
660-
+------------------------------------+----+----------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
661-
662-
Total database accesses: 533393, total allocated memory: 999592
640+
+------------------------------------+----+------------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
641+
| Operator | Id | Details | Estimated Rows | Rows | DB Hits | Memory (Bytes) | Page Cache Hits/Misses | Time (ms) | Pipeline |
642+
+------------------------------------+----+------------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
643+
| +ProduceResults | 0 | pathLength | 19612941 | 1 | 0 | 0 | 0/0 | 0.116 | |
644+
| | +----+------------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+ |
645+
| +Projection | 1 | length(p) AS pathLength | 19612941 | 1 | 0 | | 0/0 | 0.012 | |
646+
| | +----+------------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+ |
647+
| +Projection | 2 | (start)-[anon_7*]-(end) AS p | 19612941 | 1 | 17 | | 9/0 | 0.122 | |
648+
| | +----+------------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+ |
649+
| +StatefulShortestPath(Into, Trail) | 3 | SHORTEST 1 (start) ((`anon_3`)-[`anon_4`]-(`anon_5`)){2, } (end) | 19612941 | 1 | 1952 | 1337600 | 209/0 | 2.786 | In Pipeline 3 |
650+
| | +----+------------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
651+
| +CartesianProduct | 4 | | 19612941 | 1 | 0 | 9040 | | 0.053 | In Pipeline 2 |
652+
| |\ +----+------------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
653+
| | +Filter | 5 | end.trail = $autolist_1 | 22143 | 1 | 177146 | | | | |
654+
| | | +----+------------------------------------------------------------------+----------------+-------+---------+----------------+ | | |
655+
| | +NodeByLabelScan | 6 | end:N | 442865 | 88573 | 88574 | 392 | 1853/0 | 37.853 | Fused in Pipeline 1 |
656+
| | +----+------------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
657+
| +Filter | 7 | start.trail = $autolist_0 | 4429 | 1 | 177146 | | | | |
658+
| | +----+------------------------------------------------------------------+----------------+-------+---------+----------------+ | | |
659+
| +NodeByLabelScan | 8 | start:N | 88573 | 88573 | 88574 | 376 | 1853/0 | 67.672 | Fused in Pipeline 0 |
660+
+------------------------------------+----+------------------------------------------------------------------+----------------+-------+---------+----------------+------------------------+-----------+---------------------+
661+
662+
Total database accesses: 533409, total allocated memory: 1346912
663663
664664
1 row
665-
ready to start consuming query after 9 ms, results consumed after another 73 ms
665+
ready to start consuming query after 78 ms, results consumed after another 120 ms
666666
----
667667
668668
======
@@ -845,8 +845,7 @@ Or, when the estimated cardinality of the source and target nodes in a shortest
845845
* Selector is one of: `SHORTEST 1`, `ANY`, `ANY SHORTEST`, `ALL SHORTEST`, `SHORTEST GROUP`, or `SHORTEST 1 GROUP`.
846846
* There is only one relationship pattern.
847847
* If the pattern is a xref:patterns/variable-length-patterns.adoc#quantified-path-patterns[quantified path pattern] in the form of `\(()-[]-())`, or is a xref:patterns/variable-length-patterns.adoc#quantified-relationships[quantified-relationship], and it uses a filter that can be applied directly to the relationship.
848-
* If the pattern is a quantified path pattern or a quantified relationship and the relationship pattern is directed, the lower bound of the xref:patterns/reference.adoc#quantifiers[quantifier] is 0 or 1.
849-
If the relationship pattern is directed, the lower bound is 0.
848+
* If the pattern is a quantified path pattern or a quantified relationship, the lower bound of the xref:patterns/reference.adoc#quantifiers[quantifier] is 0 or 1.
850849
* In the case of a quantified path pattern, there are no node variables declared inside the quantified path pattern that are referenced elsewhere.
851850

852851
| `StatefulShortestPath(Into)`

0 commit comments

Comments
 (0)