1
0
Fork 0

Add index to speed up slow query

This commit is contained in:
Frédéric Guillot 2020-05-20 18:06:22 -07:00
parent 0683074b8b
commit 70c2568af8
4 changed files with 6 additions and 2 deletions

View file

@ -12,7 +12,7 @@ import (
"miniflux.app/logger" "miniflux.app/logger"
) )
const schemaVersion = 28 const schemaVersion = 29
// Migrate executes database migrations. // Migrate executes database migrations.
func Migrate(db *sql.DB) { func Migrate(db *sql.DB) {

View file

@ -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 ''; "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 <> ''; 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 ( "schema_version_3": `create table tokens (
id text not null, id text not null,
@ -227,6 +229,7 @@ var SqlMapChecksums = map[string]string{
"schema_version_26": "64f14add40691f18f514ac0eed10cd9b19c83a35e5c3d8e0bce667e0ceca9094", "schema_version_26": "64f14add40691f18f514ac0eed10cd9b19c83a35e5c3d8e0bce667e0ceca9094",
"schema_version_27": "4235396b37fd7f52ff6f7526416042bb1649701233e2d99f0bcd583834a0a967", "schema_version_27": "4235396b37fd7f52ff6f7526416042bb1649701233e2d99f0bcd583834a0a967",
"schema_version_28": "a64b5ba0b37fe3f209617b7d0e4dd05018d2b8362d2c9c528ba8cce19b77e326", "schema_version_28": "a64b5ba0b37fe3f209617b7d0e4dd05018d2b8362d2c9c528ba8cce19b77e326",
"schema_version_29": "527403d951d025b387baf7b1ab80c014752c5429cc0b9851aeb34b7716cf2c68",
"schema_version_3": "a54745dbc1c51c000f74d4e5068f1e2f43e83309f023415b1749a47d5c1e0f12", "schema_version_3": "a54745dbc1c51c000f74d4e5068f1e2f43e83309f023415b1749a47d5c1e0f12",
"schema_version_4": "216ea3a7d3e1704e40c797b5dc47456517c27dbb6ca98bf88812f4f63d74b5d9", "schema_version_4": "216ea3a7d3e1704e40c797b5dc47456517c27dbb6ca98bf88812f4f63d74b5d9",
"schema_version_5": "46397e2f5f2c82116786127e9f6a403e975b14d2ca7b652a48cd1ba843e6a27c", "schema_version_5": "46397e2f5f2c82116786127e9f6a403e975b14d2ca7b652a48cd1ba843e6a27c",

View file

@ -0,0 +1 @@
create index enclosures_user_entry_url_idx on enclosures(user_id, entry_id, md5(url));

View file

@ -88,7 +88,7 @@ func (s *Storage) CreateEnclosure(enclosure *model.Enclosure) error {
// IsEnclosureExists checks if an attachment exists. // IsEnclosureExists checks if an attachment exists.
func (s *Storage) IsEnclosureExists(enclosure *model.Enclosure) bool { func (s *Storage) IsEnclosureExists(enclosure *model.Enclosure) bool {
var result int 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) s.db.QueryRow(query, enclosure.UserID, enclosure.EntryID, enclosure.URL).Scan(&result)
return result >= 1 return result >= 1
} }