From d26d36e42a3667f58b3bc21e5ca7db6e059a5be5 Mon Sep 17 00:00:00 2001 From: Moritz Kiefer Date: Mon, 19 Jul 2021 15:34:45 +0200 Subject: [PATCH] Use a combined template id, stakeholder index MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes #10231 Before Benchmark (batchSize) (extraParties) (extraTemplates) Mode Cnt Score Error Units QueryBenchmark.run 10000 0 1 avgt 5 0.277 ± 0.037 s/op QueryBenchmark.run 10000 0 10 avgt 5 0.479 ± 0.301 s/op QueryBenchmark.run 10000 0 100 avgt 5 2.131 ± 0.497 s/op After Benchmark (batchSize) (extraParties) (extraTemplates) Mode Cnt Score Error Units QueryBenchmark.run 10000 1 0 avgt 5 0.274 ± 0.141 s/op QueryBenchmark.run 10000 10 0 avgt 5 0.299 ± 0.144 s/op QueryBenchmark.run 10000 100 0 avgt 5 0.281 ± 0.038 s/op changelog_begin changelog_end --- .../digitalasset/http/dbbackend/Queries.scala | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/ledger-service/db-backend/src/main/scala/com/digitalasset/http/dbbackend/Queries.scala b/ledger-service/db-backend/src/main/scala/com/digitalasset/http/dbbackend/Queries.scala index ed8753773931..50b3b6b0db38 100644 --- a/ledger-service/db-backend/src/main/scala/com/digitalasset/http/dbbackend/Queries.scala +++ b/ledger-service/db-backend/src/main/scala/com/digitalasset/http/dbbackend/Queries.scala @@ -54,10 +54,6 @@ sealed abstract class Queries { protected[this] def contractsTableSignatoriesObservers: Fragment - private[this] val indexContractsTable = CreateIndex(sql""" - CREATE INDEX contract_tpid_idx ON contract (tpid) - """) - private[this] val createOffsetTable = CreateTable( "ledger_offset", sql""" @@ -110,7 +106,6 @@ sealed abstract class Queries { createTemplateIdsTable, createOffsetTable, createContractsTable, - indexContractsTable, ) private[http] def initDatabase(implicit log: LogHandler): ConnectionIO[Unit] = { @@ -478,7 +473,12 @@ private object PostgresQueries extends Queries { CREATE INDEX contract_tpid_key_idx ON contract USING BTREE (tpid, key) """) - protected[this] override def initDatabaseDdls = super.initDatabaseDdls :+ indexContractsKeys + private[this] val indexContractsTable = CreateIndex(sql""" + CREATE INDEX contract_tpid_idx ON contract (tpid) + """) + + protected[this] override def initDatabaseDdls = + super.initDatabaseDdls :+ indexContractsTable :+ indexContractsKeys protected[this] override def contractsTableSignatoriesObservers = sql""" ,signatories TEXT ARRAY NOT NULL @@ -631,13 +631,13 @@ private object OracleQueries extends Queries { "contract_stakeholders", sql"""CREATE MATERIALIZED VIEW contract_stakeholders BUILD IMMEDIATE REFRESH FAST ON STATEMENT AS - SELECT contract_id, stakeholder FROM contract, + SELECT contract_id, tpid, stakeholder FROM contract, json_table(json_array(signatories, observers), '$$[*][*]' columns (stakeholder $partyType path '$$'))""", ) private[this] def stakeholdersIndex = CreateIndex( - sql"""CREATE INDEX stakeholder_idx ON contract_stakeholders (stakeholder)""" + sql"""CREATE INDEX stakeholder_idx ON contract_stakeholders (tpid, stakeholder)""" ) protected[this] override def initDatabaseDdls = @@ -684,7 +684,7 @@ private object OracleQueries extends Queries { case q +-: qs => joinFragment( OneAnd(q, qs.toVector) map { case (tpid, predicate) => - fr"($tpid = tpid AND (" ++ predicate ++ fr"))" + fr"($tpid = cst.tpid AND (" ++ predicate ++ fr"))" }, fr" OR ", ) @@ -730,7 +730,7 @@ private object OracleQueries extends Queries { } case MatchedQueryMarker.Unused => val NonEmpty(nequeries) = queries - queryByCondition[SurrogateTpId](fr"tpid", nequeries) + queryByCondition[SurrogateTpId](fr"cst.tpid", nequeries) } }