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

They can be rewritten as ```Log.select(:message_id).uniq.to_sql```  if needed, but I don't think it's too important.