Browse Source

Add (tenant_id, id) index on calculated_field for tenant-scoped paging

Calculated field actors page through each tenant's calculated fields on
startup (WHERE tenant_id = ? ORDER BY id LIMIT ?). With no index leading on
tenant_id, every page and its count query scan the whole table and sort,
so startup cost grows with tenants x table size.

Ship the index in schema-entities-idx.sql for new installs and as a new
4.4.1.0 LTS migration for existing ones (4.4.0.0 is already released, and
the range lower bound is exclusive).
pull/16329/head
Sergey Matvienko 2 months ago
parent
commit
ca1b84ca5a
  1. 9
      application/src/main/data/upgrade/lts/4.4.1.0/schema_update.sql
  2. 31
      application/src/main/java/org/thingsboard/server/service/install/lts/V4_4_1_0Migration.java
  3. 25
      application/src/test/java/org/thingsboard/server/service/install/lts/MigrationIntegrationTest.java
  4. 2
      dao/src/main/resources/sql/schema-entities-idx.sql

9
application/src/main/data/upgrade/lts/4.4.1.0/schema_update.sql

@ -0,0 +1,9 @@
--
-- SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
-- SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc.
-- SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1
--
-- Tenant-scoped paging over calculated fields (WHERE tenant_id = ? ORDER BY id LIMIT ?), issued for every tenant
-- when the calculated field actors initialize, otherwise scans the whole table and sorts on each call.
CREATE INDEX IF NOT EXISTS idx_calculated_field_tenant_id_id ON calculated_field(tenant_id, id);

31
application/src/main/java/org/thingsboard/server/service/install/lts/V4_4_1_0Migration.java

@ -0,0 +1,31 @@
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc.
// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1
package org.thingsboard.server.service.install.lts;
import org.springframework.stereotype.Component;
import org.thingsboard.server.queue.util.TbCoreComponent;
/**
* Registration-only migration with no {@link #apply()} (no data migration).
* <p>
* {@link MigrationService} selects migrations from the injected {@link Migration} beans, not from the
* on-disk {@code data/upgrade/lts/<version>/} directories. So this bean is what makes the runner discover
* version {@code 4.4.1.0} and execute its {@code data/upgrade/lts/4.4.1.0/schema_update.sql}, which adds the
* {@code idx_calculated_field_tenant_id_id} index used by tenant-scoped calculated field paging.
* <p>
* It needs a slot of its own rather than {@code 4.4.0.0}: that version is already released, and
* {@link Version#isInRange} is exclusive at the lower bound, so an installation already on {@code 4.4.0.0}
* would never pick up a change added there.
* <p>
* The dir/bean consistency (both ways) is guarded by a test in {@code MigrationIntegrationTest}.
*/
@Component
@TbCoreComponent
public class V4_4_1_0Migration implements Migration {
@Override
public String getVersion() {
return "4.4.1.0";
}
}

25
application/src/test/java/org/thingsboard/server/service/install/lts/MigrationIntegrationTest.java

@ -451,6 +451,25 @@ public class MigrationIntegrationTest extends AbstractControllerTest {
* older script legitimately behind. What may never happen is a script declaring a clause the fresh-install
* DDL does not. The single-row index is compared exactly: there is only ever one right form of it.
*/
@Test
public void appliesSchemaForV4410CreatesCalculatedFieldTenantIdIndex() {
// The test-context schema already ships the index (schema-entities-idx.sql), so drop it first to prove the
// 4.4.1.0 schema_update.sql re-creates it. The finally block restores it for the shared DaoSqlTest schema.
try {
jdbcTemplate.execute("DROP INDEX IF EXISTS idx_calculated_field_tenant_id_id");
assertFalse(indexExists("idx_calculated_field_tenant_id_id"));
migrationService.applyMigrations("4.4.0.0", "4.4.1.0", false, () -> {});
assertTrue(indexExists("idx_calculated_field_tenant_id_id"));
assertEquals("CREATE INDEX idx_calculated_field_tenant_id_id ON public.calculated_field USING btree (tenant_id, id)",
jdbcTemplate.queryForObject("SELECT indexdef FROM pg_indexes WHERE indexname = ?", String.class,
"idx_calculated_field_tenant_id_id"));
} finally {
jdbcTemplate.execute("CREATE INDEX IF NOT EXISTS idx_calculated_field_tenant_id_id ON calculated_field(tenant_id, id)");
}
}
@Test
public void everyTbClusterDdlCopyIsConsistentWithSchemaEntities() {
String schemaEntities = readResource("sql/schema-entities.sql");
@ -707,6 +726,12 @@ public class MigrationIntegrationTest extends AbstractControllerTest {
return Boolean.TRUE.equals(exists);
}
private boolean indexExists(String indexName) {
Boolean exists = jdbcTemplate.queryForObject(
"SELECT EXISTS (SELECT 1 FROM pg_indexes WHERE indexname = ?)", Boolean.class, indexName);
return Boolean.TRUE.equals(exists);
}
private boolean constraintExists(String constraintName) {
Boolean exists = jdbcTemplate.queryForObject(
"SELECT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = ?)", Boolean.class, constraintName);

2
dao/src/main/resources/sql/schema-entities-idx.sql

@ -181,3 +181,5 @@ CREATE INDEX IF NOT EXISTS idx_iot_hub_installed_item_tenant_id ON iot_hub_insta
CREATE INDEX IF NOT EXISTS idx_iot_hub_installed_item_item_type ON iot_hub_installed_item(tenant_id, item_type);
CREATE INDEX IF NOT EXISTS idx_iot_hub_installed_item_item_id ON iot_hub_installed_item(tenant_id, item_id);
CREATE INDEX IF NOT EXISTS idx_calculated_field_tenant_id_id ON calculated_field(tenant_id, id);

Loading…
Cancel
Save