reduce number of DB queries for stats function