Added a query plan recorder for debugging purposes

This commit is contained in:
emeric
2025-07-11 00:11:02 +02:00
parent 2fd7d9767b
commit 59e85ee424
29 changed files with 546 additions and 44 deletions
+1
View File
@@ -32,6 +32,7 @@ add_library(lmsdatabase STATIC
impl/IdType.cpp
impl/Migration.cpp
impl/Object.cpp
impl/QueryPlanRecorder.cpp
impl/Session.cpp
impl/SqlQuery.cpp
impl/Transaction.cpp
+3 -3
View File
@@ -206,7 +206,7 @@ namespace lms::db
LMS_LOG(DB, INFO, "Performing quick database check...");
// Quick check is just a simple integrity check
bool quickCheckPassed{ checkDbIntegrity(*connection, IntegrityCheckType::Quick, [&](std::string_view error) {
const bool quickCheckPassed{ checkDbIntegrity(*connection, IntegrityCheckType::Quick, [&](std::string_view error) {
LMS_LOG(DB, ERROR, "Quick check error: " << error);
}) };
@@ -222,7 +222,7 @@ namespace lms::db
LMS_LOG(DB, INFO, "Checking database integrity...");
bool integrityCheckPassed{ checkDbIntegrity(*connection, IntegrityCheckType::Full, [&](std::string_view error) {
const bool integrityCheckPassed{ checkDbIntegrity(*connection, IntegrityCheckType::Full, [&](std::string_view error) {
LMS_LOG(DB, ERROR, "Integrity check error: " << error);
}) };
@@ -238,7 +238,7 @@ namespace lms::db
LMS_LOG(DB, INFO, "Checking foreign key constraints...");
bool foreignKeyConstraintsPassed{ checkDbForeignKeyConstraints(*connection, [&](std::string_view table, long long rowId, std::string_view referredTable) {
const bool foreignKeyConstraintsPassed{ checkDbForeignKeyConstraints(*connection, [&](std::string_view table, long long rowId, std::string_view referredTable) {
LMS_LOG(DB, ERROR, "Foreign key constraint failed in table '" << table << "', rowid = " << rowId << ", referred table = '" << referredTable << "'");
}) };
+2 -2
View File
@@ -36,8 +36,6 @@ namespace lms::db
public:
Db(const std::filesystem::path& dbPath, std::size_t connectionCount);
Session& getTLSSession() override;
void executeSql(const std::string& sql);
private:
@@ -46,6 +44,8 @@ namespace lms::db
friend class Session;
Session& getTLSSession() override;
core::RecursiveSharedMutex& getMutex() { return _sharedMutex; }
Wt::Dbo::SqlConnectionPool& getConnectionPool() { return *_connectionPool; }
@@ -0,0 +1,112 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "QueryPlanRecorder.hpp"
#include <memory>
#include <mutex>
#include <Wt/Dbo/SqlStatement.h>
#include <Wt/Dbo/Transaction.h>
#include "core/ILogger.hpp"
namespace lms::db
{
std::unique_ptr<IQueryPlanRecorder> createQueryPlanRecorder()
{
return std::make_unique<QueryPlanRecorder>();
}
QueryPlanRecorder::QueryPlanRecorder()
{
LMS_LOG(DB, INFO, "Recording database query plans");
}
QueryPlanRecorder::~QueryPlanRecorder() = default;
void QueryPlanRecorder::visitQueryPlans(const QueryPlanVisitor& visitor) const
{
const std::shared_lock lock{ _mutex };
for (const auto& [query, plan] : _queryPlans)
visitor(query, plan);
}
void QueryPlanRecorder::recordQueryPlanIfNeeded(Wt::Dbo::Session& session, const std::string& query)
{
{
const std::shared_lock lock{ _mutex };
if (_queryPlans.contains(query))
return;
}
Wt::Dbo::Transaction transaction{ session };
Wt::Dbo::SqlConnection* connection{ transaction.connection() };
auto statement{ connection->prepareStatement("EXPLAIN QUERY PLAN " + query) };
statement->execute();
std::map<int, std::string> entries{ { 0, "" } };
std::map<int, std::vector<int>> relationships;
std::string detail;
while (statement->nextRow())
{
detail.clear();
int id{};
int parent{};
int unused{};
if (statement->getResult(0, &id)
&& statement->getResult(1, &parent)
&& statement->getResult(2, &unused)
&& statement->getResult(3, &detail, static_cast<int>(detail.capacity())))
{
entries.emplace(id, detail);
relationships[parent].push_back(id);
}
}
// format
std::string result;
std::function<void(int, unsigned)> formatQuery = [&](int id, unsigned level) -> void {
for (std::size_t i{}; i < level; ++i)
result += '\t';
result += entries.at(id);
result += '\n';
auto itChildren = relationships.find(id);
if (itChildren == relationships.end())
return;
for (int child : itChildren->second)
formatQuery(child, level + 1);
};
formatQuery(0, 0);
{
const std::unique_lock lock{ _mutex };
_queryPlans.try_emplace(query, std::move(result));
}
}
} // namespace lms::db
@@ -0,0 +1,48 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <map>
#include <shared_mutex>
#include <string>
#include <Wt/Dbo/Session.h>
#include "database/IQueryPlanRecorder.hpp"
namespace lms::db
{
class QueryPlanRecorder : public IQueryPlanRecorder
{
public:
QueryPlanRecorder();
~QueryPlanRecorder() override;
QueryPlanRecorder(const QueryPlanRecorder&) = delete;
QueryPlanRecorder& operator=(const QueryPlanRecorder&) = delete;
void visitQueryPlans(const QueryPlanVisitor& visitor) const override;
void recordQueryPlanIfNeeded(Wt::Dbo::Session& session, const std::string& query);
private:
mutable std::shared_mutex _mutex;
std::map<std::string, std::string> _queryPlans;
};
} // namespace lms::db
+28 -4
View File
@@ -19,22 +19,39 @@
#pragma once
#include <functional>
#include <string>
#include <string_view>
#include <Wt/Dbo/Dbo.h>
#include <Wt/Dbo/Call.h>
#include <Wt/Dbo/Query.h>
#include <Wt/Dbo/Session.h>
#include <Wt/Dbo/collection.h>
#include <Wt/WDateTime.h>
#include "core/ITraceLogger.hpp"
#include "core/Service.hpp"
#include "database/Types.hpp"
#include "QueryPlanRecorder.hpp"
namespace lms::db::utils
{
#define ESCAPE_CHAR_STR "\\"
static inline constexpr char escapeChar{ '\\' };
std::string escapeLikeKeyword(std::string_view keywords);
Wt::WDateTime normalizeDateTime(const Wt::WDateTime& dateTime);
namespace details
{
template<typename Query>
void recordQueryPlanIfNeeded(const Query& query)
{
if (IQueryPlanRecorder * recorder{ core::Service<IQueryPlanRecorder>::get() })
static_cast<QueryPlanRecorder*>(recorder)->recordQueryPlanIfNeeded(query.session(), query.asString());
}
} // namespace details
template<typename Query>
void applyRange(Query& query, std::optional<Range> range)
{
@@ -82,13 +99,18 @@ namespace lms::db::utils
template<typename Query, typename UnaryFunc>
void forEachQueryResult(const Query& query, UnaryFunc&& func)
{
details::recordQueryPlanIfNeeded(query);
LMS_SCOPED_TRACE_DETAILED_WITH_ARG("Database", "ForEachQueryResult", "Query", query.asString());
forEachResult(query.resultList(), std::forward<UnaryFunc>(func));
}
template<typename T, typename Query>
std::vector<T> fetchQueryResults(const Query& query)
{
details::recordQueryPlanIfNeeded(query);
LMS_SCOPED_TRACE_DETAILED_WITH_ARG("Database", "FetchQueryResults", "Query", query.asString());
auto collection{ query.resultList() };
@@ -98,6 +120,8 @@ namespace lms::db::utils
template<typename Query>
std::vector<typename QueryResultType<Query>::type> fetchQueryResults(const Query& query)
{
details::recordQueryPlanIfNeeded(query);
LMS_SCOPED_TRACE_DETAILED_WITH_ARG("Database", "FetchQueryResults", "Query", query.asString());
auto collection{ query.resultList() };
@@ -107,6 +131,8 @@ namespace lms::db::utils
template<typename Query>
auto fetchQuerySingleResult(const Query& query)
{
details::recordQueryPlanIfNeeded(query);
LMS_SCOPED_TRACE_DETAILED_WITH_ARG("Database", "FetchQuerySingleResult", "Query", query.asString());
return query.resultValue();
}
@@ -183,6 +209,4 @@ namespace lms::db::utils
call.run();
}
}
Wt::WDateTime normalizeDateTime(const Wt::WDateTime& dateTime);
} // namespace lms::db::utils
@@ -18,6 +18,7 @@
*/
#include "database/objects/Artwork.hpp"
#include <Wt/Dbo/WtSqlTraits.h>
#include "database/Session.hpp"
@@ -0,0 +1,39 @@
/*
* Copyright (C) 2025 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <functional>
#include <memory>
namespace lms::db
{
// Due to technical limitations, query plans are recorded globally across all databases.
// As a result, this class is implemented as a singleton rather than being owned per DB instance.
class IQueryPlanRecorder
{
public:
virtual ~IQueryPlanRecorder() = default;
using QueryPlanVisitor = std::function<void(std::string_view query, std::string_view plan)>;
virtual void visitQueryPlans(const QueryPlanVisitor& visitor) const = 0;
};
std::unique_ptr<IQueryPlanRecorder> createQueryPlanRecorder();
} // namespace lms::db