mirror of
https://github.com/Foundry376/Mailspring.git
synced 2025-09-10 14:44:52 +08:00
perf(db): Use partial indexes added in sqlite 3.8
This commit is contained in:
parent
8803b01d2f
commit
658e1a5d0d
4 changed files with 42 additions and 11 deletions
|
@ -3,6 +3,7 @@ Rx = require 'rx-lite'
|
||||||
_ = require 'underscore'
|
_ = require 'underscore'
|
||||||
{Message,
|
{Message,
|
||||||
OutboxStore,
|
OutboxStore,
|
||||||
|
AccountStore,
|
||||||
MutableQueryResultSet,
|
MutableQueryResultSet,
|
||||||
MutableQuerySubscription,
|
MutableQuerySubscription,
|
||||||
ObservableListDataSource,
|
ObservableListDataSource,
|
||||||
|
@ -35,9 +36,14 @@ class DraftListStore extends NylasStore
|
||||||
query = DatabaseStore.findAll(Message)
|
query = DatabaseStore.findAll(Message)
|
||||||
.include(Message.attributes.body)
|
.include(Message.attributes.body)
|
||||||
.order(Message.attributes.date.descending())
|
.order(Message.attributes.date.descending())
|
||||||
.where(draft: true, accountId: mailboxPerspective.accountIds)
|
.where(draft: true)
|
||||||
.page(0, 1)
|
.page(0, 1)
|
||||||
|
|
||||||
|
# Adding a "account_id IN (a,b,c)" clause to our query can result in a full
|
||||||
|
# table scan. Don't add the where clause if we know we want results from all.
|
||||||
|
if mailboxPerspective.accountIds.length < AccountStore.accounts().length
|
||||||
|
query.where(accountId: mailboxPerspective.accountIds)
|
||||||
|
|
||||||
subscription = new MutableQuerySubscription(query, {emitResultSet: true})
|
subscription = new MutableQuerySubscription(query, {emitResultSet: true})
|
||||||
$resultSet = Rx.Observable.fromNamedQuerySubscription('draft-list', subscription)
|
$resultSet = Rx.Observable.fromNamedQuerySubscription('draft-list', subscription)
|
||||||
$resultSet = Rx.Observable.combineLatest [
|
$resultSet = Rx.Observable.combineLatest [
|
||||||
|
|
|
@ -157,10 +157,20 @@ class Message extends ModelWithMetadata
|
||||||
|
|
||||||
@additionalSQLiteConfig:
|
@additionalSQLiteConfig:
|
||||||
setup: ->
|
setup: ->
|
||||||
['CREATE INDEX IF NOT EXISTS MessageListThreadIndex ON Message(thread_id, date ASC)',
|
[
|
||||||
'CREATE INDEX IF NOT EXISTS MessageListDraftIndex ON Message(account_id, draft)',
|
# For thread view
|
||||||
|
'CREATE INDEX IF NOT EXISTS MessageListThreadIndex ON Message(thread_id, date ASC)',
|
||||||
|
|
||||||
|
# For draft lookups
|
||||||
'CREATE UNIQUE INDEX IF NOT EXISTS MessageDraftIndex ON Message(client_id)',
|
'CREATE UNIQUE INDEX IF NOT EXISTS MessageDraftIndex ON Message(client_id)',
|
||||||
'CREATE UNIQUE INDEX IF NOT EXISTS MessageBodyIndex ON MessageBody(id)']
|
|
||||||
|
# Partial indexes for draft
|
||||||
|
'CREATE INDEX IF NOT EXISTS MessageListDraftIndex ON Message(account_id, date DESC) WHERE draft = 1',
|
||||||
|
'CREATE INDEX IF NOT EXISTS MessageListUnifiedDraftIndex ON Message(date DESC) WHERE draft = 1',
|
||||||
|
|
||||||
|
# MessageBody lookups
|
||||||
|
'CREATE UNIQUE INDEX IF NOT EXISTS MessageBodyIndex ON MessageBody(id)'
|
||||||
|
]
|
||||||
|
|
||||||
constructor: ->
|
constructor: ->
|
||||||
super
|
super
|
||||||
|
|
|
@ -113,16 +113,27 @@ class Thread extends ModelWithMetadata {
|
||||||
|
|
||||||
static additionalSQLiteConfig = {
|
static additionalSQLiteConfig = {
|
||||||
setup: () => [
|
setup: () => [
|
||||||
|
// ThreadCounts
|
||||||
'CREATE TABLE IF NOT EXISTS `ThreadCounts` (`category_id` TEXT PRIMARY KEY, `unread` INTEGER, `total` INTEGER)',
|
'CREATE TABLE IF NOT EXISTS `ThreadCounts` (`category_id` TEXT PRIMARY KEY, `unread` INTEGER, `total` INTEGER)',
|
||||||
'CREATE UNIQUE INDEX IF NOT EXISTS ThreadCountsIndex ON `ThreadCounts` (category_id DESC)',
|
'CREATE UNIQUE INDEX IF NOT EXISTS ThreadCountsIndex ON `ThreadCounts` (category_id DESC)',
|
||||||
|
|
||||||
'CREATE INDEX IF NOT EXISTS ThreadContactDateIndex ON `ThreadContact`(last_message_received_timestamp DESC, value, id)',
|
// ThreadContact
|
||||||
|
'CREATE INDEX IF NOT EXISTS ThreadContactDateIndex ON `ThreadContact` (last_message_received_timestamp DESC, value, id)',
|
||||||
|
|
||||||
'CREATE INDEX IF NOT EXISTS ThreadDateIndex ON `Thread`(last_message_received_timestamp DESC, id)',
|
// ThreadCategory
|
||||||
'CREATE INDEX IF NOT EXISTS ThreadStarIndex ON Thread(account_id, starred)',
|
'CREATE INDEX IF NOT EXISTS ThreadListCategoryIndex ON `ThreadCategory` (last_message_received_timestamp DESC, value, in_all_mail, unread, id)',
|
||||||
|
'CREATE INDEX IF NOT EXISTS ThreadListCategorySentIndex ON `ThreadCategory` (last_message_sent_timestamp DESC, value, in_all_mail, unread, id)',
|
||||||
|
|
||||||
'CREATE INDEX IF NOT EXISTS ThreadListCategoryIndex ON `ThreadCategory`(last_message_received_timestamp DESC, value, in_all_mail, unread, id)',
|
// Thread: General index
|
||||||
'CREATE INDEX IF NOT EXISTS ThreadListCategorySentIndex ON `ThreadCategory`(last_message_sent_timestamp DESC, value, in_all_mail, unread, id)',
|
'CREATE INDEX IF NOT EXISTS ThreadDateIndex ON `Thread` (last_message_received_timestamp DESC)',
|
||||||
|
|
||||||
|
// Thread: Partial indexes for specific views
|
||||||
|
'CREATE INDEX IF NOT EXISTS ThreadUnreadIndex ON `Thread` (account_id, last_message_received_timestamp DESC) WHERE unread = 1 AND in_all_mail = 1',
|
||||||
|
'CREATE INDEX IF NOT EXISTS ThreadUnifiedUnreadIndex ON `Thread` (last_message_received_timestamp DESC) WHERE unread = 1 AND in_all_mail = 1',
|
||||||
|
|
||||||
|
'DROP INDEX IF EXISTS `Thread`.ThreadStarIndex',
|
||||||
|
'CREATE INDEX IF NOT EXISTS ThreadStarredIndex ON `Thread` (account_id, last_message_received_timestamp DESC) WHERE starred = 1 AND in_all_mail = 1',
|
||||||
|
'CREATE INDEX IF NOT EXISTS ThreadUnifiedStarredIndex ON `Thread` (last_message_received_timestamp DESC) WHERE starred = 1 AND in_all_mail = 1',
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -185,11 +185,15 @@ class StarredMailboxPerspective extends MailboxPerspective
|
||||||
|
|
||||||
threads: =>
|
threads: =>
|
||||||
query = DatabaseStore.findAll(Thread).where([
|
query = DatabaseStore.findAll(Thread).where([
|
||||||
Thread.attributes.accountId.in(@accountIds),
|
|
||||||
Thread.attributes.starred.equal(true),
|
Thread.attributes.starred.equal(true),
|
||||||
Thread.attributes.inAllMail.equal(true),
|
Thread.attributes.inAllMail.equal(true),
|
||||||
]).limit(0)
|
]).limit(0)
|
||||||
|
|
||||||
|
# Adding a "account_id IN (a,b,c)" clause to our query can result in a full
|
||||||
|
# table scan. Don't add the where clause if we know we want results from all.
|
||||||
|
if @accountIds.length < AccountStore.accounts().length
|
||||||
|
query.where(Thread.attributes.accountId.in(@accountIds))
|
||||||
|
|
||||||
return new MutableQuerySubscription(query, {emitResultSet: true})
|
return new MutableQuerySubscription(query, {emitResultSet: true})
|
||||||
|
|
||||||
canReceiveThreadsFromAccountIds: =>
|
canReceiveThreadsFromAccountIds: =>
|
||||||
|
|
Loading…
Add table
Reference in a new issue