small optimization on category SQL queries