Skip to content

Commit

Permalink
optimize max event_sequential_id query for oracle (#11297)
Browse files Browse the repository at this point in the history
* Tweak max event_sequential_id query for oracle to ensure consistent performance on larger tables

CHANGELOG_BEGIN
CHANGELOG_END

* respond to review feedback

CHANGELOG_BEGIN
CHANGELOG_END
  • Loading branch information
dasormeter authored Oct 26, 2021
1 parent b1fed31 commit 70b90f4
Showing 1 changed file with 13 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,17 +201,23 @@ private[backend] object OracleStorageBackend

override def eventStrategy: common.EventStrategy = OracleEventStrategy

// TODO FIXME: Use tables directly instead of the participant_events view.
def maxEventSequentialIdOfAnObservableEvent(
offset: Offset
)(connection: Connection): Option[Long] = {
import com.daml.platform.store.Conversions.OffsetToStatement
// This query could be: "select max(event_sequential_id) from participant_events where event_offset <= ${range.endInclusive}"
// however tests using PostgreSQL 12 with tens of millions of events have shown that the index
// on `event_offset` is not used unless we _hint_ at it by specifying `order by event_offset`
val limitClause = OracleQueryStrategy.limitClause(Some(1))
SQL"select max(event_sequential_id) from participant_events where event_offset <= $offset group by event_offset order by event_offset desc $limitClause"
.as(get[Long](1).singleOpt)(connection)
SQL"""SELECT max(max_esi) FROM (
(
SELECT max(event_sequential_id) AS max_esi FROM participant_events_consuming_exercise
WHERE event_offset = (select max(event_offset) from participant_events_consuming_exercise where event_offset <= $offset)
) UNION ALL (
SELECT max(event_sequential_id) AS max_esi FROM participant_events_create
WHERE event_offset = (select max(event_offset) from participant_events_create where event_offset <= $offset)
) UNION ALL (
SELECT max(event_sequential_id) AS max_esi FROM participant_events_non_consuming_exercise
WHERE event_offset = (select max(event_offset) from participant_events_non_consuming_exercise where event_offset <= $offset)
)
)"""
.as(get[Long](1).?.single)(connection)
}

override def createDataSource(
Expand Down

0 comments on commit 70b90f4

Please sign in to comment.