[client-sync] Split Contact ranking query

Summary:
We were using a join for the contact ranking query and for whatever
reason on large databases this was extremely slow in SQLite. This diff
splits the query into first finding the sent Folder/Label and then
searching for non-draft Messages in that category.

Test Plan: Run locally, verify the query is faster

Reviewers: juan, evan, spang

Reviewed By: spang

Differential Revision: https://phab.nylas.com/D4315
This commit is contained in:
Mark Hahnenberg 2017-03-31 14:44:52 -07:00
parent c4763d4bd5
commit 70bb602246

View file

@ -107,16 +107,20 @@ module.exports = (server) => {
const useLabels = account.provider === 'gmail';
while (true) {
const include = [{
model: (useLabels ? Label : Folder),
const model = (useLabels ? Label : Folder);
const category = await model.findOne({
attributes: ['id', 'role'],
where: {role: 'sent'}},
];
where: {role: 'sent'},
});
const messages = await Message.findAll({
if (!category) {
reply('[]');
return;
}
while (true) {
const messages = await category.getMessages({
attributes: ['rowid', 'id', 'to', 'cc', 'bcc', 'date'],
include,
where: {
'isDraft': false, // Don't include unsent things.
'$message.rowid$': {$gt: lastID},