core (memory/sqlite): further improvments in update_v7:

- "info" messages deletion is now done by deactivating foreign keys. This seriously boost performances.
- "VACUUM" is done at the end to reduce database size.
This commit is contained in:
Goffi 2018-12-12 00:53:14 +01:00
commit 0e70587857

View file

@ -1192,9 +1192,10 @@ class Updater(object):
@defer.inlineCallbacks
def update_v7(self):
"""Update database from v6 to v7 (history unique constraint change)"""
log.info(u"Database update to v7, this may take a while")
log.info(u"Database update to v7, this may be long depending on your history "
u"size, please be patient.")
log.info(u"some cleaning first")
log.info(u"Some cleaning first")
# we need to fix duplicate stanza_id, as it can result in conflicts with the new schema
# normally database should not contain any, but better safe than sorry.
rows = yield self.dbpool.runQuery(
@ -1213,8 +1214,25 @@ class Updater(object):
"DELETE FROM history WHERE uid IN ({})".format(u",".join(u"?"*len(uids))),
uids)
log.info(u'Deleting "info" messages (this can take a while, please be patient)')
yield self.dbpool.runQuery(u"DELETE FROM history where type='info'");
def deleteInfo(txn):
# with foreign_keys on, the delete takes ages, so we deactivate it here
# the time to delete info messages from history.
txn.execute("PRAGMA foreign_keys = OFF")
txn.execute(u"DELETE FROM message WHERE history_uid IN (SELECT uid FROM history WHERE "
u"type='info')")
txn.execute(u"DELETE FROM subject WHERE history_uid IN (SELECT uid FROM history WHERE "
u"type='info')")
txn.execute(u"DELETE FROM thread WHERE history_uid IN (SELECT uid FROM history WHERE "
u"type='info')")
txn.execute(u"DELETE FROM message WHERE history_uid IN (SELECT uid FROM history WHERE "
u"type='info')")
txn.execute(u"DELETE FROM history WHERE type='info'")
# not sure that is is necessary to reactivate here, but in doubt…
txn.execute("PRAGMA foreign_keys = ON")
log.info(u'Deleting "info" messages (this can take a while)')
yield self.dbpool.runInteraction(deleteInfo)
log.info(u"Cleaning done")
# we have to rename table we will replace
@ -1279,12 +1297,15 @@ class Updater(object):
u", history_uid, thread_id, parent_id FROM thread_old")
yield self.dbpool.runQuery(query)
log.info(u"\nRemoving old tables")
# because of foreign keys, tables referencing history_old
# must be deleted first
yield self.dbpool.runQuery("DROP TABLE thread_old")
yield self.dbpool.runQuery("DROP TABLE subject_old")
yield self.dbpool.runQuery("DROP TABLE message_old")
yield self.dbpool.runQuery("DROP TABLE history_old")
log.info(u"\nReducing database size (this can take a while)")
yield self.dbpool.runQuery("VACUUM")
log.info(u"Database update done :)")
@defer.inlineCallbacks