Optimize query in get_tags