Browse Source
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
4 changed files with 67 additions and 0 deletions
@ -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); |
|||
@ -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"; |
|||
} |
|||
} |
|||
Loading…
Reference in new issue