Speed up Logbook and reduce size of database

I run this bash script every 10 mins by crons job to reduce the rows and database size by over 95%. I think it speeds up the logbook. I don’t think the deleted entries are actually used by the logbook. My just switched from mysql to mariodb but it should work with both. My sql user is hass with password hass.

#!/bin/sh
mysql -u hass -phass -e 'flush hosts';
mysql -uhass -phass HOMEBASE --execute="USE HOMEBASE";
mysql -uhass -phass HOMEBASE --execute="DELETE FROM events WHERE event_type = 'service_removed'";
mysql -uhass -phass HOMEBASE --execute="DELETE FROM events WHERE event_type = 'service_executed'";
mysql -uhass -phass HOMEBASE --execute="DELETE FROM events WHERE event_type = 'platform_discovered'";
mysql -uhass -phass HOMEBASE --execute="DELETE FROM events WHERE event_type = 'homeassistant_start'";
mysql -uhass -phass HOMEBASE --execute="DELETE FROM events WHERE event_type = 'feedreader'";
mysql -uhass -phass HOMEBASE --execute="DELETE FROM events WHERE event_type = 'service_registered'";
mysql -uhass -phass HOMEBASE --execute="DELETE FROM events WHERE event_type = 'call_service'";
mysql -uhass -phass HOMEBASE --execute="DELETE FROM events WHERE event_type = 'component_loaded'";
mysql -uhass -phass HOMEBASE --execute="DELETE FROM events WHERE event_type = 'logbook_entry'";
mysql -uhass -phass HOMEBASE --execute="DELETE FROM events WHERE event_type = 'system_log_event'";
mysql -uhass -phass HOMEBASE --execute="OPTIMIZE TABLE EVENTS;"
1 Like

Good work.

Do not try this with sqlite!