Skip to content

Commit cb781a1

Browse files
Add best practice example to subqueries in transactions with deadlocks section (#1301) (#1302)
Co-authored-by: Henrik Nyman <[email protected]>
1 parent cb7cad6 commit cb781a1

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

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

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -955,7 +955,7 @@ CALL (row) {
955955
MERGE (y:Year {year: row.year})
956956
MERGE (m)-[r:RELEASED_IN]->(y)
957957
} IN 2 CONCURRENT TRANSACTIONS OF 10 ROWS ON ERROR RETRY FOR 3 SECONDS THEN CONTINUE REPORT STATUS AS status
958-
RETURN status.transactionID as transaction, status.committed AS successfulTransaction
958+
RETURN status.transactionId as transaction, status.committed AS successfulTransaction
959959
----
960960
// end::subqueries_in_transactions_deadlock_example[]
961961
@@ -1063,6 +1063,31 @@ The result shows that all transactions are now successful:
10631063
+-------------------------------------------------+
10641064
----
10651065
1066+
Deadlock resolution and transaction retries are time-consuming, making deadlock avoidance a preferred strategy.
1067+
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.
1068+
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.
1069+
This method benefits from the performance of concurrent transactions while avoiding deadlocks.
1070+
1071+
.Avoiding deadlocks by dividing an import task into a data-independent concurrent subquery followed by a data-dependent serial subquery
1072+
// tag::subqueries_in_transactions_deadlock_example_2[]
1073+
[source, cypher]
1074+
----
1075+
LOAD CSV WITH HEADERS FROM 'https://data.neo4j.com/importing-cypher/movies.csv' AS row
1076+
CALL (row) {
1077+
MERGE (m:Movie {movieId: row.movieId})
1078+
MERGE (y:Year {year: row.year})
1079+
RETURN m, y
1080+
} IN CONCURRENT TRANSACTIONS OF 10 ROWS ON ERROR RETRY THEN CONTINUE REPORT STATUS AS nodeStatus
1081+
CALL (m, y) {
1082+
MERGE (m)-[r:RELEASED_IN]->(y)
1083+
} IN TRANSACTIONS OF 10 ROWS ON ERROR RETRY THEN CONTINUE REPORT STATUS AS relationshipStatus
1084+
RETURN nodeStatus.transactionId as nodeTransaction,
1085+
nodeStatus.committed AS successfulNodeTransaction,
1086+
relationshipStatus.transactionId as relationshipTransaction,
1087+
relationshipStatus.committed AS successfulRelationshipTransaction
1088+
----
1089+
// end::subqueries_in_transactions_deadlock_example_2[]
1090+
10661091
=====
10671092

10681093
[[restrictions]]

0 commit comments

Comments
 (0)