From 1ad95b3a10e87d9937309ac704f32cfee86fcc01 Mon Sep 17 00:00:00 2001 From: Aaron Chung Date: Tue, 18 Aug 2026 00:42:01 +0530 Subject: [PATCH] Use correlated subqueries on user_statistics table in the account_view for network stats (instead of lateral join) --- .../cloud/upgrade/DatabaseUpgradeChecker.java | 2 ++ .../upgrade/dao/Upgrade42210to42220.java | 30 +++++++++++++++++++ ...ew.sql => schema-42210to42220-cleanup.sql} | 15 ++-------- .../META-INF/db/schema-42210to42220.sql | 21 +++++++++++++ .../META-INF/db/views/cloud.account_view.sql | 4 +-- 5 files changed, 58 insertions(+), 14 deletions(-) create mode 100644 engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42210to42220.java rename engine/schema/src/main/resources/META-INF/db/{views/cloud.account_netstats_view.sql => schema-42210to42220-cleanup.sql} (65%) create mode 100644 engine/schema/src/main/resources/META-INF/db/schema-42210to42220.sql diff --git a/engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java b/engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java index 3868ca960e06..ee19b89268cb 100644 --- a/engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java +++ b/engine/schema/src/main/java/com/cloud/upgrade/DatabaseUpgradeChecker.java @@ -33,6 +33,7 @@ import javax.inject.Inject; +import com.cloud.upgrade.dao.Upgrade42210to42220; import com.cloud.utils.FileUtil; import org.apache.cloudstack.utils.CloudStackVersion; import org.apache.commons.lang3.StringUtils; @@ -246,6 +247,7 @@ public DatabaseUpgradeChecker() { .next("4.20.4.0", new Upgrade42040to42100()) .next("4.21.0.0", new Upgrade42100to42200()) .next("4.22.0.0", new Upgrade42200to42210()) + .next("4.22.1.0", new Upgrade42210to42220()) .build(); } diff --git a/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42210to42220.java b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42210to42220.java new file mode 100644 index 000000000000..392dc4d4a3b5 --- /dev/null +++ b/engine/schema/src/main/java/com/cloud/upgrade/dao/Upgrade42210to42220.java @@ -0,0 +1,30 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.upgrade.dao; + +public class Upgrade42210to42220 extends DbUpgradeAbstractImpl implements DbUpgrade, DbUpgradeSystemVmTemplate { + + @Override + public String[] getUpgradableVersionRange() { + return new String[] {"4.22.1.0", "4.22.2.0"}; + } + + @Override + public String getUpgradedVersion() { + return "4.22.2.0"; + } +} diff --git a/engine/schema/src/main/resources/META-INF/db/views/cloud.account_netstats_view.sql b/engine/schema/src/main/resources/META-INF/db/schema-42210to42220-cleanup.sql similarity index 65% rename from engine/schema/src/main/resources/META-INF/db/views/cloud.account_netstats_view.sql rename to engine/schema/src/main/resources/META-INF/db/schema-42210to42220-cleanup.sql index 11193c465fd7..c77d04991619 100644 --- a/engine/schema/src/main/resources/META-INF/db/views/cloud.account_netstats_view.sql +++ b/engine/schema/src/main/resources/META-INF/db/schema-42210to42220-cleanup.sql @@ -15,17 +15,8 @@ -- specific language governing permissions and limitations -- under the License. --- cloud.account_netstats_view source - +--; +-- Schema upgrade cleanup from 4.22.1.0 to 4.22.2.0 +--; DROP VIEW IF EXISTS `cloud`.`account_netstats_view`; - -CREATE VIEW `cloud`.`account_netstats_view` AS -select - `user_statistics`.`account_id` AS `account_id`, - (sum(`user_statistics`.`net_bytes_received`) + sum(`user_statistics`.`current_bytes_received`)) AS `bytesReceived`, - (sum(`user_statistics`.`net_bytes_sent`) + sum(`user_statistics`.`current_bytes_sent`)) AS `bytesSent` -from - `user_statistics` -group by - `user_statistics`.`account_id`; diff --git a/engine/schema/src/main/resources/META-INF/db/schema-42210to42220.sql b/engine/schema/src/main/resources/META-INF/db/schema-42210to42220.sql new file mode 100644 index 000000000000..8db41ed2e294 --- /dev/null +++ b/engine/schema/src/main/resources/META-INF/db/schema-42210to42220.sql @@ -0,0 +1,21 @@ +-- Licensed to the Apache Software Foundation (ASF) under one +-- or more contributor license agreements. See the NOTICE file +-- distributed with this work for additional information +-- regarding copyright ownership. The ASF licenses this file +-- to you under the Apache License, Version 2.0 (the +-- "License"); you may not use this file except in compliance +-- with the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, +-- software distributed under the License is distributed on an +-- "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +-- KIND, either express or implied. See the License for the +-- specific language governing permissions and limitations +-- under the License. + +--; +-- Schema upgrade from 4.22.1.0 to 4.22.2.0 +--; + diff --git a/engine/schema/src/main/resources/META-INF/db/views/cloud.account_view.sql b/engine/schema/src/main/resources/META-INF/db/views/cloud.account_view.sql index edc164c40cbd..f988e5910e2e 100644 --- a/engine/schema/src/main/resources/META-INF/db/views/cloud.account_view.sql +++ b/engine/schema/src/main/resources/META-INF/db/views/cloud.account_view.sql @@ -39,8 +39,8 @@ select `data_center`.`id` AS `data_center_id`, `data_center`.`uuid` AS `data_center_uuid`, `data_center`.`name` AS `data_center_name`, - `account_netstats_view`.`bytesReceived` AS `bytesReceived`, - `account_netstats_view`.`bytesSent` AS `bytesSent`, + (SELECT COALESCE(SUM(`us`.`net_bytes_received` + `us`.`current_bytes_received`), 0) FROM `cloud`.`user_statistics` `us` WHERE `us`.`account_id` = `account`.`id`) AS `bytesReceived`, + (SELECT COALESCE(SUM(`us`.`net_bytes_sent` + `us`.`current_bytes_sent`), 0) FROM `cloud`.`user_statistics` `us` WHERE `us`.`account_id` = `account`.`id`) AS `bytesSent`, `vmlimit`.`max` AS `vmLimit`, `vmcount`.`count` AS `vmTotal`, `runningvm`.`vmcount` AS `runningVms`,