numerous optimizations - cold cache start is much faster now

- fix priorityDue index order
- force correct index on checkDue() and spacedCardCount()
- don't check due again if reviewEarly & newEarly false
- optimize reviewEarly/buried unsuspend
This commit is contained in:
Damien Elmes 2009-06-06 18:49:30 +09:00
parent 06780e56fd
commit bd31fb4e69

View file

@ -604,14 +604,20 @@ and combinedDue <= :t""", t=time.time())
"select count(*) from cards where " "select count(*) from cards where "
"type = 2 and priority in (1,2,3,4) and isDue = 1") "type = 2 and priority in (1,2,3,4) and isDue = 1")
def forceIndex(self, index):
ver = sqlite.sqlite_version.split(".")
if int(ver[1]) >= 6 and int(ver[2]) >= 4:
# only supported in 3.6.4+
return " indexed by " + index + " "
return ""
def checkDue(self): def checkDue(self):
"Mark expired cards due, and update counts." "Mark expired cards due, and update counts."
self.checkDailyStats() self.checkDailyStats()
# mark due & update counts # mark due & update counts
stmt = """ stmt = ("update cards " + self.forceIndex("ix_cards_priorityDue") +
update cards set "set isDue = 1 where type = %d and isDue = 0 and " +
isDue = 1 where type = %d and isDue = 0 and "priority in (1,2,3,4) and combinedDue <= :now")
priority in (1,2,3,4) and combinedDue <= :now"""
# failed cards # failed cards
self.failedSoonCount += self.s.statement( self.failedSoonCount += self.s.statement(
stmt % 0, now=time.time()+self.delay0).rowcount stmt % 0, now=time.time()+self.delay0).rowcount
@ -630,7 +636,6 @@ type = 0 and isDue = 1 and combinedDue <= :now""", now=time.time())
def rebuildQueue(self): def rebuildQueue(self):
"Update relative delays based on current time." "Update relative delays based on current time."
t = time.time()
# setup global/daily stats # setup global/daily stats
self._globalStats = globalStats(self) self._globalStats = globalStats(self)
self._dailyStats = dailyStats(self) self._dailyStats = dailyStats(self)
@ -655,7 +660,6 @@ type = 0 and isDue = 1 and combinedDue <= :now""", now=time.time())
or Deck.initialFactor) or Deck.initialFactor)
# recache css # recache css
self.rebuildCSS() self.rebuildCSS()
#print "rebuild queue", time.time() - t
def checkDailyStats(self): def checkDailyStats(self):
# check if the day has rolled over # check if the day has rolled over
@ -667,6 +671,7 @@ type = 0 and isDue = 1 and combinedDue <= :now""", now=time.time())
if ids: if ids:
self.updatePriorities(ids) self.updatePriorities(ids)
self.flushMod() self.flushMod()
if self.reviewEarly or self.newEarly:
self.reviewEarly = False self.reviewEarly = False
self.newEarly = False self.newEarly = False
self.checkDue() self.checkDue()
@ -856,9 +861,9 @@ select count(id) from cards where type in (0,1,2) and priority = 0""")
def spacedCardCount(self): def spacedCardCount(self):
return self.s.scalar(""" return self.s.scalar("""
select count(cards.id) from cards where select count(cards.id) from cards %s where
type in (1,2) and isDue = 0 and priority in (1,2,3,4) and combinedDue > :now type = 2 and isDue = 0 and priority in (1,2,3,4) and combinedDue > :now
and due < :now""", now=time.time()) and due < :now""" % self.forceIndex("ix_cards_priorityDue"), now=time.time())
def isEmpty(self): def isEmpty(self):
return not self.cardCount return not self.cardCount
@ -1225,7 +1230,6 @@ answerAlign from cardModels""")])
(hexifyID(row[0]), row[1]) for row in self.s.all(""" (hexifyID(row[0]), row[1]) for row in self.s.all("""
select id, lastFontColour from cardModels""")]) select id, lastFontColour from cardModels""")])
self.css = css self.css = css
#print css
return css return css
def copyModel(self, oldModel): def copyModel(self, oldModel):
@ -2466,7 +2470,6 @@ seq > :s and seq <= :e order by seq desc""", s=start, e=end)
for c, s in enumerate(sql): for c, s in enumerate(sql):
if mod and not c % mod: if mod and not c % mod:
self.updateProgress() self.updateProgress()
#print "--", s.encode("utf-8")[0:30]
self.engine.execute(s) self.engine.execute(s)
newend = self._latestUndoRow() newend = self._latestUndoRow()
dst.append([u[0], newstart, newend]) dst.append([u[0], newstart, newend])
@ -2669,7 +2672,9 @@ class DeckStorage(object):
# update counts # update counts
deck.rebuildQueue() deck.rebuildQueue()
# unsuspend reviewed early & buried # unsuspend reviewed early & buried
ids = deck.s.column0("select id from cards where priority in (-1, -2)") ids = deck.s.column0(
"select id from cards where type in (0,1,2) and isDue = 0 and "
"priority in (-1, -2)")
if ids: if ids:
deck.updatePriorities(ids) deck.updatePriorities(ids)
deck.checkDue() deck.checkDue()
@ -2711,7 +2716,7 @@ create index if not exists ix_cards_duePriority on cards
# check due # check due
deck.s.statement(""" deck.s.statement("""
create index if not exists ix_cards_priorityDue on cards create index if not exists ix_cards_priorityDue on cards
(type, isDue, combinedDue, priority)""") (type, isDue, priority, combinedDue)""")
# average factor # average factor
deck.s.statement(""" deck.s.statement("""
create index if not exists ix_cards_factor on cards create index if not exists ix_cards_factor on cards
@ -3134,6 +3139,7 @@ nextFactor, reps, thinkingTime, yesCount, noCount from reviewHistory""")
deck.version = 34 deck.version = 34
deck.s.commit() deck.s.commit()
if deck.version < 36: if deck.version < 36:
deck.s.statement("drop index if exists ix_cards_priorityDue")
DeckStorage._addIndices(deck) DeckStorage._addIndices(deck)
deck.s.execute("analyze") deck.s.execute("analyze")
deck.version = 36 deck.version = 36