From e6b399ea273094e2822739bb56bcd934aaf1fb00 Mon Sep 17 00:00:00 2001 From: Evan Morikawa Date: Fri, 31 Mar 2017 14:45:14 -0700 Subject: [PATCH] [client-app] remove the `ANALYZE` in favor of `pragma.optimize` Summary: Removes some VERY long running `ANLAYZE` queries. Was taking up to 50 seconds on my 9GB database on every boot https://sqlite.org/pragma.html#pragma_optimize Test Plan: I tested to make sure the app still quits quickly. It does. The SQLite docs also say this should be fast. Reviewers: halla, spang, mark, juan Reviewed By: juan Differential Revision: https://phab.nylas.com/D4314 --- .../stores/database-setup-query-builder.es6 | 17 -------------- .../src/flux/stores/database-store.es6 | 22 ++++--------------- 2 files changed, 4 insertions(+), 35 deletions(-) diff --git a/packages/client-app/src/flux/stores/database-setup-query-builder.es6 b/packages/client-app/src/flux/stores/database-setup-query-builder.es6 index 1bd0ad17a..210b18a56 100644 --- a/packages/client-app/src/flux/stores/database-setup-query-builder.es6 +++ b/packages/client-app/src/flux/stores/database-setup-query-builder.es6 @@ -19,23 +19,6 @@ export default class DatabaseSetupQueryBuilder { return queries; } - analyzeQueries() { - const queries = []; - - for (const klass of DatabaseObjectRegistry.getAllConstructors()) { - const attributes = Object.keys(klass.attributes).map(k => klass.attributes[k]); - const collectionAttributes = attributes.filter((attr) => - attr.queryable && attr instanceof AttributeCollection - ) - - queries.push(`ANALYZE \`${klass.name}\``); - collectionAttributes.forEach((attribute) => { - queries.push(`ANALYZE \`${tableNameForJoin(klass, attribute.itemClass)}\``) - }); - } - return queries; - } - setupQueriesForTable(klass) { const attributes = Object.keys(klass.attributes).map(k => klass.attributes[k]); let queries = []; diff --git a/packages/client-app/src/flux/stores/database-store.es6 b/packages/client-app/src/flux/stores/database-store.es6 index c2965af7a..a7a25188c 100644 --- a/packages/client-app/src/flux/stores/database-store.es6 +++ b/packages/client-app/src/flux/stores/database-store.es6 @@ -136,7 +136,6 @@ class DatabaseStore extends NylasStore { this._checkDatabaseVersion({allowUnset: true}, () => { this._runDatabaseSetup(() => { app.setDatabasePhase(DatabasePhase.Ready); - setTimeout(() => this._runDatabaseAnalyze(), 60 * 1000); }); }); } else if (phase === DatabasePhase.Ready) { @@ -152,6 +151,10 @@ class DatabaseStore extends NylasStore { } else if (phase === DatabasePhase.Close) { this._open = false; if (this._db) { + // https://sqlite.org/pragma.html#pragma_optimize + // We do this instead of holding up initial booting by running + // potentially very expensive `ANALYZE` queries. + this._db.pragma('optimize'); this._db.close(); this._db = null; } @@ -223,23 +226,6 @@ class DatabaseStore extends NylasStore { return ready(); } - _runDatabaseAnalyze() { - const builder = new DatabaseSetupQueryBuilder(); - const queries = builder.analyzeQueries(); - const start = Date.now() - const step = () => { - const query = queries.shift(); - if (query) { - debug(`DatabaseStore: ${query}`); - this._executeInBackground(query, []).then(step); - } else { - const msec = Date.now() - start - console.log(`Completed ANALYZE of database - took ${msec}msec`); - } - } - step(); - } - _handleSetupError(err = (new Error(`Manually called _handleSetupError`))) { NylasEnv.reportError(err, {}, {noWindows: true});