Added statistics gathering for queries