Skip to content

Commit 759c2e8

Browse files
committed
Make sure we're using indexes during rollbacks
Fixes #2083 (comment) The planner sometimes uses a query similar to Index Scan using tx_pkey on tx Filter: (block_id >= $1) the filter is not Index Cond, so this ends up in a sequential scan. The index refers to the primary key and is only used for sorting. Instead we want to use Sort (top-N heapsort) -> Index Scan using idx_tx_block_id Index Cond: (block_id >= $1) With this change we're forcing a similar plan Aggregate -> Index Scan using idx_tx_block_id Index Cond: (block_id >= $1) making sure we're using the Index Cond and then sorting.
1 parent 3c8724b commit 759c2e8

1 file changed

Lines changed: 6 additions & 10 deletions

File tree

cardano-db/src/Cardano/Db/Statement/MinIds.hs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,18 @@ queryMinRefIdStmt ::
4848
-- | Raw ID decoder (Int64)
4949
HsqlD.Row Int64 ->
5050
HsqlStmt.Statement b (Maybe Int64)
51-
queryMinRefIdStmt fieldName encoder idDecoder =
51+
queryMinRefIdStmt fieldName encoder _idDecoder =
5252
HsqlStmt.Statement sql encoder decoder True
5353
where
5454
validCol = validateColumn @a fieldName
5555
sql =
5656
TextEnc.encodeUtf8 $
5757
Text.concat
58-
[ "SELECT id"
58+
[ "SELECT MIN(id)"
5959
, " FROM " <> tableName (Proxy @a)
6060
, " WHERE " <> validCol <> " >= $1"
61-
, " ORDER BY id ASC"
62-
, " LIMIT 1"
6361
]
64-
decoder = HsqlD.rowMaybe idDecoder
62+
decoder = HsqlD.singleRow (HsqlD.column (HsqlD.nullable HsqlD.int8))
6563

6664
queryMinRefId ::
6765
forall a b.
@@ -92,21 +90,19 @@ queryMinRefIdNullableStmt ::
9290
-- | Raw ID decoder (Int64)
9391
HsqlD.Row Int64 ->
9492
HsqlStmt.Statement b (Maybe Int64)
95-
queryMinRefIdNullableStmt fieldName encoder idDecoder =
93+
queryMinRefIdNullableStmt fieldName encoder _idDecoder =
9694
HsqlStmt.Statement sql encoder decoder True
9795
where
9896
validCol = validateColumn @a fieldName
99-
decoder = HsqlD.rowMaybe idDecoder
10097
sql =
10198
TextEnc.encodeUtf8 $
10299
Text.concat
103-
[ "SELECT id"
100+
[ "SELECT MIN(id)"
104101
, " FROM " <> tableName (Proxy @a)
105102
, " WHERE " <> validCol <> " IS NOT NULL"
106103
, " AND " <> validCol <> " >= $1"
107-
, " ORDER BY id ASC"
108-
, " LIMIT 1"
109104
]
105+
decoder = HsqlD.singleRow (HsqlD.column (HsqlD.nullable HsqlD.int8))
110106

111107
queryMinRefIdNullable ::
112108
forall a b.

0 commit comments

Comments
 (0)