From 70bb6022467e407d0a3e951141802a0550cb897c Mon Sep 17 00:00:00 2001 From: Mark Hahnenberg Date: Fri, 31 Mar 2017 14:44:52 -0700 Subject: [PATCH] [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 --- .../src/local-api/routes/contacts.js | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/packages/client-sync/src/local-api/routes/contacts.js b/packages/client-sync/src/local-api/routes/contacts.js index ebcf8bf00..45c861ceb 100644 --- a/packages/client-sync/src/local-api/routes/contacts.js +++ b/packages/client-sync/src/local-api/routes/contacts.js @@ -107,16 +107,20 @@ module.exports = (server) => { const useLabels = account.provider === 'gmail'; - while (true) { - const include = [{ - model: (useLabels ? Label : Folder), - attributes: ['id', 'role'], - where: {role: 'sent'}}, - ]; + const model = (useLabels ? Label : Folder); + const category = await model.findOne({ + attributes: ['id', 'role'], + 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},