Add option to silence query plan debug output for individual queries

Summary:
Sometimes you have to perform a query that needs to do a table scan. For those
times you don't want to silence entire classes of queries (e.g. by table name),
you can now silence individual queries.

Test Plan: Run locally, verify that noisy stuff disappears from logs

Reviewers: evan, juan

Reviewed By: juan

Differential Revision: https://phab.nylas.com/D3763
This commit is contained in:
Mark Hahnenberg 2017-01-23 17:57:18 -08:00
parent 313bd18913
commit afb49b8b2f
3 changed files with 12 additions and 3 deletions

View file

@ -29,6 +29,7 @@ export default class SearchIndexer {
.order(modelClass.naturalSortOrder()) .order(modelClass.naturalSortOrder())
.offset(indexSize) .offset(indexSize)
.limit(1) .limit(1)
.silenceQueryPlanDebugOutput()
// console.info('SearchIndexer: _getIndexCutoff query', query.sql()); // console.info('SearchIndexer: _getIndexCutoff query', query.sql());
const models = await query; const models = await query;
return models[0]; return models[0];

View file

@ -56,6 +56,7 @@ export default class ModelQuery {
this._returnIds = false; this._returnIds = false;
this._includeJoinedData = []; this._includeJoinedData = [];
this._count = false; this._count = false;
this._logQueryPlanDebugOutput = true;
} }
clone() { clone() {
@ -90,6 +91,11 @@ export default class ModelQuery {
return this; return this;
} }
silenceQueryPlanDebugOutput() {
this._logQueryPlanDebugOutput = false;
return this;
}
// Public: Add one or more where clauses to the query // Public: Add one or more where clauses to the query
// //
// - `matchers` An {Array} of {Matcher} objects that add where clauses to the underlying query. // - `matchers` An {Array} of {Matcher} objects that add where clauses to the underlying query.

View file

@ -296,7 +296,7 @@ class DatabaseStore extends NylasStore {
// //
// If a query is made before the database has been opened, the query will be // If a query is made before the database has been opened, the query will be
// held in a queue and run / resolved when the database is ready. // held in a queue and run / resolved when the database is ready.
_query(query, values = [], background = false) { _query(query, values = [], background = false, logQueryPlanDebugOutput = true) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (!this._open) { if (!this._open) {
this._waiting.push(() => this._query(query, values).then(resolve, reject)); this._waiting.push(() => this._query(query, values).then(resolve, reject));
@ -320,7 +320,9 @@ class DatabaseStore extends NylasStore {
const planString = `${plan.map(row => row.detail).join('\n')} for ${query}`; const planString = `${plan.map(row => row.detail).join('\n')} for ${query}`;
const quiet = ['ThreadCounts', 'ThreadSearch', 'ContactSearch', 'COVERING INDEX']; const quiet = ['ThreadCounts', 'ThreadSearch', 'ContactSearch', 'COVERING INDEX'];
if (planString.includes('SCAN') && !quiet.find(str => planString.includes(str))) { if (planString.includes('SCAN') &&
!quiet.find(str => planString.includes(str)) &&
logQueryPlanDebugOutput) {
console.log("Consider setting the .background() flag on this query to avoid blocking the event loop:") console.log("Consider setting the .background() flag on this query to avoid blocking the event loop:")
this._prettyConsoleLog(planString); this._prettyConsoleLog(planString);
} }
@ -574,7 +576,7 @@ class DatabaseStore extends NylasStore {
// - resolves with the result of the database query. // - resolves with the result of the database query.
// //
run(modelQuery, options = {format: true}) { run(modelQuery, options = {format: true}) {
return this._query(modelQuery.sql(), [], modelQuery._background).then((result) => { return this._query(modelQuery.sql(), [], modelQuery._background, modelQuery._logQueryPlanDebugOutput).then((result) => {
let transformed = modelQuery.inflateResult(result); let transformed = modelQuery.inflateResult(result);
if (options.format !== false) { if (options.format !== false) {
transformed = modelQuery.formatResult(transformed) transformed = modelQuery.formatResult(transformed)