From 932ed44a9fda044a9e9259122fc73cd3278cceae Mon Sep 17 00:00:00 2001 From: Glen Sawyer Date: Fri, 16 Dec 2022 13:14:49 -0700 Subject: [PATCH] [plugin] Statistics: do not increment db sequences unnecessarily during cloud sync (#9921) When you do an insert using either ON CONFLICT [...] DO NOTHING or INSERT OR IGNORE to prevent duplicate rows, sqlite still increments the sequence counter for each of the duplicate rows that it did not insert. The only way around that is to explicitly write the SQL statement so that it doesn't try to insert the duplicates in the first place. --- plugins/statistics.koplugin/main.lua | 5 ++++- plugins/vocabbuilder.koplugin/db.lua | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/plugins/statistics.koplugin/main.lua b/plugins/statistics.koplugin/main.lua index f865878f4..aca136499 100644 --- a/plugins/statistics.koplugin/main.lua +++ b/plugins/statistics.koplugin/main.lua @@ -2966,7 +2966,10 @@ function ReaderStatistics.onSync(local_path, cached_path, income_path) title, authors, notes, last_open, highlights, pages, series, language, md5, total_read_time, total_read_pages ) SELECT title, authors, notes, last_open, highlights, pages, series, language, md5, total_read_time, total_read_pages - FROM income_db.book WHERE true ON CONFLICT (title, authors, md5) DO NOTHING; + FROM income_db.book + WHERE (title, authors, md5) NOT IN ( + SELECT title, authors, md5 FROM book + ); -- We create a book_id mapping temp table (view not possible due to attached db) CREATE TEMP TABLE book_id_map AS diff --git a/plugins/vocabbuilder.koplugin/db.lua b/plugins/vocabbuilder.koplugin/db.lua index 0a7160996..070117329 100644 --- a/plugins/vocabbuilder.koplugin/db.lua +++ b/plugins/vocabbuilder.koplugin/db.lua @@ -436,7 +436,7 @@ function VocabularyBuilder.onSync(local_path, cached_path, income_path) sql = sql .. [[ -- We merge the local db with income db to form the synced db. -- First we do the books - INSERT OR IGNORE INTO title (name) SELECT name FROM income_db.title; + INSERT INTO title (name) SELECT name FROM income_db.title WHERE name NOT IN (SELECT name FROM title); -- Then update income db's book title id references UPDATE income_db.vocabulary SET title_id = ifnull(