Skip to content

Commit 96b5cf5

Browse files
author
Fabian Baumeister
committed
feat: Add performance rules for persistence layer
1 parent 4119344 commit 96b5cf5

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

modules/ROOT/nav.adoc

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
* Persistence
1313
** Relational Databases
1414
*** xref:persistence/jpa.adoc[]
15+
*** xref:persistence/performance.adoc[]
1516
*** xref:persistence/transactional.adoc[]
1617
1718
* Cross cutting
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
= Performance
2+
3+
When doing performance optimization, it's important to keep the following rules in mind.
4+
5+
== Rule 0: Measure, don't think
6+
7+
[quote,Donald E. Knuth]
8+
Premature optimization is the root of all evil.
9+
10+
Before starting to optimize any database operation, it's important to analyse the root cause by measuring the execution times.
11+
12+
== Rule 1: The performance of a use case is proportional to the number of SQL statements it generates
13+
14+
```
15+
executionTime(usecase) = executionTime(queryA) + executionTime(queryB) * N
16+
```
17+
18+
* It has shown, that the problem often is in the number of query executions (N) and not in the query itself
19+
* Typical values for executionTime(queryA) are `5 to 30 msec` and `5 to 100 msec` for executionTime(queryB)
20+
* Before optimizing a single query A or B, check and if possible reduce the number of executions of the queries
21+
* In some rare cases the executionTime of queryB is the problem and tuning the SQL or the DB is helpful
22+
* Always consider Rule 0 before applying any action
23+
24+
25+
== Rule 2: The performance of a use case is the product of objects in a session with the number of flush operations
26+
27+
In case a Object Relational Mapper (like JPA) is used and a high number of objects are in the session, then the flush operations can become a problem.
28+
29+
The default configuration read/write auto flush of JPA makes sure that prior of any query the current session is flushed, to ensure, that the query gets the latest updates from the DB.
30+
31+
If there are big datasets in the session, the flush operation can take an relativ huge amount of time. E.g. in an analytics (read only) application with lots of data, the flush would be not necessary, becasuse no data was changed. Nonetheless, it flushes the session with all data prior to any other query.
32+
33+
* In case the number of objects in the session is < 1000 there's usually not an issue
34+
* Check if the number of objects in the session is necessary, missused eager loading
35+
* Check if the flush mode can be adjusted, as in the mentioned analytics application example
36+
* Always consider Rule 0 before applying any action
37+

0 commit comments

Comments
 (0)