fix(eventstore): order by sequence if filter by aggregate id (#8019)

# Which Problems Are Solved

Queriying events by an aggregate id can produce high loads on the
database if the aggregate id contains many events (count > 1000000).

# How the Problems Are Solved

Instead of using the postion and in_tx_order columns we use the sequence
column which guarantees correct ordering in a single aggregate and uses
more optimised indexes.

# Additional Context

Closes https://github.com/zitadel/DevOps/issues/50

Co-authored-by: Livio Spring <livio.a@gmail.com>
This commit is contained in:
Silvan
2024-05-28 08:49:30 +02:00
committed by GitHub
parent 5fa32cbfb9
commit 4dc86c2415
2 changed files with 22 additions and 5 deletions

View File

@@ -282,17 +282,23 @@ func (db *CRDB) db() *database.DB {
return db.DB
}
func (db *CRDB) orderByEventSequence(desc, useV1 bool) string {
func (db *CRDB) orderByEventSequence(desc, shouldOrderBySequence, useV1 bool) string {
if useV1 {
if desc {
return ` ORDER BY event_sequence DESC`
}
return ` ORDER BY event_sequence`
}
if shouldOrderBySequence {
if desc {
return ` ORDER BY "sequence" DESC`
}
return ` ORDER BY "sequence"`
}
if desc {
return ` ORDER BY "position" DESC, in_tx_order DESC`
}
return ` ORDER BY "position", in_tx_order`
}