From 253bd4c71cd708ad00ed0e9e59f569e70fed0745 Mon Sep 17 00:00:00 2001 From: Essem Date: Mon, 9 Aug 2021 10:12:48 -0500 Subject: [PATCH] Postgres query optimization attempt --- utils/database/postgresql.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/utils/database/postgresql.js b/utils/database/postgresql.js index 992f7cd..44b9691 100644 --- a/utils/database/postgresql.js +++ b/utils/database/postgresql.js @@ -8,7 +8,7 @@ const connection = new Pool({ }); exports.getGuild = async (query) => { - return (await connection.query("SELECT * FROM guilds WHERE guild_id = $1", [query])).rows[0]; + return (await connection.query("SELECT * FROM guilds WHERE guild_id = $1 limit 1", [query])).rows[0]; }; exports.setPrefix = async (prefix, guild) => { @@ -59,10 +59,10 @@ exports.getCounts = async () => { }; exports.addCount = async (command) => { - let count = await connection.query("SELECT * FROM counts WHERE command = $1", [command]); + let count = await connection.query("SELECT * FROM counts WHERE command = $1 limit 1", [command]); if (!count.rows[0]) { await connection.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 0]); - count = await connection.query("SELECT * FROM counts WHERE command = $1", [command]); + count = await connection.query("SELECT * FROM counts WHERE command = $1 limit 1", [command]); } await connection.query("UPDATE counts SET count = $1 WHERE command = $2", [count.rows[0].count ? count.rows[0].count + 1 : 1, command]); }; @@ -75,7 +75,7 @@ exports.addGuild = async (guild) => { }; exports.fixGuild = async (guild) => { - const guildDB = await connection.query("SELECT * FROM guilds WHERE guild_id = $1", [guild.id]); + const guildDB = await connection.query("SELECT exists(SELECT 1 FROM guilds WHERE guild_id = $1)", [guild.id]); if (guildDB.rows.length === 0) { logger.log(`Registering guild database entry for guild ${guild.id}...`); return await this.addGuild(guild);