add partial index on odid, and search odid as well

the odid != 0 check in cards_for_deck.sql is necessary for sqlite
to be able to take advantage of the index
This commit is contained in:
Damien Elmes 2020-05-02 13:30:13 +10:00
parent 0bb80329e8
commit 3ffb37270d
5 changed files with 14 additions and 3 deletions

View file

@ -366,8 +366,6 @@ impl Collection {
}
fn delete_all_cards_in_normal_deck(&mut self, did: DeckID) -> Result<()> {
// fixme: need to search on odid as well
// fixme: the odid requirement will require a table scan, which will be slow when deleting a large tree
let cids = self.storage.all_cards_in_single_deck(did)?;
self.remove_cards_inner(&cids)
}

View file

@ -0,0 +1,9 @@
select
id
from cards
where
did = ?1
or (
odid != 0
and odid = ?1
)

View file

@ -117,7 +117,7 @@ impl SqliteStorage {
pub(crate) fn all_cards_in_single_deck(&self, did: DeckID) -> Result<Vec<CardID>> {
self.db
.prepare_cached("select id from cards where did = ?")?
.prepare_cached(include_str!("cards_for_deck.sql"))?
.query_and_then(&[did], |r| r.get(0).map_err(Into::into))?
.collect()
}

View file

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

View file

@ -35,6 +35,9 @@ create table decks (
kind bytes not null
);
create unique index idx_decks_id on decks (id);
create index idx_cards_odid on cards (odid)
where
odid != 0;
update col
set
ver = 15;