From d93b836ad06d35710219899a6e5229f4bd36f2b3 Mon Sep 17 00:00:00 2001 From: Mike Primm Date: Sun, 27 Mar 2022 13:59:39 -0500 Subject: [PATCH] Add index for Maps table (because SQLite execution planner is stupid) --- .../storage/mssql/MicrosoftSQLMapStorage.java | 22 ++++++++++++++++-- .../dynmap/storage/mysql/MySQLMapStorage.java | 21 +++++++++++++++-- .../postgresql/PostgreSQLMapStorage.java | 19 ++++++++++++++- .../storage/sqllte/SQLiteMapStorage.java | 23 +++++++++++++++++-- 4 files changed, 78 insertions(+), 7 deletions(-) diff --git a/DynmapCore/src/main/java/org/dynmap/storage/mssql/MicrosoftSQLMapStorage.java b/DynmapCore/src/main/java/org/dynmap/storage/mssql/MicrosoftSQLMapStorage.java index deb60083..96b00510 100644 --- a/DynmapCore/src/main/java/org/dynmap/storage/mssql/MicrosoftSQLMapStorage.java +++ b/DynmapCore/src/main/java/org/dynmap/storage/mssql/MicrosoftSQLMapStorage.java @@ -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(); diff --git a/DynmapCore/src/main/java/org/dynmap/storage/mysql/MySQLMapStorage.java b/DynmapCore/src/main/java/org/dynmap/storage/mysql/MySQLMapStorage.java index fdf45679..fd980bf1 100644 --- a/DynmapCore/src/main/java/org/dynmap/storage/mysql/MySQLMapStorage.java +++ b/DynmapCore/src/main/java/org/dynmap/storage/mysql/MySQLMapStorage.java @@ -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(); diff --git a/DynmapCore/src/main/java/org/dynmap/storage/postgresql/PostgreSQLMapStorage.java b/DynmapCore/src/main/java/org/dynmap/storage/postgresql/PostgreSQLMapStorage.java index 260147bc..361e445a 100644 --- a/DynmapCore/src/main/java/org/dynmap/storage/postgresql/PostgreSQLMapStorage.java +++ b/DynmapCore/src/main/java/org/dynmap/storage/postgresql/PostgreSQLMapStorage.java @@ -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(); diff --git a/DynmapCore/src/main/java/org/dynmap/storage/sqllte/SQLiteMapStorage.java b/DynmapCore/src/main/java/org/dynmap/storage/sqllte/SQLiteMapStorage.java index e72dcb0b..744881c8 100644 --- a/DynmapCore/src/main/java/org/dynmap/storage/sqllte/SQLiteMapStorage.java +++ b/DynmapCore/src/main/java/org/dynmap/storage/sqllte/SQLiteMapStorage.java @@ -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();