Skip to content

Commit a97b4f8

Browse files
Add best practice example to subqueries in transactions with deadlocks section (neo4j#1301)
Co-authored-by: Jens Pryce-Åklundh <[email protected]>
1 parent 4856b49 commit a97b4f8

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

modules/ROOT/pages/subqueries/subqueries-in-transactions.adoc

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,7 @@ CALL (row) {
959959
MERGE (y:Year {year: row.year})
960960
MERGE (m)-[r:RELEASED_IN]->(y)
961961
} IN 2 CONCURRENT TRANSACTIONS OF 10 ROWS ON ERROR RETRY FOR 3 SECONDS THEN CONTINUE REPORT STATUS AS status
962-
RETURN status.transactionID as transaction, status.committed AS successfulTransaction
962+
RETURN status.transactionId as transaction, status.committed AS successfulTransaction
963963
----
964964
// end::subqueries_in_transactions_deadlock_example[]
965965
@@ -1067,6 +1067,30 @@ The result shows that all transactions are now successful:
10671067
+-------------------------------------------------+
10681068
----
10691069
1070+
Deadlock resolution and transaction retries are time-consuming, making deadlock avoidance a preferred strategy.
1071+
This can be achieved by dividing a task into two distinct subqueries: a data-independent subquery run in parallel with maximum concurrency, and a data-dependent subquery executed serially (i.e. one transaction at a time) to avoid deadlocks.
1072+
In the below example, nodes and properties are created in a concurrent subquery, while the relationships connecting those nodes are created in a serial subquery.
1073+
This method benefits from the performance of concurrent transactions while avoiding deadlocks.
1074+
1075+
.Avoiding deadlocks by dividing an import task into a data-independent concurrent subquery followed by a data-dependent serial subquery
1076+
// tag::subqueries_in_transactions_deadlock_example_2[]
1077+
[source, cypher]
1078+
----
1079+
LOAD CSV WITH HEADERS FROM 'https://data.neo4j.com/importing-cypher/movies.csv' AS row
1080+
CALL (row) {
1081+
MERGE (m:Movie {movieId: row.movieId})
1082+
MERGE (y:Year {year: row.year})
1083+
RETURN m, y
1084+
} IN CONCURRENT TRANSACTIONS OF 10 ROWS ON ERROR RETRY THEN CONTINUE REPORT STATUS AS nodeStatus
1085+
CALL (m, y) {
1086+
MERGE (m)-[r:RELEASED_IN]->(y)
1087+
} IN TRANSACTIONS OF 10 ROWS ON ERROR RETRY THEN CONTINUE REPORT STATUS AS relationshipStatus
1088+
RETURN nodeStatus.transactionId as nodeTransaction,
1089+
nodeStatus.committed AS successfulNodeTransaction,
1090+
relationshipStatus.transactionId as relationshipTransaction,
1091+
relationshipStatus.committed AS successfulRelationshipTransaction
1092+
----
1093+
// end::subqueries_in_transactions_deadlock_example_2[]
10701094
10711095
.Click to see an example of failed transactions being retried without using `ON ERROR RETRY`
10721096
[%collapsible]

0 commit comments

Comments
 (0)