mirror of https://github.com/apache/cloudstack.git
events,alerts: Add missing indexes (#8276)
This PR adds missing indexes on `alerts` & `events` tables.
For alerts table, some of the queries are part of a couple of APIs and some operations. I have added the index for the same. Ref:
8f39087377/engine/schema/src/main/java/com/cloud/alert/dao/AlertDaoImpl.java (L40-L45)
For Events table, we query for `resource_id` & `resource_type` in the UI for a resource's events. Indexes were missing, so I have added those.
This commit is contained in:
parent
080a5aee00
commit
a791d46abe
|
|
@ -21,6 +21,7 @@ import java.sql.PreparedStatement;
|
|||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public class DatabaseAccessObject {
|
||||
|
|
@ -85,8 +86,8 @@ public class DatabaseAccessObject {
|
|||
return columnExists;
|
||||
}
|
||||
|
||||
public String generateIndexName(String tableName, String columnName) {
|
||||
return String.format("i_%s__%s", tableName, columnName);
|
||||
public String generateIndexName(String tableName, String... columnName) {
|
||||
return String.format("i_%s__%s", tableName, StringUtils.join(columnName, "__"));
|
||||
}
|
||||
|
||||
public boolean indexExists(Connection conn, String tableName, String indexName) {
|
||||
|
|
@ -101,8 +102,8 @@ public class DatabaseAccessObject {
|
|||
return false;
|
||||
}
|
||||
|
||||
public void createIndex(Connection conn, String tableName, String columnName, String indexName) {
|
||||
String stmt = String.format("CREATE INDEX %s on %s (%s)", indexName, tableName, columnName);
|
||||
public void createIndex(Connection conn, String tableName, String indexName, String... columnNames) {
|
||||
String stmt = String.format("CREATE INDEX %s ON %s (%s)", indexName, tableName, StringUtils.join(columnNames, ", "));
|
||||
s_logger.debug("Statement: " + stmt);
|
||||
try (PreparedStatement pstmt = conn.prepareStatement(stmt)) {
|
||||
pstmt.execute();
|
||||
|
|
|
|||
|
|
@ -23,11 +23,11 @@ public class DbUpgradeUtils {
|
|||
|
||||
private static DatabaseAccessObject dao = new DatabaseAccessObject();
|
||||
|
||||
public static void addIndexIfNeeded(Connection conn, String tableName, String columnName) {
|
||||
String indexName = dao.generateIndexName(tableName, columnName);
|
||||
public static void addIndexIfNeeded(Connection conn, String tableName, String... columnNames) {
|
||||
String indexName = dao.generateIndexName(tableName, columnNames);
|
||||
|
||||
if (!dao.indexExists(conn, tableName, indexName)) {
|
||||
dao.createIndex(conn, tableName, columnName, indexName);
|
||||
dao.createIndex(conn, tableName, indexName, columnNames);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -76,6 +76,7 @@ public class Upgrade41810to41900 implements DbUpgrade, DbUpgradeSystemVmTemplate
|
|||
public void performDataMigration(Connection conn) {
|
||||
decryptConfigurationValuesFromAccountAndDomainScopesNotInSecureHiddenCategories(conn);
|
||||
migrateBackupDates(conn);
|
||||
addIndexes(conn);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -254,4 +255,11 @@ public class Upgrade41810to41900 implements DbUpgrade, DbUpgradeSystemVmTemplate
|
|||
}
|
||||
}
|
||||
|
||||
private void addIndexes(Connection conn) {
|
||||
DbUpgradeUtils.addIndexIfNeeded(conn, "alert", "archived", "created");
|
||||
DbUpgradeUtils.addIndexIfNeeded(conn, "alert", "type", "data_center_id", "pod_id");
|
||||
|
||||
DbUpgradeUtils.addIndexIfNeeded(conn, "event", "resource_type", "resource_id");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -93,8 +93,8 @@ public class DatabaseAccessObjectTest {
|
|||
|
||||
@Test
|
||||
public void generateIndexNameTest() {
|
||||
String indexName = dao.generateIndexName("mytable","mycolumn");
|
||||
Assert.assertEquals( "i_mytable__mycolumn", indexName);
|
||||
String indexName = dao.generateIndexName("mytable","mycolumn1", "mycolumn2");
|
||||
Assert.assertEquals( "i_mytable__mycolumn1__mycolumn2", indexName);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
|
@ -136,10 +136,11 @@ public class DatabaseAccessObjectTest {
|
|||
|
||||
Connection conn = connectionMock;
|
||||
String tableName = "mytable";
|
||||
String columnName = "mycolumn";
|
||||
String columnName1 = "mycolumn1";
|
||||
String columnName2 = "mycolumn2";
|
||||
String indexName = "myindex";
|
||||
|
||||
dao.createIndex(conn, tableName, columnName, indexName);
|
||||
dao.createIndex(conn, tableName, indexName, columnName1, columnName2);
|
||||
verify(connectionMock, times(1)).prepareStatement(anyString());
|
||||
verify(preparedStatementMock, times(1)).execute();
|
||||
verify(preparedStatementMock, times(1)).close();
|
||||
|
|
|
|||
Loading…
Reference in New Issue