fixes #8565 - only get unique, unused message/source IDs to speed up expiry

it's better not to write id in sql strings, but use sources.id, messages.id, etc. here it's not important because there's no joins, but it's just best practice IMO.