Postgres query optimization attempt

This commit is contained in:
Essem 2021-08-09 10:12:48 -05:00
parent 75258f3ebc
commit 253bd4c71c
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C

View file

@ -8,7 +8,7 @@ const connection = new Pool({
}); });
exports.getGuild = async (query) => { 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) => { exports.setPrefix = async (prefix, guild) => {
@ -59,10 +59,10 @@ exports.getCounts = async () => {
}; };
exports.addCount = async (command) => { 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]) { if (!count.rows[0]) {
await connection.query("INSERT INTO counts (command, count) VALUES ($1, $2)", [command, 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]); 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) => { 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) { if (guildDB.rows.length === 0) {
logger.log(`Registering guild database entry for guild ${guild.id}...`); logger.log(`Registering guild database entry for guild ${guild.id}...`);
return await this.addGuild(guild); return await this.addGuild(guild);