add an index on notes(mid) so we can avoid the tablescan

This commit is contained in:
Damien Elmes 2020-05-04 21:29:58 +10:00
parent f6931197d2
commit 2308b136fd
5 changed files with 10 additions and 6 deletions

View File

@ -317,7 +317,6 @@ class ModelManager:
def useCount(self, m: NoteType) -> Any: def useCount(self, m: NoteType) -> Any:
"Number of note using M." "Number of note using M."
print("useCount() is slow; prefer all_use_counts()")
return self.col.db.scalar("select count() from notes where mid = ?", m["id"]) return self.col.db.scalar("select count() from notes where mid = ?", m["id"])
# Copying # Copying

View File

@ -656,7 +656,7 @@ class RustBackend:
json=bjson, preserve_usn_and_mtime=preserve_usn json=bjson, preserve_usn_and_mtime=preserve_usn
) )
), ),
release_gil=True release_gil=True,
).add_or_update_notetype ).add_or_update_notetype
nt["id"] = id nt["id"] = id

View File

@ -1,10 +1,13 @@
select select
nt.id, nt.id,
nt.name, nt.name,
count(n.id) (
select
count(*)
from notes n
where
nt.id = n.mid
)
from notetypes nt from notetypes nt
left join notes n on (n.mid = nt.id)
group by
nt.id
order by order by
nt.name nt.name

View File

@ -6,6 +6,7 @@ drop table templates;
drop table notetypes; drop table notetypes;
drop table decks; drop table decks;
drop index idx_cards_odid; drop index idx_cards_odid;
drop index idx_notes_mid;
update col update col
set set
ver = 11; ver = 11;

View File

@ -35,6 +35,7 @@ create table decks (
kind bytes not null kind bytes not null
); );
create unique index idx_decks_name on decks (name); create unique index idx_decks_name on decks (name);
create index idx_notes_mid on notes (mid);
create index idx_cards_odid on cards (odid) create index idx_cards_odid on cards (odid)
where where
odid != 0; odid != 0;