From 23c3c917a734aef877141e6236ba50bd1d6f848b Mon Sep 17 00:00:00 2001 From: Rustypredator Date: Sun, 5 May 2019 15:15:32 +0200 Subject: [PATCH 1/2] Update DB Structure Reference Issue: #49 The Changes make sure, that you do'nt have duplicate data in the Database by separating the Data, that changes from the data that is static. That way you'll also save space (a little). e.g. every player would only need to be created once. the Ping data will go into `player_data` same with worlds. if there are any questions i'll be happy to answer :) --- src/main/resources/create.sql | 113 ++++++++++++++++++++-------------- 1 file changed, 67 insertions(+), 46 deletions(-) diff --git a/src/main/resources/create.sql b/src/main/resources/create.sql index 464e5b2..650d2e7 100644 --- a/src/main/resources/create.sql +++ b/src/main/resources/create.sql @@ -1,51 +1,72 @@ -# LagMonitor table +CREATE TABLE IF NOT EXISTS `{prefix}monitor` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `process_usage` float unsigned NOT NULL, + `os_usage` float unsigned NOT NULL, + `free_ram` mediumint(8) unsigned NOT NULL, + `free_ram_pct` float unsigned NOT NULL, + `os_free_ram` mediumint(8) unsigned NOT NULL, + `os_free_ram_pct` float unsigned NOT NULL, + `load_avg` float unsigned NOT NULL, + `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + +CREATE TABLE IF NOT EXISTS `{prefix}native` ( + `native_id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `mc_read` smallint(5) unsigned DEFAULT NULL, + `mc_write` smallint(5) unsigned DEFAULT NULL, + `free_space` int(10) unsigned DEFAULT NULL, + `free_space_pct` float unsigned DEFAULT NULL, + `disk_read` smallint(5) unsigned DEFAULT NULL, + `disk_write` smallint(5) unsigned DEFAULT NULL, + `net_read` smallint(5) unsigned DEFAULT NULL, + `net_write` smallint(5) unsigned DEFAULT NULL, + `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`native_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + CREATE TABLE IF NOT EXISTS `{prefix}tps` ( - tps_id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT, - tps FLOAT UNSIGNED NOT NULL, - updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP -); + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `tps` float unsigned NOT NULL, + `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -CREATE TABLE IF NOT EXISTS `{prefix}monitor` ( - monitor_id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT, - process_usage FLOAT UNSIGNED NOT NULL, - os_usage FLOAT UNSIGNED NOT NULL, - free_ram MEDIUMINT UNSIGNED NOT NULL, - free_ram_pct FLOAT UNSIGNED NOT NULL, - os_free_ram MEDIUMINT UNSIGNED NOT NULL, - os_free_ram_pct FLOAT UNSIGNED NOT NULL, - load_avg FLOAT UNSIGNED NOT NULL, - updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP -); +CREATE TABLE IF NOT EXISTS `{prefix}player` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `uuid` char(40) NOT NULL, + `name` varchar(16) NOT NULL, + `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `uuid` (`uuid`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -CREATE TABLE IF NOT EXISTS `{prefix}worlds` ( - world_id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT, - monitor_id INTEGER UNSIGNED NOT NULL, - world_name VARCHAR(255) NOT NULL, - chunks_loaded SMALLINT UNSIGNED NOT NULL, - tile_entities SMALLINT UNSIGNED NOT NULL, - world_size MEDIUMINT UNSIGNED NOT NULL, - entities INT UNSIGNED NOT NULL, - FOREIGN KEY (monitor_id) REFERENCES `{prefix}monitor` (monitor_id) -); +CREATE TABLE IF NOT EXISTS `{prefix}world` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(255) NOT NULL, + `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -CREATE TABLE IF NOT EXISTS `{prefix}players` ( - world_id INTEGER UNSIGNED, - uuid CHAR(40) NOT NULL, - NAME VARCHAR(16) NOT NULL, - ping SMALLINT UNSIGNED NOT NULL, - PRIMARY KEY (world_id, uuid), - FOREIGN KEY (world_id) REFERENCES `{prefix}worlds` (world_id) -); +CREATE TABLE IF NOT EXISTS `{prefix}player_data` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `player` int(10) unsigned NOT NULL, + `world` int(10) unsigned NOT NULL, + `ping` int(10) unsigned NOT NULL DEFAULT '0', + `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + FOREIGN KEY (`player`) REFERENCES `{prefix}player` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, + FOREIGN KEY (`world`) REFERENCES `{prefix}world` (`id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; -CREATE TABLE IF NOT EXISTS `{prefix}native` ( - native_id INTEGER UNSIGNED PRIMARY KEY AUTO_INCREMENT, - mc_read SMALLINT UNSIGNED, - mc_write SMALLINT UNSIGNED, - free_space INT UNSIGNED, - free_space_pct FLOAT UNSIGNED, - disk_read SMALLINT UNSIGNED, - disk_write SMALLINT UNSIGNED, - net_read SMALLINT UNSIGNED, - net_write SMALLINT UNSIGNED, - updated TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP -); +CREATE TABLE IF NOT EXISTS `{prefix}world_data` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `world` int(10) unsigned NOT NULL, + `chunks_loaded` smallint(5) unsigned NOT NULL, + `tile_entities` smallint(5) unsigned NOT NULL, + `world_size` mediumint(8) unsigned NOT NULL, + `entities` int(10) unsigned NOT NULL, + `updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + FOREIGN KEY (`world`) REFERENCES `{prefix}world` (`id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; From 7f95956d01c8f83a300d01cf6796fc9203720341 Mon Sep 17 00:00:00 2001 From: Rustypredator Date: Sun, 5 May 2019 15:26:40 +0200 Subject: [PATCH 2/2] id Replaced `native_id` with `id`. It makes it easier to use the db, when you use the format `tablename.fieldname` e.g: `prefix_native.id` instead of `native.native_id`. --- src/main/resources/create.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/create.sql b/src/main/resources/create.sql index 650d2e7..dc73602 100644 --- a/src/main/resources/create.sql +++ b/src/main/resources/create.sql @@ -12,7 +12,7 @@ CREATE TABLE IF NOT EXISTS `{prefix}monitor` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; CREATE TABLE IF NOT EXISTS `{prefix}native` ( - `native_id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `mc_read` smallint(5) unsigned DEFAULT NULL, `mc_write` smallint(5) unsigned DEFAULT NULL, `free_space` int(10) unsigned DEFAULT NULL,