query optimization of stats generation