From 70c2568af86a302c592b3f1f995c97b57bd8a755 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Wed, 20 May 2020 18:06:22 -0700 Subject: [PATCH] Add index to speed up slow query --- database/migration.go | 2 +- database/sql.go | 3 +++ database/sql/schema_version_29.sql | 1 + storage/enclosure.go | 2 +- 4 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 database/sql/schema_version_29.sql diff --git a/database/migration.go b/database/migration.go index ea0f1f85..8fd5f93d 100644 --- a/database/migration.go +++ b/database/migration.go @@ -12,7 +12,7 @@ import ( "miniflux.app/logger" ) -const schemaVersion = 28 +const schemaVersion = 29 // Migrate executes database migrations. func Migrate(db *sql.DB) { diff --git a/database/sql.go b/database/sql.go index cd3f418c..009357e9 100644 --- a/database/sql.go +++ b/database/sql.go @@ -170,6 +170,8 @@ alter table entries alter column changed_at set not null; `, "schema_version_28": `alter table entries add column share_code text not null default ''; create unique index entries_share_code_idx on entries using btree(share_code) where share_code <> ''; +`, + "schema_version_29": `create index enclosures_user_entry_url_idx on enclosures(user_id, entry_id, md5(url)); `, "schema_version_3": `create table tokens ( id text not null, @@ -227,6 +229,7 @@ var SqlMapChecksums = map[string]string{ "schema_version_26": "64f14add40691f18f514ac0eed10cd9b19c83a35e5c3d8e0bce667e0ceca9094", "schema_version_27": "4235396b37fd7f52ff6f7526416042bb1649701233e2d99f0bcd583834a0a967", "schema_version_28": "a64b5ba0b37fe3f209617b7d0e4dd05018d2b8362d2c9c528ba8cce19b77e326", + "schema_version_29": "527403d951d025b387baf7b1ab80c014752c5429cc0b9851aeb34b7716cf2c68", "schema_version_3": "a54745dbc1c51c000f74d4e5068f1e2f43e83309f023415b1749a47d5c1e0f12", "schema_version_4": "216ea3a7d3e1704e40c797b5dc47456517c27dbb6ca98bf88812f4f63d74b5d9", "schema_version_5": "46397e2f5f2c82116786127e9f6a403e975b14d2ca7b652a48cd1ba843e6a27c", diff --git a/database/sql/schema_version_29.sql b/database/sql/schema_version_29.sql new file mode 100644 index 00000000..5d7ee8b6 --- /dev/null +++ b/database/sql/schema_version_29.sql @@ -0,0 +1 @@ +create index enclosures_user_entry_url_idx on enclosures(user_id, entry_id, md5(url)); diff --git a/storage/enclosure.go b/storage/enclosure.go index 785e2dd2..4596696c 100644 --- a/storage/enclosure.go +++ b/storage/enclosure.go @@ -88,7 +88,7 @@ func (s *Storage) CreateEnclosure(enclosure *model.Enclosure) error { // IsEnclosureExists checks if an attachment exists. func (s *Storage) IsEnclosureExists(enclosure *model.Enclosure) bool { var result int - query := `SELECT count(*) as c FROM enclosures WHERE user_id=$1 AND entry_id=$2 AND url=$3` + query := `SELECT 1 FROM enclosures WHERE user_id=$1 AND entry_id=$2 AND url=$3` s.db.QueryRow(query, enclosure.UserID, enclosure.EntryID, enclosure.URL).Scan(&result) return result >= 1 }