WorldTimesTable table structure optimization

- Replaced user_id with uuid
- Replaced server_id with server_uuid
This commit is contained in:
Rsl1122 2018-12-12 20:57:16 +02:00
parent e3b0d8efa8
commit b4089cff28
6 changed files with 142 additions and 36 deletions

View File

@ -201,7 +201,8 @@ public abstract class SQLDB extends Database {
new IPAnonPatch(this),
new NicknameLastSeenPatch(this),
new VersionTableRemovalPatch(this),
new DiskUsagePatch(this)
new DiskUsagePatch(this),
new WorldTimesOptimizationPatch(this)
};
try {

View File

@ -0,0 +1,83 @@
/*
* This file is part of Player Analytics (Plan).
*
* Plan is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3 as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Plan is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Plan. If not, see <https://www.gnu.org/licenses/>.
*/
package com.djrapitops.plan.system.database.databases.sql.patches;
import com.djrapitops.plan.api.exceptions.database.DBOpException;
import com.djrapitops.plan.system.database.databases.sql.SQLDB;
import com.djrapitops.plan.system.database.databases.sql.tables.WorldTimesTable;
import com.djrapitops.plan.system.database.databases.sql.tables.WorldTimesTable.Col;
public class WorldTimesOptimizationPatch extends Patch {
private String tempTableName;
private String tableName;
public WorldTimesOptimizationPatch(SQLDB db) {
super(db);
tableName = WorldTimesTable.TABLE_NAME;
tempTableName = "temp_world_times";
}
@Override
public boolean hasBeenApplied() {
return hasColumn(tableName, Col.ID.get())
&& hasColumn(tableName, Col.UUID.get())
&& hasColumn(tableName, Col.SERVER_UUID.get())
&& !hasColumn(tableName, "user_id")
&& !hasColumn(tableName, "server_id")
&& !hasTable(tempTableName); // If this table exists the patch has failed to finish.
}
@Override
public void apply() {
try {
tempOldTable();
db.getWorldTimesTable().createTable();
db.execute("INSERT INTO " + tableName + " (" +
Col.UUID + ", " +
Col.SERVER_UUID + ", " +
Col.ADVENTURE + ", " +
Col.CREATIVE + ", " +
Col.SURVIVAL + ", " +
Col.SPECTATOR + ", " +
Col.SESSION_ID + ", " +
Col.WORLD_ID +
") SELECT " +
"(SELECT plan_users.uuid FROM plan_users WHERE plan_users.id = " + tempTableName + ".user_id LIMIT 1), " +
"(SELECT plan_servers.uuid FROM plan_servers WHERE plan_servers.id = " + tempTableName + ".server_id LIMIT 1), " +
Col.ADVENTURE + ", " +
Col.CREATIVE + ", " +
Col.SURVIVAL + ", " +
Col.SPECTATOR + ", " +
Col.SESSION_ID + ", " +
Col.WORLD_ID +
" FROM " + tempTableName
);
dropTable(tempTableName);
} catch (Exception e) {
throw new DBOpException(WorldTimesOptimizationPatch.class.getSimpleName() + " failed.", e);
}
}
private void tempOldTable() {
if (!hasTable(tempTableName)) {
renameTable(tableName, tempTableName);
}
}
}

View File

@ -35,8 +35,11 @@ public class WorldTimesSeverIDPatch extends Patch {
@Override
public boolean hasBeenApplied() {
String tableName = WorldTimesTable.TABLE_NAME;
String columnName = WorldTimesTable.Col.SERVER_ID.get();
return hasColumn(tableName, columnName)
String columnName = "server_id";
// WorldTimesOptimizationPatch makes this patch incompatible with newer patch versions.
return hasColumn(tableName, "server_uuid")
|| hasColumn(tableName, columnName)
&& allValuesHaveServerID(tableName, columnName);
}
@ -60,7 +63,7 @@ public class WorldTimesSeverIDPatch extends Patch {
Map<Integer, Integer> sessionIDServerIDRelation = db.getSessionsTable().getIDServerIDRelation();
String sql = "UPDATE " + WorldTimesTable.TABLE_NAME + " SET " +
WorldTimesTable.Col.SERVER_ID + "=?" +
"server_id=?" +
" WHERE " + WorldTimesTable.Col.SESSION_ID + "=?";
db.executeBatch(new ExecStatement(sql) {

View File

@ -41,7 +41,10 @@ public class WorldsServerIDPatch extends Patch {
public boolean hasBeenApplied() {
String tableName = WorldTable.TABLE_NAME;
String columnName = WorldTable.Col.SERVER_ID.get();
return hasColumn(tableName, columnName)
// WorldOptimizationPatch makes this patch incompatible with newer patch versions.
return hasColumn(tableName, "server_uuid")
|| hasColumn(tableName, columnName)
&& allValuesHaveServerID(tableName, columnName);
}
@ -134,7 +137,7 @@ public class WorldsServerIDPatch extends Patch {
String sql = "UPDATE " + worldTimesTable + " SET " +
WorldTimesTable.Col.WORLD_ID + "=?" +
" WHERE " + WorldTimesTable.Col.WORLD_ID + "=?" +
" AND " + WorldTimesTable.Col.SERVER_ID + "=?";
" AND " + "server_id=?";
db.executeBatch(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {

View File

@ -175,6 +175,7 @@ public class WorldTable extends Table {
public enum Col implements Column {
ID("id"),
SERVER_ID("server_id"),
SERVER_UUID("server_uuid"),
NAME("world_name");
private final String column;

View File

@ -59,35 +59,33 @@ public class WorldTimesTable extends UserIDTable {
sessionsTable = db.getSessionsTable();
serverTable = db.getServerTable();
insertStatement = "INSERT INTO " + tableName + " (" +
Col.USER_ID + ", " +
Col.UUID + ", " +
Col.WORLD_ID + ", " +
Col.SERVER_ID + ", " +
Col.SERVER_UUID + ", " +
Col.SESSION_ID + ", " +
Col.SURVIVAL + ", " +
Col.CREATIVE + ", " +
Col.ADVENTURE + ", " +
Col.SPECTATOR +
") VALUES (" +
usersTable.statementSelectID + ", " +
") VALUES (?, " +
worldTable.statementSelectID + ", " +
serverTable.statementSelectServerID + ", " +
"?, ?, ?, ?, ?)";
"?, ?, ?, ?, ?, ?)";
}
@Override
public void createTable() throws DBInitException {
createTable(TableSqlParser.createTable(tableName)
.column(Col.USER_ID, Sql.INT).notNull()
.primaryKeyIDColumn(supportsMySQLQueries, Col.ID)
.column(Col.UUID, Sql.varchar(36)).notNull()
.column(Col.WORLD_ID, Sql.INT).notNull()
.column(Col.SERVER_ID, Sql.INT).notNull()
.column(Col.SERVER_UUID, Sql.varchar(36)).notNull()
.column(Col.SESSION_ID, Sql.INT).notNull()
.column(Col.SURVIVAL, Sql.LONG).notNull().defaultValue("0")
.column(Col.CREATIVE, Sql.LONG).notNull().defaultValue("0")
.column(Col.ADVENTURE, Sql.LONG).notNull().defaultValue("0")
.column(Col.SPECTATOR, Sql.LONG).notNull().defaultValue("0")
.foreignKey(Col.USER_ID, usersTable.getTableName(), UsersTable.Col.ID)
.primaryKey(supportsMySQLQueries, Col.ID)
.foreignKey(Col.WORLD_ID, worldTable.getTableName(), WorldTable.Col.ID)
.foreignKey(Col.SERVER_ID, serverTable.getTableName(), ServerTable.Col.SERVER_ID)
.foreignKey(Col.SESSION_ID, sessionsTable.getTableName(), SessionsTable.Col.ID)
.toString()
);
@ -105,7 +103,7 @@ public class WorldTimesTable extends UserIDTable {
worldNameColumn +
" FROM " + tableName +
" INNER JOIN " + worldTable + " on " + worldIDColumn + "=" + Col.WORLD_ID +
" WHERE " + Col.USER_ID + "=" + usersTable.statementSelectID;
" WHERE " + Col.UUID + "=?";
query(new QueryStatement<Object>(sql, 2000) {
@Override
@ -185,7 +183,7 @@ public class WorldTimesTable extends UserIDTable {
worldNameColumn +
" FROM " + tableName +
" INNER JOIN " + worldTable + " on " + worldIDColumn + "=" + Col.WORLD_ID +
" WHERE " + tableName + "." + Col.SERVER_ID + "=" + db.getServerTable().statementSelectServerID +
" WHERE " + tableName + "." + Col.SERVER_UUID + "=?" +
" GROUP BY " + Col.WORLD_ID;
return query(new QueryStatement<WorldTimes>(sql, 1000) {
@ -227,7 +225,7 @@ public class WorldTimesTable extends UserIDTable {
worldNameColumn +
" FROM " + tableName +
" INNER JOIN " + worldTable + " on " + worldIDColumn + "=" + Col.WORLD_ID +
" WHERE " + Col.USER_ID + "=" + usersTable.statementSelectID +
" WHERE " + Col.UUID + "=?" +
" GROUP BY " + Col.WORLD_ID;
return query(new QueryStatement<WorldTimes>(sql) {
@ -366,9 +364,43 @@ public class WorldTimesTable extends UserIDTable {
});
}
@Override
public void removeUser(UUID uuid) {
String sql = "DELETE FROM " + tableName + " WHERE (" + Col.UUID + "=?)";
execute(new ExecStatement(sql) {
@Override
public void prepare(PreparedStatement statement) throws SQLException {
statement.setString(1, uuid.toString());
}
});
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof WorldTimesTable)) return false;
if (!super.equals(o)) return false;
WorldTimesTable that = (WorldTimesTable) o;
return Objects.equals(serverTable, that.serverTable) &&
Objects.equals(worldTable, that.worldTable) &&
Objects.equals(sessionsTable, that.sessionsTable) &&
Objects.equals(insertStatement, that.insertStatement);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), serverTable, worldTable, sessionsTable, insertStatement);
}
public enum Col implements Column {
ID("id"),
@Deprecated
USER_ID(UserIDTable.Col.USER_ID.get()),
UUID("uuid"),
@Deprecated
SERVER_ID("server_id"),
SERVER_UUID("server_uuid"),
SESSION_ID("session_id"),
WORLD_ID("world_id"),
SURVIVAL("survival_time"),
@ -392,21 +424,4 @@ public class WorldTimesTable extends UserIDTable {
return column;
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof WorldTimesTable)) return false;
if (!super.equals(o)) return false;
WorldTimesTable that = (WorldTimesTable) o;
return Objects.equals(serverTable, that.serverTable) &&
Objects.equals(worldTable, that.worldTable) &&
Objects.equals(sessionsTable, that.sessionsTable) &&
Objects.equals(insertStatement, that.insertStatement);
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), serverTable, worldTable, sessionsTable, insertStatement);
}
}