Skip to content

Commit 51fdd90

Browse files
committed
Add best practice example to subqueries in transactions with deadlocks section
1 parent 5f76f16 commit 51fdd90

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,29 @@ The result shows that all transactions are now successful:
10671067
+-------------------------------------------------+
10681068
----
10691069
1070+
However, deadlock resolution and retrying of transactions takes time, so the best practice is to avoid deadlocks in the first place.
1071+
This can be done by splitting up the subquery into a data-independent subquery that can be run in parallel with maximum concurrency,
1072+
and a data-dependent subquery that is run serially to avoid deadlocks.
1073+
In this example nodes and properties are created in a concurrent subquery, while the relationships are then created in a serial subquery.
1074+
This way we can benefit from the performance of concurrent transactions while avoiding the deadlocks that slow down the overall performance.
1075+
1076+
.Query splitting up the transactional subquery in a data-independent concurrent subquery followed by a data-dependent serial subquery to avoid deadlocks
1077+
// tag::subqueries_in_transactions_deadlock_example_2[]
1078+
[source, cypher]
1079+
----
1080+
LOAD CSV WITH HEADERS FROM 'https://data.neo4j.com/importing-cypher/movies.csv' AS row
1081+
CALL (row) {
1082+
MERGE (m:Movie {movieId: row.movieId})
1083+
MERGE (y:Year {year: row.year})
1084+
RETURN m, y
1085+
} IN CONCURRENT TRANSACTIONS OF 10 ROWS ON ERROR RETRY FOR 3 SECONDS THEN CONTINUE REPORT STATUS AS nodeStatus
1086+
CALL (m, y) {
1087+
MERGE (m)-[r:RELEASED_IN]->(y)
1088+
} IN TRANSACTIONS OF 10 ROWS ON ERROR RETRY FOR 3 SECONDS THEN CONTINUE REPORT STATUS AS relationshipStatus
1089+
RETURN nodeStatus.transactionID as nodeTransaction, nodeStatus.committed AS successfulNodeTransaction,
1090+
relationshipStatus.transactionID as relationshipTransaction, relationshipStatus.committed AS successfulRelationshipTransaction
1091+
----
1092+
// end::subqueries_in_transactions_deadlock_example_2[]
10701093
10711094
.Click to see an example of failed transactions being retried without using `ON ERROR RETRY`
10721095
[%collapsible]

0 commit comments

Comments
 (0)