From 47e005db533cbd9b2e40efc0adb276b677e228e2 Mon Sep 17 00:00:00 2001 From: Koushik Das Date: Thu, 2 Jul 2015 12:11:53 +0530 Subject: [PATCH] CLOUDSTACK-8606: DB performance impacted due to VM sync. VM sync. generates a lot of queries on vm_instance table with 'instance_name' as filter. Since the field is not indexed, these kind of queries will impact DB performance. Added an index for instance_name field. --- .../cloud/upgrade/dao/Upgrade452to460.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade452to460.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade452to460.java index 9c2b1e3fae9e..0ad26035ca7d 100644 --- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade452to460.java +++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade452to460.java @@ -22,6 +22,8 @@ import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; import org.apache.log4j.Logger; @@ -59,6 +61,7 @@ public File[] getPrepareScripts() { @Override public void performDataMigration(final Connection conn) { updateVMInstanceUserId(conn); + addIndexForVMInstance(conn); } public void updateVMInstanceUserId(final Connection conn) { @@ -136,6 +139,22 @@ private void removeBumPriorityColumn(final Connection conn) { } } + private void addIndexForVMInstance(Connection conn) { + // Drop index if it exists + List indexList = new ArrayList(); + s_logger.debug("Dropping index i_vm_instance__instance_name from vm_instance table if it exists"); + indexList.add("i_vm_instance__instance_name"); + DbUpgradeUtils.dropKeysIfExist(conn, "vm_instance", indexList, false); + + // Now add index + try (PreparedStatement pstmt = conn.prepareStatement("ALTER TABLE `cloud`.`vm_instance` ADD INDEX `i_vm_instance__instance_name`(`instance_name`)");) { + pstmt.executeUpdate(); + s_logger.debug("Added index i_vm_instance__instance_name to vm_instance table"); + } catch (SQLException e) { + throw new CloudRuntimeException("Unable to add index i_vm_instance__instance_name to vm_instance table for the column instance_name", e); + } + } + @Override public File[] getCleanupScripts() { final String script = Script.findScript("", "db/schema-452to460-cleanup.sql");