[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
This commit is contained in:
Evan Morikawa 2017-03-31 14:45:14 -07:00
parent 328ea428be
commit e6b399ea27
2 changed files with 4 additions and 35 deletions

View file

@ -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 = [];

View file

@ -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});