Add index for Maps table (because SQLite execution planner is stupid)

This commit is contained in:
Mike Primm 2022-03-27 13:59:39 -05:00
parent 7feb9e6735
commit d93b836ad0
4 changed files with 78 additions and 7 deletions

View File

@ -483,9 +483,10 @@ public class MicrosoftSQLMapStorage extends MapStorage {
doUpdate(c, "CREATE TABLE " + tableMarkerIcons + " (IconName VARCHAR(128) PRIMARY KEY NOT NULL, Image varbinary(MAX))");
doUpdate(c, "CREATE TABLE " + tableMarkerFiles + " (FileName VARCHAR(128) PRIMARY KEY NOT NULL, Content varchar(MAX))");
doUpdate(c, "CREATE TABLE " + tableStandaloneFiles + " (FileName VARCHAR(128) NOT NULL, ServerID BIGINT NOT NULL DEFAULT 0, Content varchar(MAX), PRIMARY KEY (FileName, ServerID))");
doUpdate(c, "CREATE INDEX " + tableMaps + "_idx ON " + tableMaps + "(WorldID, MapID, Variant, ServerID)");
doUpdate(c, "CREATE TABLE " + tableSchemaVersion + " (level INT PRIMARY KEY NOT NULL)");
doUpdate(c, "INSERT INTO " + tableSchemaVersion + " (level) VALUES (5)");
version = 5; // Initial - we have all the following updates already
doUpdate(c, "INSERT INTO " + tableSchemaVersion + " (level) VALUES (6)");
version = 6; // Initial - we have all the following updates already
} catch (SQLException x) {
logSQLException("Error creating tables", x);
err = true;
@ -495,6 +496,23 @@ public class MicrosoftSQLMapStorage extends MapStorage {
c = null;
}
}
// Skip 1-5 - goofed up and released with 5 as initial version
if (version == 5) {
try {
Log.info("Updating database schema from version = " + version);
c = getConnection();
doUpdate(c, "CREATE INDEX " + tableMaps + "_idx ON " + tableMaps + "(WorldID, MapID, Variant, ServerID)");
doUpdate(c, "UPDATE " + tableSchemaVersion + " SET level=6 WHERE level = 5;");
version = 2;
} catch (SQLException x) {
logSQLException("Error updating tables to version=6", x);
err = true;
return false;
} finally {
releaseConnection(c, err);
c = null;
}
}
Log.info("Schema version = " + version);
// Load maps table - cache results
doLoadMaps();

View File

@ -484,9 +484,10 @@ public class MySQLMapStorage extends MapStorage {
doUpdate(c, "CREATE TABLE " + tableMarkerIcons + " (IconName VARCHAR(128) PRIMARY KEY NOT NULL, Image MEDIUMBLOB)");
doUpdate(c, "CREATE TABLE " + tableMarkerFiles + " (FileName VARCHAR(128) PRIMARY KEY NOT NULL, Content MEDIUMTEXT)");
doUpdate(c, "CREATE TABLE " + tableStandaloneFiles + " (FileName VARCHAR(128) NOT NULL, ServerID BIGINT NOT NULL DEFAULT 0, Content MEDIUMTEXT, PRIMARY KEY (FileName, ServerID))");
doUpdate(c, "CREATE INDEX " + tableMaps + "_idx ON " + tableMaps + "(WorldID, MapID, Variant, ServerID)");
doUpdate(c, "CREATE TABLE " + tableSchemaVersion + " (level INT PRIMARY KEY NOT NULL)");
doUpdate(c, "INSERT INTO " + tableSchemaVersion + " (level) VALUES (5)");
version = 5; // Initial - we have all the following updates already
doUpdate(c, "INSERT INTO " + tableSchemaVersion + " (level) VALUES (6)");
version = 6; // Initial - we have all the following updates already
} catch (SQLException x) {
logSQLException("Error creating tables", x);
err = true;
@ -583,6 +584,22 @@ public class MySQLMapStorage extends MapStorage {
c = null;
}
}
if (version == 5) {
try {
Log.info("Updating database schema from version = " + version);
c = getConnection();
doUpdate(c, "CREATE INDEX " + tableMaps + "_idx ON " + tableMaps + "(WorldID, MapID, Variant, ServerID)");
doUpdate(c, "UPDATE " + tableSchemaVersion + " SET level=6 WHERE level = 5;");
version = 6;
} catch (SQLException x) {
logSQLException("Error updating tables to version=5", x);
err = true;
return false;
} finally {
releaseConnection(c, err);
c = null;
}
}
Log.info("Schema version = " + version);
// Load maps table - cache results
doLoadMaps();

