Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up sensors and related tables and add constraints #3745

Merged
merged 2 commits into from Jun 30, 2016

Conversation

murrant
Copy link
Member

@murrant murrant commented Jun 29, 2016

Remove sensors for non existent devices and delete unused sensors_to_state_indexes, state_indexes, and state_translations
Change sensors.device_id to unsigned to match devices.device_id
Add foreign keys to sensors and sensors_to_state_indexes for cascading deletes

229 rows affected. (Query took 0.2732 seconds.)

DELETE FROM `sensors_to_state_indexes` WHERE `sensors_to_state_indexes`.`sensor_id` NOT IN (SELECT DISTINCT `sensors`.`sensor_id` FROM `sensors` INNER JOIN `devices` ON `sensors`.`device_id` = `devices`.`device_id`)

384 rows affected. (Query took 0.0410 seconds.)

DELETE FROM `sensors` WHERE `sensors`.`device_id` NOT IN (SELECT `device_id` FROM `devices`)

4 rows affected. (Query took 0.1045 seconds.)

DELETE FROM `state_indexes` WHERE `state_indexes`.`state_index_id` NOT IN (SELECT `sensors_to_state_indexes`.`state_index_id` FROM `sensors_to_state_indexes`)

26 rows affected. (Query took 0.0275 seconds.)

DELETE FROM `state_translations` WHERE `state_translations`.`state_index_id` NOT IN (SELECT `state_indexes`.`state_index_id` FROM `state_indexes`)

MySQL returned an empty result set (i.e. zero rows). (Query took 1.1063 seconds.)

ALTER TABLE `sensors` CHANGE `device_id` `device_id` INT(11) UNSIGNED NOT NULL DEFAULT '0'

MySQL returned an empty result set (i.e. zero rows). (Query took 1.2088 seconds.)

ALTER TABLE `sensors` ADD CONSTRAINT `sensors_device_id_foreign` FOREIGN KEY (`device_id`) REFERENCES `devices` (`device_id`) ON DELETE CASCADE

MySQL returned an empty result set (i.e. zero rows). (Query took 1.1473 seconds.)

ALTER TABLE `sensors_to_state_indexes` ADD CONSTRAINT `sensors_to_state_indexes_sensor_id_foreign` FOREIGN KEY (`sensor_id`) REFERENCES `sensors` (`sensor_id`) ON DELETE CASCADE

Remove sensors for non existent devices and delete unused sensors_to_state_indexes, state_indexes, and state_translations
Change sensors.device_id to unsigned to match devices.device_id
Add foreign keys to sensors and sensors_to_state_indexes for cascading deletes
@LibreNMS-CI
Copy link

Auto-Deploy finished, Test PR at http://3745.ci.librenms.org or https://3745.ci.librenms.org

@murrant murrant added the Schema label Jun 29, 2016
@laf
Copy link
Member

laf commented Jun 30, 2016

+1 from me:

mysql> DELETE FROM `sensors_to_state_indexes` WHERE `sensors_to_state_indexes`.`sensor_id` NOT IN (SELECT DISTINCT `sensors`.`sensor_id` FROM `sensors` INNER JOIN `devices` ON `sensors`.`device_id` = `devices`.`device_id`);
Query OK, 16 rows affected (0.02 sec)

mysql> DELETE FROM `sensors` WHERE `sensors`.`device_id` NOT IN (SELECT `device_id` FROM `devices`);
Query OK, 21 rows affected (0.01 sec)

mysql> DELETE FROM `state_indexes` WHERE `state_indexes`.`state_index_id` NOT IN (SELECT `sensors_to_state_indexes`.`state_index_id` FROM `sensors_to_state_indexes`);
Query OK, 0 rows affected (0.00 sec)

mysql> DELETE FROM `state_translations` WHERE `state_translations`.`state_index_id` NOT IN (SELECT `state_indexes`.`state_index_id` FROM `state_indexes`);
Query OK, 0 rows affected (0.00 sec)

mysql> ALTER TABLE `sensors` CHANGE `device_id` `device_id` INT(11) UNSIGNED NOT NULL DEFAULT '0';
Query OK, 6311 rows affected (1.63 sec)
Records: 6311  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE `sensors` ADD CONSTRAINT `sensors_device_id_foreign` FOREIGN KEY (`device_id`) REFERENCES `devices` (`device_id`) ON DELETE CASCADE;
Query OK, 6311 rows affected (0.95 sec)
Records: 6311  Duplicates: 0  Warnings: 0

mysql> ALTER TABLE `sensors_to_state_indexes` ADD CONSTRAINT `sensors_to_state_indexes_sensor_id_foreign` FOREIGN KEY (`sensor_id`) REFERENCES `sensors` (`sensor_id`) ON DELETE CASCADE;
Query OK, 1750 rows affected (0.14 sec)
Records: 1750  Duplicates: 0  Warnings: 0

@LibreNMS-CI
Copy link

Auto-Deploy finished, Test PR at http://3745.ci.librenms.org or https://3745.ci.librenms.org

@laf laf merged commit c684228 into librenms:master Jun 30, 2016
@murrant murrant deleted the sensor_cleanup branch July 1, 2016 20:46
@murrant murrant restored the sensor_cleanup branch July 1, 2016 20:46
@murrant murrant deleted the sensor_cleanup branch July 1, 2016 20:46
@murrant murrant mentioned this pull request Jul 6, 2016
@lock lock bot locked as resolved and limited conversation to collaborators Jan 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants