I do a SELECT to decide if I send a notification to a user. (10.3.23-MariaDB)
First part checks if there are 0 unread notifications, if 0 no further SELECT is needed.
If not, I count how many notifications the user has since last equal notification (in this case there always one to count from) and check it against unread ones number user_sess.unread_noti
SELECT 'yes' AS do_insert
FROM
(SELECT unread_noti
FROM user_sess
WHERE user_id = 100)user_sess
WHERE user_sess.unread_noti = 0
OR (SELECT COUNT(*) FROM notification
WHERE user_id=100 AND id > 400
) >= user_sess.unread_noti
Now I want to check multiple users for the same notification, and make it more efficient by doing the SELECT for multiple users at ones, for example something like this (INCORRECT example):
SELECT user_sess.user_id
FROM user_sess
LEFT JOIN notification ON user_sess.unread_noti > 0
AND notification.id > 400
AND notification.user_id=user_sess.user_id
WHERE user_sess.user_id IN (100,101)
AND ( user_sess.unread_noti = 0
OR COUNT(notification.id) >= user_sess.unread_noti
)
The notification.id to count from, can be the same for multiple users because I INSERT them in bulk, so they will occupy the same "spot".
notification:
id Primary int(11)
user_id Index int(11)
user_sess:
user_id Primary int(11)
unread_noti tinyint(3)