View File

@ -466,9 +466,10 @@ public class PostgreSQLMapStorage extends MapStorage {
doUpdate(c, "CREATE TABLE " + tableMarkerIcons + " (IconName VARCHAR(128) PRIMARY KEY NOT NULL, Image BYTEA)");
doUpdate(c, "CREATE TABLE " + tableMarkerFiles + " (FileName VARCHAR(128) PRIMARY KEY NOT NULL, Content BYTEA)");
doUpdate(c, "CREATE TABLE " + tableStandaloneFiles + " (FileName VARCHAR(128) NOT NULL, ServerID BIGINT NOT NULL DEFAULT 0, Content BYTEA, PRIMARY KEY (FileName, ServerID))");
doUpdate(c, "CREATE INDEX " + tableMaps + "_idx ON " + tableMaps + "(WorldID, MapID, Variant, ServerID)");
doUpdate(c, "CREATE TABLE " + tableSchemaVersion + " (level INT PRIMARY KEY NOT NULL)");
doUpdate(c, "INSERT INTO " + tableSchemaVersion + " (level) VALUES (3)");
version = 3; // initialzed to current schema
version = 4; // initialzed to current schema
} catch (SQLException x) {
logSQLException("Error creating tables", x);
err = true;
@ -513,6 +514,22 @@ public class PostgreSQLMapStorage extends MapStorage {
c = null;
}
}
if (version == 3) {
try {
Log.info("Updating database schema from version = " + version);
c = getConnection();
doUpdate(c, "CREATE INDEX " + tableMaps + "_idx ON " + tableMaps + "(WorldID, MapID, Variant, ServerID)");
doUpdate(c, "UPDATE " + tableSchemaVersion + " SET level=4 WHERE level = 3;");
version = 4;
} catch (SQLException x) {
logSQLException("Error upgrading tables to version=4", x);
err = true;
return false;
} finally {
releaseConnection(c, err);
c = null;
}
}
Log.info("Schema version = " + version);
// Load maps table - cache results
doLoadMaps();

View File

@ -401,9 +401,11 @@ public class SQLiteMapStorage extends MapStorage {
doUpdate(c, "CREATE TABLE Faces (PlayerName STRING NOT NULL, TypeID INT NOT NULL, Image BLOB, ImageLen INT, PRIMARY KEY(PlayerName, TypeID))");
doUpdate(c, "CREATE TABLE MarkerIcons (IconName STRING PRIMARY KEY NOT NULL, Image BLOB, ImageLen INT)");
doUpdate(c, "CREATE TABLE MarkerFiles (FileName STRING PRIMARY KEY NOT NULL, Content CLOB)");
// Add index, since SQLite execution planner is stupid and scans Tiles table instead of using short Maps table...
doUpdate(c, "CREATE INDEX MapIndex ON Maps(WorldID, MapID, Variant)");
doUpdate(c, "CREATE TABLE SchemaVersion (level INT PRIMARY KEY NOT NULL)");
doUpdate(c, "INSERT INTO SchemaVersion (level) VALUES (2)");
version = 2; // Initializes to current schema
doUpdate(c, "INSERT INTO SchemaVersion (level) VALUES (3)");
version = 3; // Initializes to current schema
} catch (SQLException x) {
logSQLException("Error creating tables", x);
err = true;
@ -431,6 +433,23 @@ public class SQLiteMapStorage extends MapStorage {
c = null;
}
}
if (version == 2) {
try {
Log.info("Updating database schema from version = " + version);
c = getConnection();
// Add index, since SQLite execution planner is stupid and scans Tiles table instead of using short Maps table...
doUpdate(c, "CREATE INDEX MapIndex ON Maps(WorldID, MapID, Variant)");
doUpdate(c, "UPDATE SchemaVersion SET level=3");
version = 2;
} catch (SQLException x) {
logSQLException("Error updating tables to version=3", x);
err = true;
return false;
} finally {
releaseConnection(c, err);
c = null;
}
}
Log.info("Schema version = " + version);
// Load maps table - cache results
doLoadMaps();