From 1e0ab0f331dcff7e05886723e77360931ebced77 Mon Sep 17 00:00:00 2001 From: Craig Edwards Date: Mon, 12 Aug 2024 16:04:43 +0000 Subject: [PATCH] fix N+1 --- src/game_player.cpp | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/src/game_player.cpp b/src/game_player.cpp index 54a673b..cfafe60 100644 --- a/src/game_player.cpp +++ b/src/game_player.cpp @@ -753,22 +753,52 @@ bool player::save(dpp::snowflake user_id, bool put_backup) if (inv_change) { db::query("DELETE FROM game_owned_items WHERE user_id = ?", {user_id}); - + + std::string query{"INSERT INTO game_owned_items (user_id, item_desc, item_flags, qty) VALUES"}; + db::paramlist p; for (const stacked_item& possession : possessions) { if (possession.name != "[none]") { - db::query("INSERT INTO game_owned_items (user_id, item_desc, item_flags, qty) VALUES(?,?,?,?)", {user_id, possession.name, possession.flags, possession.qty}); + p.emplace_back(user_id); + p.emplace_back(possession.name); + p.emplace_back(possession.flags); + p.emplace_back(possession.qty); + query += "(?,?,?,?),"; } } + if (!possessions.empty()) { + query = query.substr(0, query.length() - 1); + db::query(query, p); + } + p = {}; + query = "INSERT INTO game_owned_items (user_id, item_desc, item_flags, qty) VALUES"; for (const item& herb : herbs) { if (herb.name != "[none]") { - db::query("INSERT INTO game_owned_items (user_id, item_desc, item_flags, qty) VALUES(?,?,?,?)", {user_id, herb.name, herb.flags, 1}); + p.emplace_back(user_id); + p.emplace_back(herb.name); + p.emplace_back(herb.flags); + p.emplace_back(1); + query += "(?,?,?,?),"; } } + if (!herbs.empty()) { + query = query.substr(0, query.length() - 1); + db::query(query, p); + } + p = {}; + query = "INSERT INTO game_owned_items (user_id, item_desc, item_flags, qty) VALUES"; for (const item& spell : spells) { if (spell.name != "[none]") { - db::query("INSERT INTO game_owned_items (user_id, item_desc, item_flags, qty) VALUES(?,?,?,?)", {user_id, spell.name, spell.flags, 1}); + p.emplace_back(user_id); + p.emplace_back(spell.name); + p.emplace_back(spell.flags); + p.emplace_back(1); + query += "(?,?,?,?),"; } } + if (!spells.empty()) { + query = query.substr(0, query.length() - 1); + db::query(query, p); + } inv_change = false; }