-
-
Notifications
You must be signed in to change notification settings - Fork 36.4k
sqlite: expose prepared statement statistics #64541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -170,6 +170,16 @@ static constexpr const LimitInfo* GetLimitInfoFromName(std::string_view name) { | |
| return nullptr; | ||
| } | ||
|
|
||
| static constexpr const StatusInfo* GetStatusInfoFromName( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: This is structurally identical to template <typename T, size_t N>
static constexpr const T* FindByJsName(const std::array<T, N>& mapping,
std::string_view name) {
for (const auto& info : mapping) {
if (name == info.js_name) {
return &info;
}
}
return nullptr;
} |
||
| std::string_view name) { | ||
| for (const auto& info : kStatusMapping) { | ||
| if (name == info.js_name) { | ||
| return &info; | ||
| } | ||
| } | ||
| return nullptr; | ||
| } | ||
|
|
||
| inline MaybeLocal<Object> CreateSQLiteError(Isolate* isolate, | ||
| const char* message) { | ||
| Local<String> js_msg; | ||
|
|
@@ -3202,6 +3212,34 @@ void StatementSync::ExpandedSQLGetter(const FunctionCallbackInfo<Value>& args) { | |
| args.GetReturnValue().Set(result); | ||
| } | ||
|
|
||
| void StatementSync::Stat(const FunctionCallbackInfo<Value>& args) { | ||
| StatementSync* stmt; | ||
| ASSIGN_OR_RETURN_UNWRAP(&stmt, args.This()); | ||
| Environment* env = Environment::GetCurrent(args); | ||
| THROW_AND_RETURN_ON_BAD_STATE( | ||
| env, stmt->IsFinalized(), "statement has been finalized"); | ||
| Isolate* isolate = env->isolate(); | ||
|
|
||
| if (!args[0]->IsString()) { | ||
| THROW_ERR_INVALID_ARG_TYPE(isolate, | ||
| "The \"counter\" argument must be a string."); | ||
| return; | ||
| } | ||
|
|
||
| Utf8Value counter(isolate, args[0].As<String>()); | ||
| const StatusInfo* status_info = GetStatusInfoFromName(counter.ToStringView()); | ||
| if (status_info == nullptr) { | ||
| THROW_ERR_INVALID_ARG_VALUE( | ||
| isolate, "The \"counter\" argument is not a valid statistic name."); | ||
| return; | ||
| } | ||
|
|
||
| // The reset flag is always false; the counter is read without being cleared. | ||
| int value = sqlite3_stmt_status( | ||
| stmt->statement_, status_info->sqlite_status_id, false); | ||
| args.GetReturnValue().Set(Integer::New(isolate, value)); | ||
| } | ||
|
|
||
| void StatementSync::SetAllowBareNamedParameters( | ||
| const FunctionCallbackInfo<Value>& args) { | ||
| StatementSync* stmt; | ||
|
|
@@ -3617,6 +3655,7 @@ Local<FunctionTemplate> StatementSync::GetConstructorTemplate( | |
| tmpl, | ||
| FIXED_ONE_BYTE_STRING(isolate, "expandedSQL"), | ||
| StatementSync::ExpandedSQLGetter); | ||
| SetProtoMethodNoSideEffect(isolate, tmpl, "stat", StatementSync::Stat); | ||
| SetProtoMethod(isolate, | ||
| tmpl, | ||
| "setAllowBareNamedParameters", | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -52,6 +52,24 @@ static_assert( | |||||
| CheckLimitIndices(), | ||||||
| "Each kLimitMapping entry's sqlite_limit_id must match its index"); | ||||||
|
|
||||||
| // Mapping from JavaScript counter names to SQLite statement status constants | ||||||
| struct StatusInfo { | ||||||
| std::string_view js_name; | ||||||
| int sqlite_status_id; | ||||||
| }; | ||||||
|
|
||||||
| inline constexpr std::array<StatusInfo, 9> kStatusMapping = {{ | ||||||
| {"fullscanStep", SQLITE_STMTSTATUS_FULLSCAN_STEP}, | ||||||
| {"sort", SQLITE_STMTSTATUS_SORT}, | ||||||
| {"autoindex", SQLITE_STMTSTATUS_AUTOINDEX}, | ||||||
| {"vmStep", SQLITE_STMTSTATUS_VM_STEP}, | ||||||
| {"reprepare", SQLITE_STMTSTATUS_REPREPARE}, | ||||||
| {"run", SQLITE_STMTSTATUS_RUN}, | ||||||
| {"filterMiss", SQLITE_STMTSTATUS_FILTER_MISS}, | ||||||
| {"filterHit", SQLITE_STMTSTATUS_FILTER_HIT}, | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
3.37.2: https://github.com/sqlite/sqlite/blob/version-3.37.2/src/sqlite.h.in
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So maybe we need to enforce a minimum SQLite version on |
||||||
| {"memused", SQLITE_STMTSTATUS_MEMUSED}, | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since SQLite does not use an underscore here, I think
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yeah, that was the pattern followed |
||||||
| }}; | ||||||
|
|
||||||
| class DatabaseOpenConfiguration { | ||||||
| public: | ||||||
| explicit DatabaseOpenConfiguration(std::string&& location) | ||||||
|
|
@@ -272,6 +290,7 @@ class StatementSync : public BaseObject { | |||||
| static void SourceSQLGetter(const v8::FunctionCallbackInfo<v8::Value>& args); | ||||||
| static void ExpandedSQLGetter( | ||||||
| const v8::FunctionCallbackInfo<v8::Value>& args); | ||||||
| static void Stat(const v8::FunctionCallbackInfo<v8::Value>& args); | ||||||
| static void SetAllowBareNamedParameters( | ||||||
| const v8::FunctionCallbackInfo<v8::Value>& args); | ||||||
| static void SetAllowUnknownNamedParameters( | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -429,6 +429,98 @@ suite('StatementSync.prototype.expandedSQL', () => { | |||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| suite('StatementSync.prototype.stat()', () => { | ||||||
| const counters = [ | ||||||
| 'fullscanStep', 'sort', 'autoindex', 'vmStep', 'reprepare', | ||||||
| 'run', 'filterMiss', 'filterHit', 'memused', | ||||||
| ]; | ||||||
|
|
||||||
| test('returns a number for every valid counter', (t) => { | ||||||
| using db = new DatabaseSync(nextDb()); | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| db.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, val TEXT) STRICT;'); | ||||||
| const stmt = db.prepare('SELECT * FROM data'); | ||||||
| for (const counter of counters) { | ||||||
| t.assert.strictEqual(typeof stmt.stat(counter), 'number'); | ||||||
| } | ||||||
| }); | ||||||
|
|
||||||
| test('counts virtual machine steps and runs after execution', (t) => { | ||||||
| using db = new DatabaseSync(nextDb()); | ||||||
| db.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, val TEXT) STRICT;'); | ||||||
| const insert = db.prepare('INSERT INTO data (key, val) VALUES (?, ?)'); | ||||||
| for (let i = 1; i <= 5; i++) { | ||||||
| insert.run(i, `val-${i}`); | ||||||
| } | ||||||
| const stmt = db.prepare('SELECT * FROM data'); | ||||||
| t.assert.strictEqual(stmt.stat('run'), 0); | ||||||
| t.assert.strictEqual(stmt.stat('vmStep'), 0); | ||||||
| stmt.all(); | ||||||
| t.assert.strictEqual(stmt.stat('run'), 1); | ||||||
| t.assert.ok(stmt.stat('vmStep') > 0); | ||||||
| t.assert.ok(stmt.stat('memused') > 0); | ||||||
| }); | ||||||
|
|
||||||
| test('detects full table scans', (t) => { | ||||||
| using db = new DatabaseSync(nextDb()); | ||||||
| db.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, val TEXT) STRICT;'); | ||||||
| const insert = db.prepare('INSERT INTO data (key, val) VALUES (?, ?)'); | ||||||
| for (let i = 1; i <= 10; i++) { | ||||||
| insert.run(i, `val-${i}`); | ||||||
| } | ||||||
|
|
||||||
| // Filtering on a non-indexed column forces a full table scan. | ||||||
| const scan = db.prepare('SELECT * FROM data WHERE val = ?'); | ||||||
| scan.all('val-5'); | ||||||
| t.assert.ok(scan.stat('fullscanStep') > 0); | ||||||
|
|
||||||
| // Filtering on the primary key uses the index; no full scan occurs. | ||||||
| const indexed = db.prepare('SELECT * FROM data WHERE key = ?'); | ||||||
| indexed.all(5); | ||||||
| t.assert.strictEqual(indexed.stat('fullscanStep'), 0); | ||||||
| }); | ||||||
|
|
||||||
| test('reading a counter does not reset it', (t) => { | ||||||
| using db = new DatabaseSync(nextDb()); | ||||||
| db.exec('CREATE TABLE data(key INTEGER PRIMARY KEY, val TEXT) STRICT;'); | ||||||
| const stmt = db.prepare('SELECT * FROM data'); | ||||||
| stmt.all(); | ||||||
| const first = stmt.stat('run'); | ||||||
| t.assert.strictEqual(stmt.stat('run'), first); | ||||||
| }); | ||||||
|
|
||||||
| test('throws if the counter argument is not a string', (t) => { | ||||||
| using db = new DatabaseSync(nextDb()); | ||||||
| const stmt = db.prepare('SELECT 1'); | ||||||
| t.assert.throws(() => stmt.stat(), { | ||||||
| code: 'ERR_INVALID_ARG_TYPE', | ||||||
| message: /The "counter" argument must be a string/, | ||||||
| }); | ||||||
| t.assert.throws(() => stmt.stat(42), { | ||||||
| code: 'ERR_INVALID_ARG_TYPE', | ||||||
| message: /The "counter" argument must be a string/, | ||||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| test('throws if the counter name is unknown', (t) => { | ||||||
| using db = new DatabaseSync(nextDb()); | ||||||
| const stmt = db.prepare('SELECT 1'); | ||||||
| t.assert.throws(() => stmt.stat('nope'), { | ||||||
| code: 'ERR_INVALID_ARG_VALUE', | ||||||
| message: /The "counter" argument is not a valid statistic name/, | ||||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| test('throws if the statement is finalized', (t) => { | ||||||
| const db = new DatabaseSync(nextDb()); | ||||||
| const stmt = db.prepare('SELECT 1'); | ||||||
| db.close(); | ||||||
| t.assert.throws(() => stmt.stat('run'), { | ||||||
| code: 'ERR_INVALID_STATE', | ||||||
| message: /statement has been finalized/, | ||||||
| }); | ||||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| suite('StatementSync.prototype.setReadBigInts()', () => { | ||||||
| test('BigInts support can be toggled', (t) => { | ||||||
| const db = new DatabaseSync(nextDb()); | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another method to reset stats would be nice to have