BZ1073093 Alert template query does not scale well

With a high number of alert definitions, the count query does not scale well.