Skip to content

Commit 00426c4

Browse files
committed
fix(indexing): improve fetch aggregated instance query
Related: MSEARCH-1074
1 parent 19362c3 commit 00426c4

File tree

1 file changed

+58
-18
lines changed

1 file changed

+58
-18
lines changed

src/main/java/org/folio/search/service/reindex/jdbc/UploadInstanceRepository.java

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,41 @@
2020
public class UploadInstanceRepository extends UploadRangeRepository {
2121

2222
private static final String SELECT_SQL_TEMPLATE = """
23-
SELECT i.json
24-
|| jsonb_build_object('tenantId', i.tenant_id,
25-
'shared', i.shared,
26-
'isBoundWith', i.is_bound_with,
27-
'holdings', COALESCE(jsonb_agg(DISTINCT h.json || jsonb_build_object('tenantId', h.tenant_id)) FILTER (WHERE h.json IS NOT NULL), '[]'::jsonb),
28-
'items', COALESCE(jsonb_agg(it.json || jsonb_build_object('tenantId', it.tenant_id)) FILTER (WHERE it.json IS NOT NULL), '[]'::jsonb)) as json
29-
FROM %s i
30-
LEFT JOIN %s h on h.instance_id = i.id
31-
LEFT JOIN %s it on it.holding_id = h.id
32-
WHERE %s
33-
GROUP BY i.id;
23+
WITH aggregated_holdings AS (
24+
SELECT
25+
h.instance_id,
26+
jsonb_agg( h.json || jsonb_build_object('tenantId', h.tenant_id)
27+
) AS holdings_json
28+
FROM %2$s h
29+
WHERE %4$s
30+
GROUP BY h.instance_id
31+
),
32+
aggregated_items AS (
33+
SELECT
34+
it.instance_id,
35+
jsonb_agg(
36+
it.json || jsonb_build_object('tenantId', it.tenant_id)
37+
) AS items_json
38+
FROM %3$s it
39+
WHERE %5$s
40+
GROUP BY it.instance_id
41+
)
42+
SELECT
43+
i.json || jsonb_build_object(
44+
'tenantId', i.tenant_id,
45+
'shared', i.shared,
46+
'isBoundWith', i.is_bound_with,
47+
'holdings', COALESCE(ah.holdings_json, '[]'::jsonb),
48+
'items', COALESCE(ai.items_json, '[]'::jsonb)
49+
) AS json
50+
FROM %1$s i
51+
LEFT JOIN aggregated_holdings ah ON ah.instance_id = i.id
52+
LEFT JOIN aggregated_items ai ON ai.instance_id = i.id
53+
WHERE %6$s;
3454
""";
3555

36-
private static final String IDS_RANGE_WHERE_CLAUSE = "i.id >= ?::uuid AND i.id <= ?::uuid";
37-
private static final String INSTANCE_IDS_WHERE_CLAUSE = "i.id IN (%s)";
56+
private static final String IDS_RANGE_WHERE_CLAUSE = "%1$s >= ?::uuid AND %1$s <= ?::uuid";
57+
private static final String INSTANCE_IDS_WHERE_CLAUSE = "%s IN (%s)";
3858

3959
protected UploadInstanceRepository(JdbcTemplate jdbcTemplate, JsonConverter jsonConverter,
4060
FolioExecutionContext context,
@@ -56,15 +76,24 @@ public List<Map<String, Object>> fetchByIds(List<String> ids) {
5676
if (ids == null || ids.isEmpty()) {
5777
return Collections.emptyList();
5878
}
59-
var whereClause = INSTANCE_IDS_WHERE_CLAUSE.formatted(JdbcUtils.getParamPlaceholderForUuid(ids.size()));
79+
var instanceWhereClause = INSTANCE_IDS_WHERE_CLAUSE.formatted("i.id",
80+
JdbcUtils.getParamPlaceholderForUuid(ids.size()));
81+
var itemWhereClause = INSTANCE_IDS_WHERE_CLAUSE.formatted("it.instance_id",
82+
JdbcUtils.getParamPlaceholderForUuid(ids.size()));
83+
var holdingsWhereClause = INSTANCE_IDS_WHERE_CLAUSE.formatted("h.instance_id",
84+
JdbcUtils.getParamPlaceholderForUuid(ids.size()));
6085
var sql = SELECT_SQL_TEMPLATE.formatted(getFullTableName(context, entityTable()),
6186
getFullTableName(context, "holding"),
6287
getFullTableName(context, "item"),
63-
whereClause);
88+
holdingsWhereClause,
89+
itemWhereClause,
90+
instanceWhereClause);
6491
return jdbcTemplate.query(sql, ps -> {
6592
int i = 1;
66-
for (; i <= ids.size(); i++) {
67-
ps.setObject(i, ids.get(i - 1)); // set instance ids
93+
for (int paramSet = 0; paramSet < 3; paramSet++) {
94+
for (String id : ids) {
95+
ps.setObject(i++, id);
96+
}
6897
}
6998
}, rowToMapMapper());
7099
}
@@ -76,12 +105,23 @@ protected List<RangeGenerator.Range> createRanges() {
76105
return RangeGenerator.createUuidRanges(rangesCount);
77106
}
78107

108+
@Override
109+
public List<Map<String, Object>> fetchByIdRange(String lower, String upper) {
110+
var sql = getFetchBySql();
111+
return jdbcTemplate.query(sql, rowToMapMapper(), lower, upper, lower, upper, lower, upper);
112+
}
113+
79114
@Override
80115
protected String getFetchBySql() {
116+
var instanceWhereClause = IDS_RANGE_WHERE_CLAUSE.formatted("i.id");
117+
var itemWhereClause = IDS_RANGE_WHERE_CLAUSE.formatted("it.instance_id");
118+
var holdingsWhereClause = IDS_RANGE_WHERE_CLAUSE.formatted("h.instance_id");
81119
return SELECT_SQL_TEMPLATE.formatted(getFullTableName(context, entityTable()),
82120
getFullTableName(context, "holding"),
83121
getFullTableName(context, "item"),
84-
IDS_RANGE_WHERE_CLAUSE);
122+
holdingsWhereClause,
123+
itemWhereClause,
124+
instanceWhereClause);
85125
}
86126

87127
@Override

0 commit comments

Comments
 (0)