add sqlite optimization hints and handle all ignores

This commit is contained in:
Infinidoge 2024-12-28 03:57:27 -05:00
parent 07b4679238
commit bd6466ff5f
Signed by: Infinidoge
SSH key fingerprint: SHA256:GT2StvPQMMfFHyiiFJymQxfTG/z6EWLJ6NWItf5K5sA

View file

@ -72,7 +72,9 @@ async def handle_triggers(ctx, message):
"is_bot": ctx.author.bot, "is_bot": ctx.author.bot,
} }
disabled = await fetch_exists(ctx.bot.db, "SELECT * FROM users WHERE user_id=:author AND disabled IS 1", params) disabled = await fetch_exists(
ctx.bot.db, "SELECT * FROM users WHERE user_id=:author AND unlikely(disabled IS 1)", params
)
if disabled: if disabled:
log.debug(f"User {ctx.author} ({ctx.author.id}) opted out") log.debug(f"User {ctx.author} ({ctx.author.id}) opted out")
@ -86,17 +88,17 @@ async def handle_triggers(ctx, message):
SELECT keyword, user_id, use_embed, row_number() over (partition by user_id) n SELECT keyword, user_id, use_embed, row_number() over (partition by user_id) n
FROM keywords FROM keywords
LEFT JOIN users USING (user_id) LEFT JOIN users USING (user_id)
WHERE disabled IS NOT 1 -- Don't notify users who opted out WHERE likely(disabled IS NOT 1) -- Don't notify users who opted out
AND (notify_self IS 1 OR user_id IS NOT :author) -- Don't notify author unless wanted AND (unlikely(notify_self IS 1) OR user_id IS NOT :author) -- Don't notify author unless wanted
AND (bots_notify IS 1 OR :is_bot IS NOT 1) -- Don't notify from bots unless wanted AND (unlikely(bots_notify IS 1) OR :is_bot IS NOT 1) -- Don't notify from bots unless wanted
AND :author NOT IN ( -- Don't notify if... AND likely(NOT EXISTS(SELECT * FROM ( -- Don't notify if...
SELECT target FROM user_ignores -- Author is ignored in this guild SELECT target FROM user_ignores -- Author or channel is ignored in this guild
WHERE user_id=user_id AND guild_id=guild_id WHERE user_id=user_id AND guild_id=guild_id
UNION UNION
SELECT target FROM user_blocks -- Author is blocked SELECT target FROM user_blocks -- Author is blocked
WHERE user_id=user_id WHERE user_id=user_id
) ) WHERE target IN (:author, :channel)))
AND guild_id=:guild AND contains(:content, keyword, regex) AND guild_id=:guild AND unlikely(contains(:content, keyword, regex))
) )
WHERE n=1 WHERE n=1
""" """