From 9540df84cf6fed4346a57911d347ced8186bd666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Mon, 1 Jun 2020 07:39:43 +0200 Subject: [PATCH] Documentation --- CHANGELOG.md | 5 +++++ README.md | 40 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0bc64ee15..17af488f6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -70,6 +70,11 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: ## Next Release +### Documentation Diff + +- Updated FAQ: [How do I print a request as SQL?](README.md#how-do-i-print-a-request-as-sql) +- New FAQ: [How do I monitor the duration of database statements execution?](README.md#how-do-i-monitor-the-duration-of-database-statements-execution) + ### Fixed - [#784](https://github.com/groue/GRDB.swift/pull/784): Fix observation of eager-loaded to-many associations diff --git a/README.md b/README.md index 165b41201..62fe4ec8f 100644 --- a/README.md +++ b/README.md @@ -7277,6 +7277,7 @@ FAQ - [How do I open a database stored as a resource of my application?](#how-do-i-open-a-database-stored-as-a-resource-of-my-application) - [How do I close a database connection?](#how-do-i-close-a-database-connection) - [How do I print a request as SQL?](#how-do-i-print-a-request-as-sql) +- [How do I monitor the duration of database statements execution?](#how-do-i-monitor-the-duration-of-database-statements-execution) - [Generic parameter 'T' could not be inferred](#generic-parameter-t-could-not-be-inferred) - [SQLite error 10 "disk I/O error", SQLite error 23 "not authorized"](#sqlite-error-10-disk-io-error-sqlite-error-23-not-authorized) - [SQLite error 21 "wrong number of statement arguments" with LIKE queries](#sqlite-error-21-wrong-number-of-statement-arguments-with-like-queries) @@ -7346,7 +7347,7 @@ try dbQueue.read { db in } ``` -Another option is to setup a tracing function that prints out all SQL requests executed by your application. You provide the trace function when you connect to the database: +Another option is to setup a tracing function that prints out the executed SQL requests. For example, provide a tracing function when you connect to the database: ```swift // Prints all SQL statements @@ -7362,9 +7363,46 @@ try dbQueue.read { db in } ``` +If you want to hide values such as `'O''Brien'` from the logged statements, adapt the tracing function as below: + +```swift +try db.trace { event in + if case let .statement(statement) = event { + // Prints SELECT * FROM player WHERE name = ? + print(statement.sql) + } +} +``` + > :point_up: **Note**: the generated SQL may change between GRDB releases, without notice: don't have your application rely on any specific SQL output. +### How do I monitor the duration of database statements execution? + +Use the `trace(options:_:)` method, with the `.profile` option: + +```swift +var config = Configuration() +config.prepareDatabase = { db in + try db.trace(options: .profile) { event in + // Prints all SQL statements with their duration + print(event) + + // Access to detailed profiling information + if case let .profile(statement, duration) = event, duration > 0.5 { + print("Slow query: \(statement.sql)") + } + } +} +let dbQueue = try DatabaseQueue(path: dbPath, configuration: config) + +try dbQueue.read { db in + let players = try Player.filter(Column("name") == "O'Brien").fetchAll(db) + // Prints "0.003s SELECT * FROM player WHERE name = 'O''Brien'" +} +``` + + ### Generic parameter 'T' could not be inferred You may get this error when using the `read` and `write` methods of database queues and pools: