Commit 69e193fa authored by Rafael Monnerat's avatar Rafael Monnerat

slapos/collect: Create index for speed up garbage collection

  vacuum as little as possible, as it is a heavy operation to perform.
parent a9e40960
Pipeline #7750 passed with stage
in 0 seconds
...@@ -52,6 +52,9 @@ class Database: ...@@ -52,6 +52,9 @@ class Database:
CREATE_USER_PARTITION_DATE_TIME_INDEX = "CREATE INDEX IF NOT EXISTS user_partition_date_time ON"\ CREATE_USER_PARTITION_DATE_TIME_INDEX = "CREATE INDEX IF NOT EXISTS user_partition_date_time ON"\
" user (partition, date, time)" " user (partition, date, time)"
CREATE_GC_DATE_REPORTED_INDEX = "CREATE INDEX IF NOT EXISTS %s_date_reported_index ON"\
" %s (date, reported)"
CREATE_FOLDER_TABLE = "create table if not exists folder "\ CREATE_FOLDER_TABLE = "create table if not exists folder "\
"(partition text, disk_used real, date text, " \ "(partition text, disk_used real, date text, " \
" time text, reported integer NULL DEFAULT 0)" " time text, reported integer NULL DEFAULT 0)"
...@@ -167,6 +170,9 @@ class Database: ...@@ -167,6 +170,9 @@ class Database:
self._execute(self.CREATE_TEMPERATURE_TABLE) self._execute(self.CREATE_TEMPERATURE_TABLE)
self._execute(self.CREATE_HEATING_TABLE) self._execute(self.CREATE_HEATING_TABLE)
self.commit() self.commit()
for table in self.getTableList():
self._execute(self.CREATE_GC_DATE_REPORTED_INDEX % (table, table))
self.commit()
self.close() self.close()
def _getInsertionDateTuple(self): def _getInsertionDateTuple(self):
...@@ -282,15 +288,20 @@ class Database: ...@@ -282,15 +288,20 @@ class Database:
where_clause = "reported = 1" where_clause = "reported = 1"
for _date in date_list: for _date in date_list:
where_clause += " AND date != '%s' " % _date where_clause += " AND date != '%s' " % _date
vacuum = 0
delete_sql = "DELETE FROM %s WHERE %s" delete_sql = "DELETE FROM %s WHERE %s"
select_sql = "SELECT date FROM %s WHERE %s LIMIT 1"
self.connect() self.connect()
for table in self.getTableList(): for table in self.getTableList():
if table not in self.preserve_table_list: if table not in self.preserve_table_list:
self._execute(delete_sql % (table, where_clause)) if self._execute(select_sql % (table, where_clause)).fetchone() is not None:
self._execute(delete_sql % (table, where_clause))
vacuum = 1
self._execute("VACUUM;") if vacuum:
self._execute("VACUUM;")
self.commit() self.commit()
self.close() self.close()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment