[PR #2102] [MERGED] Add ability to enable DEV logs of Sqlite queries #3653

Closed
opened 2026-04-25 00:16:31 +02:00 by adam · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/advplyr/audiobookshelf/pull/2102
Author: @selfhost-alt
Created: 9/15/2023
Status: Merged
Merged: 9/22/2023
Merged by: @advplyr

Base: masterHead: sqlite-query-logging


📝 Commits (5)

  • b1524d2 Add ability to enable DEV logs of Sqlite queries
  • 71762ef Newline before printing query
  • b668c6e Remove stray quote
  • b64ecc7 Update server/Database.js
  • 1ab34fa Update server/Database.js

📊 Changes

1 file changed (+16 additions, -1 deletions)

View changed files

📝 server/Database.js (+16 -1)

📄 Description

I've been doing some debugging of Sqlite query performance, and I found it useful to enable logging of DB queries as they happen, so I figured I'd send a PR in case it's useful to others as well.

Logging all queries is pretty noisy, so rather than just logging at some log level, I've gated this logging behind a QUERY_LOGGING environment variable. Set it to QUERY_LOGGING=log to log just the query. Set it to QUERY_LOGGING=benchmark to log both the query and how long it took to run.

I'm happy to switch to some other mechanism for enabling this, if using an env var is undesirable.

An example benchmarking log message would look like this:

[2023-09-15 05:57:48] DEV: Ran the following query in 3ms:
 Executed (default): SELECT `podcast`.`id`, `podcast`.`title`, COUNT('*') AS `item_count`, `libraryItem`.`id` AS `libraryItem.id`, `libraryItem`.`ino` AS `libraryItem.ino`, `libraryItem`.`path` AS `libraryItem.path`, `libraryItem`.`relPath` AS `libraryItem.relPath`, `libraryItem`.`mediaId` AS `libraryItem.mediaId`, `libraryItem`.`mediaType` AS `libraryItem.mediaType`, `libraryItem`.`isFile` AS `libraryItem.isFile`, `libraryItem`.`isMissing` AS `libraryItem.isMissing`, `libraryItem`.`isInvalid` AS `libraryItem.isInvalid`, `libraryItem`.`mtime` AS `libraryItem.mtime`, `libraryItem`.`ctime` AS `libraryItem.ctime`, `libraryItem`.`birthtime` AS `libraryItem.birthtime`, `libraryItem`.`size` AS `libraryItem.size`, `libraryItem`.`lastScan` AS `libraryItem.lastScan`, `libraryItem`.`lastScanVersion` AS `libraryItem.lastScanVersion`, `libraryItem`.`libraryFiles` AS `libraryItem.libraryFiles`, `libraryItem`.`extraData` AS `libraryItem.extraData`, `libraryItem`.`createdAt` AS `libraryItem.createdAt`, `libraryItem`.`updatedAt` AS `libraryItem.updatedAt`, `libraryItem`.`libraryId` AS `libraryItem.libraryId`, `libraryItem`.`libraryFolderId` AS `libraryItem.libraryFolderId` FROM `podcasts` AS `podcast` LEFT OUTER JOIN `libraryItems` AS `libraryItem` ON `podcast`.`id` = `libraryItem`.`mediaId` AND `libraryItem`.`mediaType` = 'podcast' GROUP BY `podcast`.`id`, `podcast`.`title` HAVING `item_count` = 0;

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/advplyr/audiobookshelf/pull/2102 **Author:** [@selfhost-alt](https://github.com/selfhost-alt) **Created:** 9/15/2023 **Status:** ✅ Merged **Merged:** 9/22/2023 **Merged by:** [@advplyr](https://github.com/advplyr) **Base:** `master` ← **Head:** `sqlite-query-logging` --- ### 📝 Commits (5) - [`b1524d2`](https://github.com/advplyr/audiobookshelf/commit/b1524d245e193d4803215145c7fac1dad673a823) Add ability to enable DEV logs of Sqlite queries - [`71762ef`](https://github.com/advplyr/audiobookshelf/commit/71762ef8371c45b8cb9a61f09c12ea531743c632) Newline before printing query - [`b668c6e`](https://github.com/advplyr/audiobookshelf/commit/b668c6e37a2bf62784a7ba6ca17c15ab3511104a) Remove stray quote - [`b64ecc7`](https://github.com/advplyr/audiobookshelf/commit/b64ecc7c6fab4cfa6d6770433b8f9d744e71e64c) Update server/Database.js - [`1ab34fa`](https://github.com/advplyr/audiobookshelf/commit/1ab34fa77f0b6b93599c39129f4e893672981b7a) Update server/Database.js ### 📊 Changes **1 file changed** (+16 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `server/Database.js` (+16 -1) </details> ### 📄 Description I've been doing some debugging of Sqlite query performance, and I found it useful to enable logging of DB queries as they happen, so I figured I'd send a PR in case it's useful to others as well. Logging all queries is pretty noisy, so rather than just logging at some log level, I've gated this logging behind a `QUERY_LOGGING` environment variable. Set it to `QUERY_LOGGING=log` to log just the query. Set it to `QUERY_LOGGING=benchmark` to log both the query and how long it took to run. I'm happy to switch to some other mechanism for enabling this, if using an env var is undesirable. An example benchmarking log message would look like this: ``` [2023-09-15 05:57:48] DEV: Ran the following query in 3ms: Executed (default): SELECT `podcast`.`id`, `podcast`.`title`, COUNT('*') AS `item_count`, `libraryItem`.`id` AS `libraryItem.id`, `libraryItem`.`ino` AS `libraryItem.ino`, `libraryItem`.`path` AS `libraryItem.path`, `libraryItem`.`relPath` AS `libraryItem.relPath`, `libraryItem`.`mediaId` AS `libraryItem.mediaId`, `libraryItem`.`mediaType` AS `libraryItem.mediaType`, `libraryItem`.`isFile` AS `libraryItem.isFile`, `libraryItem`.`isMissing` AS `libraryItem.isMissing`, `libraryItem`.`isInvalid` AS `libraryItem.isInvalid`, `libraryItem`.`mtime` AS `libraryItem.mtime`, `libraryItem`.`ctime` AS `libraryItem.ctime`, `libraryItem`.`birthtime` AS `libraryItem.birthtime`, `libraryItem`.`size` AS `libraryItem.size`, `libraryItem`.`lastScan` AS `libraryItem.lastScan`, `libraryItem`.`lastScanVersion` AS `libraryItem.lastScanVersion`, `libraryItem`.`libraryFiles` AS `libraryItem.libraryFiles`, `libraryItem`.`extraData` AS `libraryItem.extraData`, `libraryItem`.`createdAt` AS `libraryItem.createdAt`, `libraryItem`.`updatedAt` AS `libraryItem.updatedAt`, `libraryItem`.`libraryId` AS `libraryItem.libraryId`, `libraryItem`.`libraryFolderId` AS `libraryItem.libraryFolderId` FROM `podcasts` AS `podcast` LEFT OUTER JOIN `libraryItems` AS `libraryItem` ON `podcast`.`id` = `libraryItem`.`mediaId` AND `libraryItem`.`mediaType` = 'podcast' GROUP BY `podcast`.`id`, `podcast`.`title` HAVING `item_count` = 0; ``` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
adam added the pull-request label 2026-04-25 00:16:31 +02:00
adam closed this issue 2026-04-25 00:16:31 +02:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/audiobookshelf#3653